I'm parsing an email and whenever I hit an attachment, I want to upload the attachment to S3. Whenever an attachment is found I get a stream for the attachment. I want to get the MD5sum of the attachment, then upload it to S3 using the stream again.
Is there anyway to do this without first writing the stream to a file, calculating MD5, then making a readable stream from the file and sending a stream to knox (which would be 3 stream reads)?
parser.on("attachment", function(attachment){
//Calculate MD5 sum
var md5sum = '';
s3client.putStream(attachment.stream, 'blah.jpeg', {
'md5sum': md5sum
}, function(err, res) {});
});
I think this should work?
var crypt = require("crypto")
function hashFile(attachment){
var hash = crypt.createHash("md5")
.update(attachment)
.digest("base64");
console.log(hash);
}
hashFile("some attachment blah");
Where the attachment you pass in get's MD5 hashed and return base64 encoded (you could also specify binary, or hex in the digest method)
UPDATE Ok so I looked at that mail parser and notice what it says in the help:
https://github.com/andris9/mailparser#default-behavior
Notice the mention of the checksum property.
Now look at line 283 here: https://github.com/andris9/mailparser/blob/master/lib/mailparser.js
That checksum is the MD5 you want.
So just do attachment.checksum and be done