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 17 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
36 changes: 34 additions & 2 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 Down Expand Up @@ -129,6 +130,35 @@ class GridstoreStorage {
let storeLock;

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

this.gridfs.collection(this.bucketName + '.files').findOneAndUpdate(
{
_id: hash
},
{
$set: {
'metadata.fileContentHash': calculatedFileContentHash
}
},
{
returnDocument: 'after'
},
(err, res) => {
if (err) {
return attachmentCallback(err);
NickOvt marked this conversation as resolved.
Show resolved Hide resolved
}

if (res && res.value && res.value.metadata.fileContentHash && res.value.metadata.fileContentHash === calculatedFileContentHash) {
// all good?
// do nothing
}
}
);
}

if (storeLock) {
log.silly('GridStore', '[%s] UNLOCK lock=%s status=%s', instance, lockId, storeLock.success ? 'locked' : 'empty');
if (storeLock.success) {
Expand Down Expand Up @@ -159,6 +189,8 @@ class GridstoreStorage {
return;
}

let fileHashCalculator = new FileHashCalculatorStream();

this.gridfs.collection(this.bucketName + '.files').findOneAndUpdate(
{
_id: hash
Expand Down Expand Up @@ -282,13 +314,13 @@ 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);
} 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;