JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 681
  • Score
    100M100P100Q98935F

Proper MIME type detection library that wraps the libmagic functionality

Package Exports

  • mime-magic

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mime-magic) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

About still maintained

MIME type detection library for node.js. Unlike the existing mime module, mime-magic does not return the type by interpreting the file extension. Instead it uses the libmagic(3) library which does it properly.

Currently it provides just a simple file(1) wrapper to get the things moving, but in the long run, the purpose of this module is to provide proper node.js libmagic bindings. The file(1) source tree is provided along with this package. It is built during the installation process. The module aims to use the latest available file version along with the up-to-date magic database.

The Windows version of file(1) is bundled with the package. It is a native binary build with MinGW and compressed with UPX.

Installation

Either manually clone this repository into your node_modules directory, run make build (under unices), or the recommended method:

npm install mime-magic

Usage mode

var mime = require('mime-magic');

mime.fileWrapper('/path/to/foo.pdf', function (err, type) {
    if (err) {
        console.error(err.message);
        // ERROR: cannot open `/path/to/foo.pdf' (No such file or directory)
    } else {
        console.log('Detected mime type: %s', type);
        // application/pdf
    }
});

You may use an array of paths. The callback gets an array of mimes:

var files = [
    '/path/to/foo.pdf',
    '/path/to/foo.txt'
];

mime.fileWrapper(files, function (err, types) {
    if (err) {
        console.error(err.message);
        // ERROR: cannot open `/path/to/foo.pdf' (No such file or directory)
        // ERROR: cannot open `/path/to/foo.txt' (No such file or directory)
    } else {
        console.log(types);
        // ['application/pdf', 'text/plain']
    }
});

Under Windows, you must escape the backslash separators of the path argument:

mime.fileWrapper('C:\\path\\to\\foo.pdf', function (err, type) {
    // do something
});

You may also pass a path that uses forward slashes as separators:

mime.fileWrapper('C:/path/to/foo.pdf', function (err, type) {
    // do something
});

Passing relative paths is supported. The fileWrapper uses child_process.execFile() behind the scenes, therefore the err argument contains the information returned by the execFile() method itself plus the error message returned by file(1).

Notices

The module was developed under Ubuntu 10.04 and Windows 7. It was tested under OS X Snow Leopard and FreeBSD 9.0. Other platforms may be supported, but the behavior is untested.

The Windows binaries are built by myself under Windows 7 / MinGW + MSYS. The binaries are packed with the UPX tool in order to make them smaller.

Here's the virustotal.com analysis:

Please notice that for the unpacked binary there are a couple of false positives, while for the packed binary, there are four of them.

Contributors

  • Felix Chan - #1: couldn't use fileWrapper more than once unless restarted server
  • eddyb - #3: support for arrays of paths, with the callback getting an array of mime-types.