Store binary data in MongoDB as buffers

This commit is contained in:
Andris Reinman 2017-03-21 11:35:59 +02:00
parent 4bbeedf050
commit 88a23e1ba0
3 changed files with 62 additions and 23 deletions

View file

@ -20,18 +20,39 @@ const transporter = nodemailer.createTransport({
transporter.sendMail({
envelope: {
from: 'andrisööö@kreata.ee',
from: 'andris@kreata.ee',
to: [recipient]
},
from: 'andrisööö@kreata.ee',
from: 'andris@kreata.ee',
to: recipient,
subject: 'Test ööö message [' + Date.now() + ']',
text: 'Hello world! Current time is ' + new Date().toString(),
text: 'Hello world! Current time is ' + new Date().toString() + ' <img src="cid:note@example.com"/>',
html: '<p>Hello world! Current time is <em>' + new Date().toString() + '</em></p>',
attachments: [{
path: __dirname + '/swan.jpg',
filename: 'swän.jpg'
}]
attachments: [
// attachment as plaintext
{
filename: 'notes.txt',
content: 'Some notes about this e-mail',
contentType: 'text/plain' // optional, would be detected from the filename
},
// Small Binary Buffer attachment, should be kept with message
{
filename: 'image.png',
content: new Buffer('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
'//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC', 'base64'),
cid: 'note@example.com' // should be as unique as possible
},
// Large Binary Buffer attachment, should be kept separately
{
path: __dirname + '/swan.jpg',
filename: 'swän.jpg'
}
]
}, (err, info) => {
if (err && err.response) {
console.log('Message failed: %s', err.response);

View file

@ -118,7 +118,7 @@ class Indexer {
let res = new PassThrough();
let first = true;
let root = true;
let remainder = '';
let remainder = false;
// make sure that mixed body + mime gets rebuilt correctly
let append = (data, force) => {
@ -126,10 +126,21 @@ class Indexer {
data = data.join('\r\n');
}
if (remainder || data || force) {
res.write(Buffer.from((first ? '' : '\r\n') + (remainder || '') + (data || ''), 'binary'));
first = false;
if (!first) {
res.write('\r\n');
} else {
first = false;
}
if (remainder && remainder.length) {
res.write(remainder);
}
if (data) {
res.write(Buffer.from(data, 'binary'));
}
}
remainder = '';
remainder = false;
};
let walk = (node, next) => {
@ -139,8 +150,13 @@ class Indexer {
}
root = false;
remainder = node.body || '';
if (node.body && node.body.buffer) {
remainder = node.body.buffer;
} else if (typeof node.body === 'string') {
remainder = Buffer.from(node.body, 'binary');
} else {
remainder = node.body;
}
let finalize = () => {
if (node.boundary) {
@ -305,7 +321,7 @@ class Indexer {
return continueProcessing();
});
store.end(Buffer.from(node.body, 'binary'));
store.end(node.body);
} else {
continueProcessing();
}

View file

@ -59,7 +59,7 @@ class MIMEParser {
if (this._node.parentBoundary && (line === '--' + this._node.parentBoundary || line === '--' + this._node.parentBoundary + '--')) {
if (this._node.parsedHeader['content-type'].value === 'message/rfc822') {
this._node.message = module.exports(this._node.body.join(''));
this._node.message = parse(this._node.body.join(''));
}
if (line === '--' + this._node.parentBoundary) {
@ -108,12 +108,12 @@ class MIMEParser {
finalizeTree() {
let walker = node => {
if (node.body) {
let lineCount = node.body.length;
node.body = node.body.join('').
// ensure proper line endings
replace(/\r?\n/g, '\r\n');
node.size = (node.body || '').length;
node.lineCount = lineCount;
node.lineCount = node.body.length;
node.body = Buffer.from(
node.body.join('').
// ensure proper line endings
replace(/\r?\n/g, '\r\n'), 'binary');
node.size = node.body.length;
}
node.childNodes.forEach(walker);
@ -281,7 +281,7 @@ class MIMEParser {
}
}
module.exports = function (rfc822) {
function parse(rfc822) {
let parser = new MIMEParser(rfc822);
let response;
@ -290,4 +290,6 @@ module.exports = function (rfc822) {
response = parser.tree.childNodes[0] || false;
return response;
};
}
module.exports = parse;