file-type
Advanced tools
Comparing version 8.1.0 to 9.0.0
90
index.js
@@ -154,37 +154,50 @@ 'use strict'; | ||
// https://github.com/file/file/blob/master/magic/Magdir/msooxml | ||
if (check(oxmlContentTypes, {offset: 30}) || check(oxmlRels, {offset: 30})) { | ||
const sliced = buf.subarray(4, 4 + 2000); | ||
const nextZipHeaderIndex = arr => arr.findIndex((el, i, arr) => arr[i] === 0x50 && arr[i + 1] === 0x4B && arr[i + 2] === 0x3 && arr[i + 3] === 0x4); | ||
const header2Pos = nextZipHeaderIndex(sliced); | ||
// The docx, xlsx and pptx file types extend the Office Open XML file format: | ||
// https://en.wikipedia.org/wiki/Office_Open_XML_file_formats | ||
// We look for: | ||
// - one entry named '[Content_Types].xml' or '_rels/.rels', | ||
// - one entry indicating specific type of file. | ||
// MS Office, OpenOffice and LibreOffice may put the parts in different order, so the check should not rely on it. | ||
const findNextZipHeaderIndex = (arr, startAt = 0) => arr.findIndex((el, i, arr) => i >= startAt && arr[i] === 0x50 && arr[i + 1] === 0x4B && arr[i + 2] === 0x3 && arr[i + 3] === 0x4); | ||
if (header2Pos !== -1) { | ||
const slicedAgain = buf.subarray(header2Pos + 8, header2Pos + 8 + 1000); | ||
const header3Pos = nextZipHeaderIndex(slicedAgain); | ||
let zipHeaderIndex = 0; // The first zip header was already found at index 0 | ||
let oxmlFound = false; | ||
let type = null; | ||
if (header3Pos !== -1) { | ||
const offset = 8 + header2Pos + header3Pos + 30; | ||
do { | ||
const offset = zipHeaderIndex + 30; | ||
if (checkString('word/', {offset})) { | ||
return { | ||
ext: 'docx', | ||
mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' | ||
}; | ||
} | ||
if (!oxmlFound) { | ||
oxmlFound = (check(oxmlContentTypes, {offset}) || check(oxmlRels, {offset})); | ||
} | ||
if (checkString('ppt/', {offset})) { | ||
return { | ||
ext: 'pptx', | ||
mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' | ||
}; | ||
} | ||
if (checkString('xl/', {offset})) { | ||
return { | ||
ext: 'xlsx', | ||
mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | ||
}; | ||
} | ||
if (!type) { | ||
if (checkString('word/', {offset})) { | ||
type = { | ||
ext: 'docx', | ||
mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' | ||
}; | ||
} else if (checkString('ppt/', {offset})) { | ||
type = { | ||
ext: 'pptx', | ||
mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' | ||
}; | ||
} else if (checkString('xl/', {offset})) { | ||
type = { | ||
ext: 'xlsx', | ||
mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | ||
}; | ||
} | ||
} | ||
if (oxmlFound && type) { | ||
return type; | ||
} | ||
zipHeaderIndex = findNextZipHeaderIndex(buf, offset); | ||
} while (zipHeaderIndex >= 0); | ||
// No more zip parts available in the buffer, but maybe we are almost certain about the type? | ||
if (type) { | ||
return type; | ||
} | ||
@@ -316,3 +329,3 @@ } | ||
ext: 'avi', | ||
mime: 'video/x-msvideo' | ||
mime: 'video/vnd.avi' | ||
}; | ||
@@ -323,3 +336,3 @@ } | ||
ext: 'wav', | ||
mime: 'audio/x-wav' | ||
mime: 'audio/vnd.wave' | ||
}; | ||
@@ -404,5 +417,5 @@ } | ||
) { | ||
return { | ||
return { // MPEG-4 layer 3 (audio) | ||
ext: 'm4a', | ||
mime: 'audio/m4a' | ||
mime: 'audio/mp4' // RFC 4337 | ||
}; | ||
@@ -475,3 +488,3 @@ } | ||
if (check([0x4D, 0x41, 0x43, 0x20])) { | ||
if (check([0x4D, 0x41, 0x43, 0x20])) { // 'MAC ' | ||
return { | ||
@@ -483,2 +496,9 @@ ext: 'ape', | ||
if (check([0x77, 0x76, 0x70, 0x6B])) { // 'wvpk' | ||
return { | ||
ext: 'wv', | ||
mime: 'audio/wavpack' | ||
}; | ||
} | ||
if (check([0x23, 0x21, 0x41, 0x4D, 0x52, 0x0A])) { | ||
@@ -565,3 +585,3 @@ return { | ||
ext: 'eot', | ||
mime: 'application/octet-stream' | ||
mime: 'application/vnd.ms-fontobject' | ||
}; | ||
@@ -568,0 +588,0 @@ } |
{ | ||
"name": "file-type", | ||
"version": "8.1.0", | ||
"version": "9.0.0", | ||
"description": "Detect the file type of a Buffer/Uint8Array", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -79,3 +79,3 @@ # file-type [](https://travis-ci.org/sindresorhus/file-type) | ||
It only needs the first 4100 bytes. | ||
It only needs the first 4100 bytes. The exception is detection of `docx`, `pptx`, and `xlsx` which potentially requires reading the whole file. | ||
@@ -168,2 +168,3 @@ | ||
- [`ape`](https://en.wikipedia.org/wiki/Monkey%27s_Audio) - Monkey's Audio | ||
- [`wv`](https://en.wikipedia.org/wiki/WavPack) - WavPack | ||
@@ -170,0 +171,0 @@ *SVG isn't included as it requires the whole file to be read, but you can get it [here](https://github.com/sindresorhus/is-svg).* |
26340
724
188