Skip to content

Commit 54233b8

Browse files
committed
Added MailMessage.attachmentnosql().
1 parent f321ce7 commit 54233b8

3 files changed

Lines changed: 92 additions & 6 deletions

File tree

changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- added: `MailMessage.high()` sets `high` priority of the email messsage
1515
- added: `MailMessage.low()` sets `low` priority of the email messsage
1616
- added: `MailMessage.confidential()` sets `Sensitivity` header with `confidential` value
17+
- added: `MailMessage.attachmentnosql(db, id, [name])` sends a file from NoSQL embedded database
1718
- added: `SchemaBuilderEntity.$stop()` stops the async list
1819
- added: `SchemaOptions.stop()` alias to `$.model.$stop()`
1920
- added: `SchemaOptions.next()` alias to `$.model.$next()`

mail.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,21 @@ Message.prototype.attachment = function(filename, name) {
267267
return this;
268268
};
269269

270+
Message.prototype.attachmentnosql = function(db, id, name) {
271+
272+
var extension;
273+
var type;
274+
275+
if (name) {
276+
extension = framework_utils.getExtension(name);
277+
type = framework_utils.getContentType(extension);
278+
}
279+
280+
!this.files && (this.files = []);
281+
this.files.push({ nosql: db, name: name, filename: id, type: type, extension: extension });
282+
return this;
283+
};
284+
270285
/**
271286
* Clears a timeout for sending emails (if the email is sent through the F.onMail)
272287
* @return {Message}
@@ -368,6 +383,7 @@ Mailer.prototype.destroy = function(obj) {
368383
};
369384

370385
const ATTACHMENT_SO = { encoding: 'base64' };
386+
const ATTACHMENT_SO_NOSQL = { encoding: 'base64', start: 2000 };
371387

372388
Mailer.prototype.$writeattachment = function(obj) {
373389

@@ -379,11 +395,37 @@ Mailer.prototype.$writeattachment = function(obj) {
379395
return this;
380396
}
381397

398+
var stream;
399+
400+
if (attachment.nosql) {
401+
NOSQL(attachment.nosql).binary.readbase64(attachment.filename, function(err, stream, meta) {
402+
if (err) {
403+
F.error(err, 'Mail.attachment()', attachment.filename);
404+
mailer.$writeattachment(obj);
405+
} else {
406+
407+
if (!attachment.name) {
408+
attachment.name = meta.name;
409+
attachment.type = meta.type;
410+
attachment.extension = U.getExtension(meta.name);
411+
}
412+
413+
writeattachemnt_stream(attachment, obj, stream);
414+
}
415+
});
416+
} else {
417+
stream = Fs.createReadStream(attachment.filename, ATTACHMENT_SO);
418+
writeattachemnt_stream(attachment, obj, stream);
419+
}
420+
421+
return this;
422+
};
423+
424+
function writeattachemnt_stream(attachment, obj, stream) {
425+
382426
var name = attachment.name;
383-
var stream = Fs.createReadStream(attachment.filename, ATTACHMENT_SO);
427+
var isCalendar = attachment.extension === 'ics';
384428
var message = [];
385-
var extension = attachment.extension;
386-
var isCalendar = extension === 'ics';
387429

388430
message.push('--' + obj.boundary);
389431

@@ -409,8 +451,7 @@ Mailer.prototype.$writeattachment = function(obj) {
409451
mailer.$writeattachment(obj);
410452
});
411453

412-
return this;
413-
};
454+
}
414455

415456
function writeattachment_data(chunk) {
416457

nosql.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const JSONBOOL = '":true ';
6565
const DIRECTORYLENGTH = 9;
6666
const IMAGES = { gif: 1, jpg: 1, jpeg: 1, png: 1, svg: 1 };
6767
const BINARYREADDATA = { start: BINARY_HEADER_LENGTH };
68+
const BINARYREADDATABASE64 = { start: BINARY_HEADER_LENGTH, encoding: 'base64' };
6869
const BINARYREADMETA = { start: 0, end: BINARY_HEADER_LENGTH - 1, encoding: 'binary' };
6970
const CLEANDBTICKS = 86400000 * 2; // 48 hours
7071

@@ -3376,7 +3377,7 @@ DatabaseBuilder.prototype.repository = function(key, value) {
33763377
DatabaseBuilder.prototype.compile = function() {
33773378
var self = this;
33783379
var raw = self.$code.join('');
3379-
var code = 'var repository=$F.repository;var options=$F.options;var arg=$F.arg;var fn=$F.fn;var $is=false;var $tmp;' + raw + (self.$code.length && raw.substring(raw.length - 7) !== 'return;' ? 'if(!$is)return;' : '') + 'if(options.fields){var $doc={};for(var $i=0;$i<options.fields.length;$i++){var prop=options.fields[$i];$doc[prop]=doc[prop]}if(options.sort)$doc[options.sort.name]=doc[options.sort.name];return $doc}else if(options.fields2){var $doc={};var $keys=Object.keys(doc);for(var $i=0;$i<$keys.length;$i++){var prop=$keys[$i];!options.fields2[prop]&&($doc[prop]=doc[prop])}return $doc}else{return doc}';
3380+
var code = 'var repository=$F.repository,options=$F.options,arg=$F.arg,fn=$F.fn,$is=false,$tmp;var R=repository;' + raw + (self.$code.length && raw.substring(raw.length - 7) !== 'return;' ? 'if(!$is)return;' : '') + 'if(options.fields){var $doc={};for(var $i=0;$i<options.fields.length;$i++){var prop=options.fields[$i];$doc[prop]=doc[prop]}if(options.sort)$doc[options.sort.name]=doc[options.sort.name];return $doc}else if(options.fields2){var $doc={};var $keys=Object.keys(doc);for(var $i=0;$i<$keys.length;$i++){var prop=$keys[$i];!options.fields2[prop]&&($doc[prop]=doc[prop])}return $doc}else{return doc}';
33803381
var opt = self.$options;
33813382
self.$inlinesort = !!(opt.take && opt.sort && opt.sort !== null);
33823383
self.$limit = (opt.take || 0) + (opt.skip || 0);
@@ -5040,6 +5041,49 @@ Binary.prototype.read = function(id, callback, count) {
50405041
return self;
50415042
};
50425043

5044+
Binary.prototype.readbase64 = function(id, callback, count) {
5045+
5046+
var self = this;
5047+
5048+
if (count > 3) {
5049+
callback(new Error('File not found.'));
5050+
return self;
5051+
}
5052+
5053+
var isnew = false;
5054+
5055+
if (id > 0)
5056+
isnew = true;
5057+
else if (id[0] === 'B' || id[0] === 'b') {
5058+
id = +id.substring(id.length - DIRECTORYLENGTH);
5059+
isnew = true;
5060+
} else if (id.indexOf('#') === -1)
5061+
id = self.db.name + '#' + id;
5062+
5063+
var filename;
5064+
5065+
if (isnew) {
5066+
filename = Path.join(self.$directory(id), id.toString().padLeft(DIRECTORYLENGTH, '0') + EXTENSION_BINARY);
5067+
} else
5068+
filename = framework_utils.join(self.directory, id + EXTENSION_BINARY);
5069+
5070+
var stream = Fs.createReadStream(filename, BINARYREADMETA);
5071+
stream.on('error', err => callback(err));
5072+
stream.on('data', function(buffer) {
5073+
var json = buffer.toString('utf8').replace(REG_CLEAN, '');
5074+
if (json) {
5075+
var meta = JSON.parse(json, jsonparser);
5076+
stream = Fs.createReadStream(filename, BINARYREADDATABASE64);
5077+
callback(null, stream, meta);
5078+
CLEANUP(stream);
5079+
} else
5080+
setTimeout(readfileattempt, 100, self, id, callback, count || 1);
5081+
});
5082+
5083+
return self;
5084+
};
5085+
5086+
50435087
function readfileattempt(self, id, callback, count) {
50445088
self.read(id, callback, count + 1);
50455089
}

0 commit comments

Comments
 (0)