An async libmagic binding for node.js for detecting content types by data inspection.
npm install mmmagic
Get general description of a file:
var Magic = require('mmmagic').Magic;
var magic = new Magic();
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
console.log(result);
// output on Windows with 32-bit node:
// PE32 executable (DLL) (GUI) Intel 80386, for MS Windows
});
Get mime type for a file:
var mmm = require('mmmagic'),
Magic = mmm.Magic;
var magic = new Magic(mmm.MAGIC_MIME_TYPE);
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
console.log(result);
// output on Windows with 32-bit node:
// application/x-dosexec
});
Get mime type and mime encoding for a file:
var mmm = require('mmmagic'),
Magic = mmm.Magic;
var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING);
// the above flags can also be shortened down to just: mmm.MAGIC_MIME
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
console.log(result);
// output on Windows with 32-bit node:
// application/x-dosexec; charset=binary
});
Get general description of the contents of a Buffer:
var Magic = require('mmmagic').Magic;
var magic = new Magic(),
buf = new Buffer('import Options\nfrom os import unlink, symlink');
magic.detect(buf, function(err, result) {
if (err) throw err;
console.log(result);
// output: Python script, ASCII text executable
});
(constructor)([< mixed >magicSource][, < Integer >flags]) - Creates and returns a new Magic instance. magicSource
(if specified) can either be a path string that points to a (compatible) magic file to use or it can be a Buffer containing the contents of a (compatible) magic file. If magicSource
is not a string and not false
, the bundled magic file will be used. If magicSource
is false
, mmmagic will default to searching for a magic file to use (order of magic file searching: MAGIC
env var -> various file system paths (see man file
)). flags is a bitmask with the following valid values (available as constants on require('mmmagic')
):
detectFile(< String >path, < Function >callback) - (void) - Inspects the file pointed at by path. The callback receives two arguments: an < Error > object in case of error (null otherwise), and a < String > containing the result of the inspection.
detect(< Buffer >data, < Function >callback) - (void) - Inspects the contents of data. The callback receives two arguments: an < Error > object in case of error (null otherwise), and a < String > containing the result of the inspection.