Package Exports
- detect-file-type
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 (detect-file-type) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
detect-file-type
Detect file type by signatures. file-type inspired
Supported types (will be updated)
jpg, png, gif, bmp, webp, tif, cr2, jxr, psd, zip, epub, xpi, tar
Installation
npm i detect-file-type
Usage
var detect = require('detect-file-type');
detect.fromFile('./image.jpg', function(err, result) {
if (err) {
return console.log(err);
}
console.log(result); // { ext: 'jpg', mime: 'image/jpeg' }
});
API
fromFile(filePath, bufferLength, callback)
Detect file type from hard disk
filePath
- path to filebufferLength
- (optional) Buffer size (in bytes) starting from the start of file. By default 500. If size of file less than 500Kb then param the same as size of the filecallback
fromBuffer(buffer, callback)
Detect file type from buffer
buffer
- uint8arraycallback
addSignature(siganture)
Add new signature for file type detecting
signature
- a signature. See section about it below
Signature and creating your own signatures
Detecting of file type work via signatures. The simplest signature in JSON format looks like:
{
"type": "jpg",
"ext": "jpg",
"mime": "image/jpeg",
"rules": [
{ "type": "equal", "start": 0, "end": 2, "bytes": "ffd8" }
]
}
params:
type
- signature type, mostly it's the same as param 'ext'ext
- file extensionmime
- mime type of filerules
- list of rules for detecting
More details about param rules
:
This param have to be array of objects
type
- a rule type. There are available a few types:equal
,notEqual
,contains
,notContains
,or
,and
More details about: equal, notEqual, contains и notContains.
equal
- here is required fieldbytes
. We get a dump of buffer fromstart
(equals 0 by default) toend
(equals buffer.length by default). After that we compare the dump with value in parambytes
. If values are equal then this rule is correct.notEqual
- here is required fieldbytes
. We get a dump of buffer fromstart
(equals 0 by default) toend
(equals buffer.length by default). After that we compare the dump with value in parambytes
. If values aren't equal then this rule is correct.contains
- here is required fieldbytes
. We get a dump of buffer fromstart
(equals 0 by default) toend
(equals buffer.length by default). After that we try to find the sequence frombytes
in this dump. If the dump containsbytes
then rules is correct.notContains
- here is required fieldbytes
. We get a dump of buffer fromstart
(equals 0 by default) toend
(equals buffer.length by default). After that we try to find the sequence frombytes
in this dump. If the dump containsbytes
then rules is correct.
More details about the rule types or
and and
Actually, these types are necessary when you work with complicated signatures. For example, when file contains few sequences of bytes in different parts of file. Here is required field 'rules', where you should define set of another rules. See example:
{
"type": "tif",
"ext": "tif",
"mime": "image/tiff",
"rules": [
{ "type": "and", "rules":
[
{ "type": "notEqual", "start": 8, "end": 10, "bytes": "4352" },
{ "type": "or", "rules":
[
{ "type": "equal", "start": 0, "end": 4, "bytes": "49492a00" },
{ "type": "equal", "start": 0, "end": 4, "bytes": "4d4d002a" }
]
}
]
}
]
}
Explanation: If dump starts from 8th byte and ends to 10th byte isn't equal "4352", and dump starts from 0 and ends to 4th byte is equal "49492a00" or is equal "4d4d002a" then data looks like file with 'tif' format.
or
- means that any rules fromrules
should be correct. If at least 1 rule is correct then list are correct too.and
- means that each rule fromrules
should be correct. If all rules are correct then list is correct. When at least 1 rule fail then all list is incorrect.
The rules or
and and
can be nested without restrictions.
License
WTFPL © Dmitry Pavlovsky