Skip to content

Commit

Permalink
Merge pull request #1 from martinheidegger/patch-1
Browse files Browse the repository at this point in the history
Added async support & cleaned the constructor a little.
  • Loading branch information
heldr committed Sep 14, 2013
2 parents d564b53 + a49fae2 commit 578b777
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions lib/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down

0 comments on commit 578b777

Please sign in to comment.