From c9cd5dfc84408dc2f2c7fdeecad444e8a872ebe2 Mon Sep 17 00:00:00 2001 From: Ivo Bathke Date: Tue, 8 Sep 2015 12:58:08 +0200 Subject: [PATCH] added option to encode a local file --- README.md | 17 +++++++++++++++-- index.js | 50 +++++++++++++++++++++++++++++++++++--------------- test/tests.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index c5ae3a1..0d44cfc 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ node-base64-image ================= -Download images from remote URLs and encode/decode them to base64 +Download images from remote URLs or use local images and encode/decode them to base64 To install `npm install node-base64-image --save` @@ -26,9 +26,22 @@ base64.base64encode('www.someurl.com/image.jpg', function (err, image) { }); ``` +#### Encode a local image +``` +var path = __dirname + '/../test.jpg', + options = {localFile: true, string: true}; + +base64.base64encode(path, function (err, image) { + if (err) { console.log(err); } + console.log(image); +}); +``` + + ##### Parameters url (string) - the url of the image to be downloaded and encoded. options (object) - if string is passed is with 'true', the image returned will be a base64 string. Otherwise, the base64 buffer is returned. + if localFile is passed is with 'true', a local image instead of a remote one will be used callback (function) - the callback will contain the err object and the encoded image object. #### Decode and write a base64 encoded image to disk @@ -42,7 +55,7 @@ base64.base64decoder(imageData, options, function (err, saved) { }); ``` -##### Paramaters +##### Parameters imageData (buffer) - the base64 image buffer. options (object) - contains the 'filename' property; this will be the written image file. callback (function) - the callback will contain the err object and the 'successful save' string. diff --git a/index.js b/index.js index d88b5aa..63439df 100644 --- a/index.js +++ b/index.js @@ -14,21 +14,39 @@ var base64encoder = function (url, options, callback) { throw new Error('URL cannot be empty!'); } - request({url: url, encoding: null}, function (err, res, body) { - if (err) { return callback(err); } - - if (body && res.statusCode === 200) { - var image; - - if (options && options.string === true) { - image = body.toString('base64'); - return callback(null, image); - } else { - image = new Buffer(body, 'base64'); - return callback(null, image); - } + var encoder = function (body, options) { + var image; + + if (options && options.string === true) { + image = body.toString('base64'); + return callback(null, image); + } else { + image = new Buffer(body, 'base64'); + return callback(null, image); } - }); + }; + + if (options && options.localFile === true) { + fs.readFile(url, function (err, data) { + if (err) { + return callback(err); + } + + return encoder(data, options); + }); + } + else { + request({url: url, encoding: null}, function (err, res, body) { + if (err) { + return callback(err); + } + + if (body && res.statusCode === 200) { + return encoder(body, options); + } + }); + } + }; var base64decoder = function (imageBuffer, options, callback) { @@ -36,7 +54,9 @@ var base64decoder = function (imageBuffer, options, callback) { if (options && options.filename) { fs.writeFile(options.filename + '.jpg', imageBuffer, 'base64', function (err) { - if (err) { return callback(err); } + if (err) { + return callback(err); + } return callback(null, 'Image saved successfully to disk!'); }); } diff --git a/test/tests.js b/test/tests.js index 5871ec6..dab621a 100644 --- a/test/tests.js +++ b/test/tests.js @@ -63,6 +63,39 @@ describe('Image download and encode/decode to Base64', function () { done(); }); }); + + it('should use a local image and return base64 encoded Buffer', function (done) { + this.timeout(15000); + + var path = __dirname + '/../test.jpg', + options = {localFile: true}; + + base64Image.base64encoder(path, options, function (err, image) { + should.not.exist(err); + + image.should.exist; + image.should.be.an.instanceOf(Buffer); + + done(); + }); + }); + + it('should use a local image and return base64 encoded string', function (done) { + this.timeout(15000); + + var path = __dirname + '/../test.jpg', + options = {localFile: true, string: true}; + + base64Image.base64encoder(path, options, function (err, image) { + should.not.exist(err); + + image.should.exist; + image.should.match(/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/); + + done(); + }); + }); + }); describe('Base64 Decoder', function () {