Tuesday, December 18, 2012

Javascript mimetype detector - Tool Tuesday

Warning: Shameless self-promotion.
As I noted in my post about the proliferation of app formats, HTML5/Javascript are becoming more and more viable languages to build desktop applications in, because they natively work on phones, tablets, and desktops; as well as nearly all types of operating systems without os specific instructions.
One of the things I recently needed for a project was a way to detect the mime of user uploaded files, but I didn't want to use the builtin browser detection; as this system would have to work on file names that had also been saved as binary blobs in a database.
I came up with this solution.

Usage

Get a list of mimes by extension.
jsmime.getMimesByExt("exe") --> ["application/x-msdos-program"]
Get a list of mimes by path.
jsmime.getMimesByPath("C:\Some\Path\File.exe") --> ["application/x-msdos-program"]
Check to see if a path (or extension) is of the type given.
jsmime.pathIsMime("C:\Some\Path\File.exe", "application/x-msdos-program") --> true
The default returned for unknown extensions is:
["application/octet-stream", "text/plain"]

No comments:

Post a Comment