Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  116] [ 3]  / answers: 1 / hits: 22260  / 5 Years ago, thu, august 8, 2019, 12:00:00

I have created a buffer from a file which can be pdf, jpg or any other format. Now I want to detect if the buffer is of the pdf file or any other file.



request({ url, encoding: null }, (err, resp, buffer) => {
hashFromFilebuffer('sha256', buffer).then(function (result) {
console.log(result)
}).catch(function (error) {
console.log(error)
});
});

More From » node.js

 Answers
73

Have a look at this: https://github.com/sindresorhus/file-type/
.
If you want to know how it works, I think the code is at https://github.com/sindresorhus/file-type/blob/master/index.js



From the code in the source, it appears that the file is a pdf if the first bytes are
[0x25, 0x50, 0x44, 0x46], and is a jpg if the first bytes are [0xFF, 0xD8, 0xFF]


[#51793] Tuesday, July 30, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Sat, Aug 15, 20, 00:00, 4 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
;