detect-file-type
Advanced tools
Comparing version 0.0.15 to 0.1.0
16
index.js
@@ -21,2 +21,4 @@ 'use strict'; | ||
var customFunctions = []; | ||
exports.default = { | ||
@@ -86,2 +88,13 @@ fromFile: function fromFile(filePath, bufferLength, callback) { | ||
if (result === null) { | ||
customFunctions.every(function (fn) { | ||
var fnResult = fn(buffer); | ||
if (fnResult) { | ||
result = fnResult; | ||
return false; | ||
}; | ||
return true; | ||
}); | ||
} | ||
callback(null, result); | ||
@@ -280,2 +293,5 @@ }, | ||
}, | ||
addCustomFunction: function addCustomFunction(fn) { | ||
customFunctions.push(fn); | ||
}, | ||
getFileSize: function getFileSize(filePath, callback) { | ||
@@ -282,0 +298,0 @@ _fs2.default.stat(filePath, function (err, stat) { |
{ | ||
"name": "detect-file-type", | ||
"version": "0.0.15", | ||
"version": "0.1.0", | ||
"description": "Detect file type by signature", | ||
@@ -35,4 +35,5 @@ "main": "index.js", | ||
"chai": "^3.5.0", | ||
"is-html": "^1.0.0", | ||
"mocha": "^2.5.3" | ||
} | ||
} |
@@ -46,2 +46,37 @@ # detect-file-type [![Build Status](https://travis-ci.org/dimapaloskin/detect-file-type.svg?branch=master)](https://travis-ci.org/dimapaloskin/detect-file-type) | ||
### addCustomFunction(fn) | ||
Add custom function which receive buffer and trying to detect file type. | ||
- `fn` - function which receive buffer | ||
This method needed for more complicated cases like html or xml for example. Truly uncomfortable to detect html via signatures because html format has a lot of "magic numbers" in the different places. So you can install [is-html](https://www.npmjs.com/package/is-html) package for example and use its functionality. | ||
```js | ||
const detect = require('detect-file-type'); | ||
const isHtml = require('is-html'); | ||
detect.addCustomFunction((buffer) => { | ||
const str = buffer.toString(); | ||
if (isHtml(str)) { | ||
return { | ||
ext: 'html', | ||
mime: 'text/html' | ||
} | ||
} | ||
return false; | ||
}); | ||
detect.fromFile('./some.html', (err, result) => { | ||
if (err) { | ||
return console.log(err); | ||
} | ||
console.log(result); // { ext: 'html', mime: 'text/html' } | ||
}); | ||
``` | ||
**Note**: custom function should be pure (without any async operations) | ||
## Signature and creating your own signatures | ||
@@ -48,0 +83,0 @@ Detecting of file type work via signatures. |
import fs from 'fs'; | ||
import signatures from './signatures.json'; | ||
const customFunctions = []; | ||
export default { | ||
@@ -69,2 +71,13 @@ | ||
if (result === null) { | ||
customFunctions.every((fn) => { | ||
const fnResult = fn(buffer); | ||
if (fnResult) { | ||
result = fnResult; | ||
return false; | ||
}; | ||
return true; | ||
}); | ||
} | ||
callback(null, result); | ||
@@ -257,2 +270,6 @@ }, | ||
addCustomFunction(fn) { | ||
customFunctions.push(fn); | ||
}, | ||
getFileSize(filePath, callback) { | ||
@@ -259,0 +276,0 @@ fs.stat(filePath, (err, stat) => { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45312
1087
151
7