Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZMS-172 #733

Merged
merged 21 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b3d48ce
Added submission api endpoint to api docs generation
NickOvt Apr 23, 2024
c01170f
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Apr 29, 2024
3f1c3e0
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Apr 29, 2024
1a0272d
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 6, 2024
b2aaba5
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 9, 2024
7d4eb9f
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 9, 2024
3bc4530
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 16, 2024
8fdc0d3
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 27, 2024
2607896
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt May 30, 2024
10836eb
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Jun 5, 2024
ea9e4e0
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Jul 25, 2024
4c2f07d
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Aug 1, 2024
c0f0660
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Sep 2, 2024
ceeacc7
Merge branch 'master' of github.com:nodemailer/wildduck
NickOvt Sep 5, 2024
6a9bae9
on attachment upload calculate file content hash
NickOvt Sep 13, 2024
2d8b782
make stream into separate file, refactor
NickOvt Sep 13, 2024
e3afdec
create stream during the try to store. otherwise getting stuck
NickOvt Sep 13, 2024
6515b27
refactor file content hash update
NickOvt Sep 16, 2024
daa9767
safer file content hash handling
NickOvt Sep 16, 2024
d195fb9
refactor code. Fix possible race condition
NickOvt Sep 23, 2024
7b99366
refactor function. Pass callback as last function param
NickOvt Sep 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions lib/attachments/gridstore-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const errors = require('../errors');
const log = require('npmlog');
const crypto = require('crypto');
const base64Offset = require('./base64-offset');
const FileHashCalculatorStream = require('../filehash-stream');

// Set to false to disable base64 decoding feature
const FEATURE_DECODE_ATTACHMENTS = true;
Expand All @@ -32,6 +33,23 @@ class GridstoreStorage {
});
}

updateFileWithContentHashMetadata(args, hash, calculatedFileContentHash, callback) {
this.gridfs.collection(this.bucketName + '.files').findOneAndUpdate(
{
_id: hash
},
{
$set: {
'metadata.fileContentHash': calculatedFileContentHash
}
},
{
returnDocument: 'after'
},
() => callback(...args) // do not really care about error here. If error then highly likely the file has not been uploaded either
);
}

async get(attachmentId) {
let attachmentData = await this.gridfs.collection(this.bucketName + '.files').findOne({
_id: attachmentId
Expand Down Expand Up @@ -129,6 +147,13 @@ class GridstoreStorage {
let storeLock;

let attachmentCallback = (...args) => {
// store finished uploading, add the hash of the file contents to file metadata
let calculatedFileContentHash;

if (args.length > 2) {
calculatedFileContentHash = args[2];
}

if (storeLock) {
log.silly('GridStore', '[%s] UNLOCK lock=%s status=%s', instance, lockId, storeLock.success ? 'locked' : 'empty');
if (storeLock.success) {
Expand All @@ -137,7 +162,11 @@ class GridstoreStorage {
// might be already finished if retrying after delay
return;
}
callback(...args);
if (calculatedFileContentHash) {
// locked upload, new file
this.updateFileWithContentHashMetadata(args, hash, calculatedFileContentHash, callback);
return; // return from attachmentCallback. Top level callback will be ran after hash update
}
});
// unset variable to prevent double releasing
storeLock = false;
Expand All @@ -149,6 +178,11 @@ class GridstoreStorage {
// might be already finished if retrying after delay
return;
}
if (calculatedFileContentHash) {
// no lock upload, new file
this.updateFileWithContentHashMetadata(args, hash, calculatedFileContentHash, callback);
return; // return from attachmentCallback. Top level callback will be ran after hash update
}
callback(...args);
};

Expand All @@ -159,6 +193,8 @@ class GridstoreStorage {
return;
}

let fileHashCalculator = new FileHashCalculatorStream();

this.gridfs.collection(this.bucketName + '.files').findOneAndUpdate(
{
_id: hash
Expand Down Expand Up @@ -282,13 +318,14 @@ class GridstoreStorage {
attachmentCallback(err);
});

store.once('finish', () => attachmentCallback(null, id));
store.once('finish', () => attachmentCallback(null, id, fileHashCalculator.hash));

if (!metadata.decoded) {
store.end(attachment.body);
fileHashCalculator.pipe(store);
fileHashCalculator.end(attachment.body);
} else {
let decoder = new libbase64.Decoder();
decoder.pipe(store);
decoder.pipe(fileHashCalculator).pipe(store);
decoder.once('error', err => {
// pass error forward
store.emit('error', err);
Expand Down
38 changes: 38 additions & 0 deletions lib/filehash-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const Transform = require('stream').Transform;
const crypto = require('crypto');

class FileHashCalculatorStream extends Transform {
constructor(options) {
super(options);
this.bodyHash = crypto.createHash('sha256');
this.hash = null;
}

updateHash(chunk) {
this.bodyHash.update(chunk);
}

_transform(chunk, encoding, callback) {
if (!chunk || !chunk.length) {
return callback();
}

if (typeof chunk === 'string') {
chunk = Buffer.from(chunk, encoding);
}

this.updateHash(chunk);
this.push(chunk);

callback();
}

_flush(done) {
this.hash = this.bodyHash.digest('base64');
done();
}
}

module.exports = FileHashCalculatorStream;