mimetics
Version:
Identifies file types based on magic bytes, patterns, and other unique file attributes.
556 lines (554 loc) • 12.1 kB
JavaScript
const MAGIC_BYTES = {
ZIP: [
[0x50, 0x4b, 0x03, 0x04],
[0x50, 0x4b, 0x05, 0x06],
[0x50, 0x4b, 0x07, 0x08],
],
OFFICE: [0xd0, 0xcf, 0x11, 0xe0],
}
module.exports = [
// Image Types
{
id: Symbol('jpeg'),
tag: 'image', type: 'jpeg',
ext: ['jpg', 'jpeg'],
mime: 'image/jpeg',
magic: [0xff, 0xd8, 0xff],
pattern: null,
},
{
id: Symbol('png'),
tag: 'image', type: 'png',
ext: 'png',
mime: 'image/png',
magic: [0x89, 0x50, 0x4e, 0x47],
pattern: null,
},
{
id: Symbol('gif87a'),
tag: 'image', type: 'gif87a',
ext: 'gif',
mime: 'image/gif',
magic: [0x47, 0x49, 0x46, 0x38, 0x37, 0x61], // GIF87a
pattern: null,
},
{
id: Symbol('gif89a'),
tag: 'image', type: 'gif89a',
ext: 'gif',
mime: 'image/gif',
magic: [0x47, 0x49, 0x46, 0x38, 0x39, 0x61], // GIF89a
pattern: null,
},
{
id: Symbol('tif'),
tag: 'image', type: 'tif',
ext: ['tif', 'tiff'],
mime: 'image/tiff',
magic: [0x49, 0x49, 0x2a, 0x00], // Little endian
pattern: null,
},
{
id: Symbol('tif'),
tag: 'image', type: 'tif',
ext: ['tif', 'tiff'],
mime: 'image/tiff',
magic: [0x4d, 0x4d, 0x00, 0x2a], // Big endian
pattern: null,
},
{
id: Symbol('bitmap'),
tag: 'image', type: 'bitmap',
ext: 'bmp',
mime: 'image/bmp',
magic: [0x42, 0x4d],
pattern: null,
},
{
id: Symbol('icon'),
tag: 'image', type: 'icon',
ext: 'ico',
mime: 'image/x-icon',
magic: [0x00, 0x00, 0x01, 0x00],
pattern: null,
},
{
id: Symbol('webp'),
tag: 'image', type: 'webp',
ext: 'webp',
mime: 'image/webp',
magic: [0x52, 0x49, 0x46, 0x46, 0x57, 0x45, 0x42, 0x50], // RIFFWEBP
pattern: null,
},
{
id: Symbol('pdf'),
tag: 'image', type: 'pdf',
ext: 'pdf',
mime: 'application/pdf',
magic: [0x25, 0x50, 0x44, 0x46],
pattern: null,
},
{
id: Symbol('svg'),
tag: 'image', type: 'svg',
ext: 'svg',
mime: 'image/svg+xml',
magic: null,
pattern: /^\s*<\s*svg[^>]*>/i,
},
// Video Types
{
id: Symbol('mp4'),
tag: 'video', type: 'mp4',
ext: 'mp4',
mime: 'video/mp4',
magic: [0x00, 0x00, 0x00, 0x1c, 0x66, 0x74, 0x79, 0x70],
pattern: null,
},
{
id: Symbol('quicktime'),
tag: 'video', type: 'quicktime',
ext: 'mov',
mime: 'video/quicktime',
magic: [0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70],
pattern: null,
},
{
id: Symbol('avi'),
tag: 'video', type: 'avi',
ext: 'avi',
mime: 'video/x-msvideo',
magic: [0x52, 0x49, 0x46, 0x46, 0x41, 0x56, 0x49, 0x20], // RIFFAVI
pattern: null,
},
{
id: Symbol('mkv'),
tag: 'video', type: 'mkv',
ext: 'mkv',
mime: 'video/x-matroska',
magic: [0x1a, 0x45, 0xdf, 0xa3],
pattern: null,
},
{
id: Symbol('webm'),
tag: 'video', type: 'webm',
ext: 'webm',
mime: 'video/webm',
magic: [0x1a, 0x45, 0xdf, 0xa3], // Same as MKV
pattern: null,
},
{
id: Symbol('flv'),
tag: 'video', type: 'flv',
ext: 'flv',
mime: 'video/x-flv',
magic: [0x46, 0x4c, 0x56, 0x01],
pattern: null,
},
// Audio Types
{
id: Symbol('mp3'),
tag: 'audio', type: 'mp3',
ext: 'mp3',
mime: 'audio/mpeg',
magic: [
[0x49, 0x44, 0x33],
[0xff, 0xfb],
[0xff, 0xf3],
[0xff, 0xf2],
],
pattern: null,
},
{
id: Symbol('ogg'),
tag: 'audio', type: 'ogg',
ext: 'ogg',
mime: 'audio/ogg',
magic: [0x4F, 0x67, 0x67, 0x53], // OggS
pattern: null,
},
{
id: Symbol('wav'),
tag: 'audio', type: 'wav',
ext: 'wav',
mime: 'audio/wav',
magic: [0x52, 0x49, 0x46, 0x46, '*', '*', '*', '*', 0x57, 0x41, 0x56, 0x45],
pattern: null,
},
// Compressed Types
{
id: Symbol('zip'),
tag: 'compressed', type: 'zip',
ext: ['zip', 'epub'],
mime: 'application/zip',
zipped: true,
magic: MAGIC_BYTES.ZIP,
pattern: null,
},
{
id: Symbol('rar'),
tag: 'compressed', type: 'rar',
ext: 'rar',
mime: 'application/x-rar-compressed',
magic: [0x52, 0x61, 0x72, 0x21],
pattern: null,
},
{
id: Symbol('gzip'),
tag: 'compressed', type: 'gzip',
ext: 'gz',
mime: 'application/gzip',
magic: [0x1f, 0x8b],
pattern: null,
},
{
id: Symbol('7zip'),
tag: 'compressed', type: '7zip',
ext: '7z',
mime: 'application/x-7z-compressed',
magic: [0x37, 0x7a, 0xbc, 0xaf],
pattern: null,
},
{
id: Symbol('tar'),
tag: 'compressed', type: 'tar',
ext: 'tar',
mime: 'application/x-tar',
magic: null, // TAR files can be identified by their file structure
pattern: null,
},
// Microsoft Office Types
{
id: Symbol('word'),
tag: 'office', type: 'word',
ext: 'doc',
mime: 'application/vnd.ms-office',
magic: MAGIC_BYTES.OFFICE,
pattern: null,
},
{
id: Symbol('excel'),
tag: 'office', type: 'excel',
ext: 'xls',
mime: 'application/vnd.ms-office',
magic: MAGIC_BYTES.OFFICE,
pattern: null,
},
{
id: Symbol('powerpoint'),
tag: 'office', type: 'powerpoint',
ext: 'ppt',
mime: 'application/vnd.ms-office',
magic: MAGIC_BYTES.OFFICE,
pattern: null,
},
{
id: Symbol('word'),
tag: 'office', type: 'word',
ext: 'docx',
mime: 'application/vnd.openxmlformats-officedocument',
zipped: [`[Content_Types].xml`, `word/document.xml`],
magic: MAGIC_BYTES.ZIP,
pattern: null,
},
{
id: Symbol('excel'),
tag: 'office', type: 'excel',
ext: 'xlsx',
mime: 'application/vnd.openxmlformats-officedocument',
zipped: [`[Content_Types].xml`, `xl/workbook.xml`],
magic: MAGIC_BYTES.ZIP,
pattern: null,
},
{
id: Symbol('powerpoint'),
tag: 'office', type: 'powerpoint',
ext: 'pptx',
mime: 'application/vnd.openxmlformats-officedocument',
zipped: [`[Content_Types].xml`, `ppt/presentation.xml`],
magic: MAGIC_BYTES.ZIP,
pattern: null,
},
// Code
{
id: Symbol('html'),
tag: 'code', type: 'html',
ext: ['html', 'htm'],
mime: 'text/html',
magic: null,
pattern: /^\s*(