diff options
author | David Tomberlin <david.tomberlin@sendgrid.com> | 2012-01-25 15:19:17 -0800 |
---|---|---|
committer | David Tomberlin <david.tomberlin@sendgrid.com> | 2012-01-25 15:19:17 -0800 |
commit | 3dc338de413f0789ad03569c8b58f417f1457d4d (patch) | |
tree | 1d0d25a6f17c61ce1cba76909a140bbb7f0372db | |
parent | a19128e8ed00ea89cb9e8f34726d5ff1dde9ddb6 (diff) | |
download | sendgrid-nodejs-3dc338de413f0789ad03569c8b58f417f1457d4d.zip sendgrid-nodejs-3dc338de413f0789ad03569c8b58f417f1457d4d.tar.gz sendgrid-nodejs-3dc338de413f0789ad03569c8b58f417f1457d4d.tar.bz2 |
initial changes to files
-rw-r--r-- | lib/email.js | 80 |
1 files changed, 64 insertions, 16 deletions
diff --git a/lib/email.js b/lib/email.js index af3684e..ab25a45 100644 --- a/lib/email.js +++ b/lib/email.js @@ -2,6 +2,7 @@ var querystring = require('querystring'); var https = require('https'); +var http = require('http'); var _ = require('underscore'); var fs = require('fs'); var SmtpapiHeaders = require('./smtpapi_headers'); @@ -20,8 +21,7 @@ function Email(params) { bcc: [], replyto: '', date: new Date(), - files: {}, - file_data: {}, + files: [], headers: {} }; @@ -90,26 +90,74 @@ Email.prototype.setFilterSetting = function(filters) { this.smtpapi.setFilterSetting(filters); } -Email.prototype.addFile = function(filename, filepath) { - this.files[filename] = filepath; +Email.prototype.addFile = function(file_object) { + var file = file_object; + if (!_.isUndefined(file.content)) { + file.type = 'content'; + } else if (!_.isUndefined(file.path)) { + file.type = 'path'; + if (_.isEmtpy(file.contentType)) { + file.contentType = mime.lookup(file.path); + } + } else if (!_.isUndefined(file.url)) { + file.type = 'url'; + if (_.isEmtpy(file.contentType)) { + file.contentType = mime.lookup(file.url); + } else { + file.type = 'none'; + file.contentType = 'none'; + } + this.files.push(file); } Email.prototype.processFiles = function(callback) { var self = this; var attachments_count = _.size(this.files); - _(this.files).each(function(v, k) { - fs.readFile(v, function(error, data) { - attachments_count--; - if (error) { + _(this.files).each(function(file) { + if (file.type === 'path') { + fs.readFile(file, function(error, data) { + attachments_count--; + if (error) { + return callback(false, error); + } else { + file.content = data; + } + }); + } else if (file.type === 'url') { + var urlParts = url.parse(file.url); + var host = urlParts.host; + var path = urlParts.path; + var req = https.request({host: host, path: path, method: 'GET'}, function(res) { + attachments_count--; + var bufferArray = []; + var totalBufferSize = 0; + res.setEncoding('base64'); + res.on('data', function(chunk) { + totalBufferSize += Buffer.byteLength(chunk); + bufferArray.push(chunk); + }).on('end', function() { + var buffer = new Buffer(totalBufferSize, 'base64'); + var bufPos = 0; + _(bufferArray).each(function(val) { + bufPos += buffer.write(val, bufPos, 'base64'); + }); + file.content = buffer; + }); + }).on('error', function(error) { return callback(false, error); - } else { - self.file_data[k] = data; - } - - if (attachments_count == 0) { - callback(true); - } - }); + }); + } else if (file.type === 'content') { + attachments_count--; + // nothing else to do, right? + } else if (file.type === 'none') { + attachments_count--; + // nothing to see but us chickens, don't punch us. + } + if (attachments_count == 0) { + callback(true); + } else { + callback(false); + } }); } |