Skip to content

Commit

Permalink
add support for promises; closes douzi8#6
Browse files Browse the repository at this point in the history
  • Loading branch information
e-e committed Jan 13, 2018
1 parent 44cd228 commit 6272372
Show file tree
Hide file tree
Showing 2 changed files with 620 additions and 9 deletions.
70 changes: 61 additions & 9 deletions base64-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var request = require('ajax-request');
function base64(filename, data) {
var extname = path.extname(filename).substr(1);
extname = extname || 'png';

return 'data:image/' + extname + ';base64,' + data.toString('base64');
}

Expand All @@ -28,16 +28,56 @@ function img(data) {
};
}

function base64Promise(filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, function (err, data) {
if (err) reject(err);

resolve(base64(filename, data));
});
});
};

function requestBase64Promise(url) {
return new Promise((resolve, reject) => {
request({
url: url,
isBuffer: true
}, function (err, res, body) {
if (err) return reject(err);

var data = 'data:' + res.headers['content-type'] + ';base64,' + body.toString('base64');
resolve({ res, data });
});
});
};

function imgPromise(data, destpath, name) {
return new Promise((resolve, reject) => {
var result = img(data);
var filepath = path.join(destpath, name + result.extname);

fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function (err) {
if (err) return reject(err);
resolve(filepath);
});
});
};

/**
* @description
* Get image file base64 data
* @example
* base64Img.base64('path/demo.png', function(err, data) {})
* @example
* base64Img.base64('path/demo.png').then(function(data) {
*
* });
*/
exports.base64 = function(filename, callback) {
if (!callback) callback = util.noop;
exports.base64 = function (filename, callback) {
if (!callback) return base64Promise(filename);

fs.readFile(filename, function(err, data) {
fs.readFile(filename, function (err, data) {
if (err) return callback(err);

callback(null, base64(filename, data));
Expand All @@ -50,7 +90,7 @@ exports.base64 = function(filename, callback) {
* @example
* var data = base64Img.base64Sync('path/demo.png');
*/
exports.base64Sync = function(filename) {
exports.base64Sync = function (filename) {
var data = fs.readFileSync(filename);

return base64(filename, data);
Expand All @@ -66,8 +106,16 @@ exports.base64Sync = function(filename) {
*
* }
* );
* @example
* request.base64(
* 'http://webresource.c-ctrip.com/ResCRMOnline/R5/html5/images/57.png',
* ).then(function({res, data}) {
*
* });
*/
exports.requestBase64 = function(url, callback) {
exports.requestBase64 = function (url, callback) {
if (!callback) return requestBase64Promise(url);

request({
url: url,
isBuffer: true
Expand All @@ -84,12 +132,16 @@ exports.requestBase64 = function(url, callback) {
* Convert image base64 data to img
* @example
* base64Img.img('data:image/png;base64,...', 'dest', '1', function(err, filepath) {});
* @example
* base64Img.img('data:image/png;base64,...', 'dest', '1').then(function(filepath) {});
*/
exports.img = function(data, destpath, name, callback) {
exports.img = function (data, destpath, name, callback) {
if (!callback) return imgPromise(data, destpath, name);

var result = img(data);
var filepath = path.join(destpath, name + result.extname);

fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function(err) {
fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function (err) {
callback(err, filepath);
});
};
Expand All @@ -100,7 +152,7 @@ exports.img = function(data, destpath, name, callback) {
* @example
* var filepath = base64Img.imgSync('data:image/png;base64,...', 'dest', '1');
*/
exports.imgSync = function(data, destpath, name) {
exports.imgSync = function (data, destpath, name) {
var result = img(data);
var filepath = path.join(destpath, name + result.extname);

Expand Down
Loading

0 comments on commit 6272372

Please sign in to comment.