diff --git a/lib/exec.js b/lib/exec.js index 66537a9..f49dd72 100644 --- a/lib/exec.js +++ b/lib/exec.js @@ -15,22 +15,37 @@ module.exports = (function () { mimer = require('mimer'), uri = require('./uri'), css = require('./css'), - existsSync = fs.existsSync || path.existsSync; + existsSync = fs.existsSync || path.existsSync, + exists = fs.exists || path.exists; var DataURI = function (fileName) { if (!(this instanceof DataURI)) { - if (fileName) { - var dUri = new DataURI(); - - return dUri.createConfig(fileName); - } - return new DataURI(); - } - - if (fileName) { + return new DataURI(fileName); + } else if (fileName) { return this.createConfig(fileName); } }; + + DataURI.async = function(fileName, callback) { + DataURI.asyncURI(fileName, function(err, uri) { + if(err) return callback(err); + callback(null, uri.content); + }); + }; + + DataURI.asyncURI = function(fileName, callback) { + exists(fileName, function(isExisting) { + fs.readFile(fileName, 'base64', function(err, data) { + if(err) return callback(err); + var uri = new DataURI(); + uri.fileName = fileName; + uri.base64 = data; + uri.mimetype = mimer(fileName); + uri.content = uri(uri); + callback(null, uri); + }); + }); + } DataURI.prototype.createConfig = function (fileName) {