从节点js中的缓冲区检测文件类型?
Detecting file type from buffer in node js?
我已经从一个文件创建了一个缓冲区,该文件可以是 pdf、jpg 或任何其他格式。现在我想检测缓冲区是 pdf 文件还是任何其他文件。
request({ url, encoding: null }, (err, resp, buffer) => {
hashFromFilebuffer('sha256', buffer).then(function (result) {
console.log(result)
}).catch(function (error) {
console.log(error)
});
});
看看这个:https://github.com/sindresorhus/file-type/
.
如果你想知道它是如何工作的,我想代码在https://github.com/sindresorhus/file-type/blob/master/index.js
从源代码来看,如果第一个字节是
[0x25, 0x50, 0x44, 0x46]
,如果第一个字节是 [0xFF, 0xD8, 0xFF]
,则为 jpg
我已经从一个文件创建了一个缓冲区,该文件可以是 pdf、jpg 或任何其他格式。现在我想检测缓冲区是 pdf 文件还是任何其他文件。
request({ url, encoding: null }, (err, resp, buffer) => {
hashFromFilebuffer('sha256', buffer).then(function (result) {
console.log(result)
}).catch(function (error) {
console.log(error)
});
});
看看这个:https://github.com/sindresorhus/file-type/ . 如果你想知道它是如何工作的,我想代码在https://github.com/sindresorhus/file-type/blob/master/index.js
从源代码来看,如果第一个字节是
[0x25, 0x50, 0x44, 0x46]
,如果第一个字节是 [0xFF, 0xD8, 0xFF]