Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ var Magic = require('../build/Release/magic');
var fbpath = require('path').join(__dirname, '..', 'magic', 'magic');
Magic.setFallback(fbpath);

Magic.Magic.prototype.detectFileAsync = function(path) {
return new Promise((resolve, reject) => {
this.detectFile(path, (err, result) => {
if (err) {
return reject(err)
}

resolve(result)
})
})
}

Magic.Magic.prototype.detectAsync = function(buffer) {
return new Promise((resolve, reject) => {
this.detect(buffer, (err, mimeType) => {
if (err) {
return reject(err)
}

resolve(mimeType)
})
})
}

module.exports = {
Magic: Magic.Magic,
MAGIC_NONE: 0x000000, /* No flags (default for Windows) */
Expand Down
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ var tests = [
},
what: 'detect - Normal operation, mime type'
},
{ run: function() {
var magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE);
magic.detectFileAsync(path.join(__dirname, '..', 'src', 'binding.cc'))
.then(result => {
assert.strictEqual(result, 'text/x-c++');
next();
});
},
what: 'detectFileAsync - Normal operation'
},
{ run: function() {
var magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE);
assert.rejects(magic.detectFileAsync('/no/such/path1234567')).then(next);
},
what: 'detectFileAsync - Error'
},
{ run: function() {
var buf = fs.readFileSync(path.join(__dirname, '..', 'src', 'binding.cc'));
var magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE);
magic.detectAsync(buf).then(result => {
assert.strictEqual(result, 'text/x-c++');
next();
});
},
what: 'detectAsync - Normal operation'
}
];

function next() {
Expand Down