Skip to content

Instantly share code, notes, and snippets.

@hekike
Last active December 27, 2015 07:29
Show Gist options
  • Select an option

  • Save hekike/7289302 to your computer and use it in GitHub Desktop.

Select an option

Save hekike/7289302 to your computer and use it in GitHub Desktop.
How to detect the filetype of the stream in NodeJS.
var
beginBuff,
beginBuffStr;
// type detection
if (chunkNum === 0) {
beginBuff = data.slice(0, 8);
beginBuffStr = beginBuff.toString('hex', 0, beginBuff.length);
// jpg
if (beginBuffStr.indexOf('ffd8ffe0') > -1) {
console.log('JPG');
}
// xml
else if (beginBuffStr.indexOf('3c3f786d6c207665') > -1) {
console.log('XML');
}
...
}
chunkNum += 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment