-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADDED: - Download a given remote URL and return a new Buffer or a string - Tests
- Loading branch information
1 parent
0de7374
commit ba1381e
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language: node_js | ||
node_js: | ||
- "0.11" | ||
- "0.10" | ||
notifications: | ||
email: | ||
on_success: always | ||
on_failure: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
var request = require('request'); | ||
|
||
var base64encoder = function (url, options, callback) { | ||
|
||
options = options || {}; | ||
if (typeof callback !== 'function') { | ||
throw new Error('Callback needs to be a function!'); | ||
} | ||
|
||
if (url === undefined || url === null) { | ||
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); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
base64encoder: base64encoder | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "nkde-base64-image", | ||
"version": "0.0.1", | ||
"description": "Download images from remote URLs and encode/decode them to base64", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test/tests.js" | ||
}, | ||
"engines": { | ||
"node": ">=0.10" | ||
}, | ||
"repository": "https://github.com/riyadhalnur/node-base64-image", | ||
"homepage": "http://riyadhalnur.github.io/node-base64-image/", | ||
"keywords": [ | ||
"image", | ||
"download", | ||
"base64", | ||
"encode", | ||
"decode", | ||
"javascript", | ||
"node" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/riyadhalnur/node-base64-image/issuess" | ||
}, | ||
"author": "Riyadh Al Nur <[email protected]> (http://blog.verticalaxisbd.com)", | ||
"license": "GPLv3", | ||
"dependencies": { | ||
"request": "^2.51.x" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.0.x", | ||
"should": "^4.3.x" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
var should = require('should'); | ||
var base64Image = require('../index'); | ||
|
||
describe('Image download and encode/decode to Base64', function () { | ||
describe('Base64 encoder', function () { | ||
it('should throw an error if callback is not a function', function (done) { | ||
var url, options, callback; | ||
|
||
var helperFunc = function () { | ||
base64Image.base64encoder(url, options, callback); | ||
}; | ||
|
||
helperFunc.should.throw(Error); | ||
helperFunc.should.throw('Callback needs to be a function!'); | ||
|
||
done(); | ||
}); | ||
|
||
it('should throw an error if url is null or undefined', function (done) { | ||
var url = undefined, options; | ||
|
||
var helperFunc = function () { | ||
base64Image.base64encoder(url, options, function (err, image) { | ||
err.should.exist; | ||
image.should.not.exist; | ||
}); | ||
}; | ||
|
||
helperFunc.should.throw(Error); | ||
helperFunc.should.throw('URL cannot be empty!'); | ||
|
||
done(); | ||
}); | ||
|
||
it('should download an image and return base64 encoded Buffer', function (done) { | ||
this.timeout(15000); | ||
|
||
var url = 'http://www.pctools.com/security-news/wp-content/uploads/2011/09/code.jpg', options = {}; | ||
|
||
base64Image.base64encoder(url, options, function (err, image) { | ||
should.not.exist(err); | ||
|
||
image.should.exist; | ||
image.should.be.an.instanceOf(Buffer); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('should download an image and return base64 encoded string', function (done) { | ||
this.timeout(15000); | ||
|
||
var url = 'http://www.pctools.com/security-news/wp-content/uploads/2011/09/code.jpg', options = {string: true}; | ||
|
||
base64Image.base64encoder(url, 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(); | ||
}); | ||
}); | ||
}); | ||
}); |