Browse Source

Merge pull request #219 from transloadit/feature/file-types

[WIP] Better file type detection
Artur Paikin 7 years ago
parent
commit
5de51770c2

+ 1 - 0
.eslintignore

@@ -1,5 +1,6 @@
 dist/**
 lib/**
+src/vendor/**
 node_modules/**
 test/lib/**
 playground/**

+ 49 - 65
src/core/Core.js

@@ -144,59 +144,59 @@ class Uppy {
   }
 
   addFile (file) {
-    const updatedFiles = Object.assign({}, this.state.files)
-
-    const fileName = file.name || 'noname'
-    const fileType = Utils.getFileType(file)
-    const fileTypeGeneral = fileType[0]
-    const fileTypeSpecific = fileType[1]
-    const fileExtension = Utils.getFileNameAndExtension(fileName)[1]
-    const isRemote = file.isRemote || false
-
-    const fileID = Utils.generateFileID(fileName)
-
-    const newFile = {
-      source: file.source || '',
-      id: fileID,
-      name: fileName,
-      extension: fileExtension || '',
-      meta: {
-        name: fileName
-      },
-      type: {
-        general: fileTypeGeneral,
-        specific: fileTypeSpecific
-      },
-      data: file.data,
-      progress: {
-        percentage: 0,
-        uploadComplete: false,
-        uploadStarted: false
-      },
-      size: file.data.size || 'N/A',
-      isRemote: isRemote,
-      remote: file.remote || '',
-      preview: file.preview
-    }
+    Utils.getFileType(file).then((fileType) => {
+      const updatedFiles = Object.assign({}, this.state.files)
+      const fileName = file.name || 'noname'
+      const fileExtension = Utils.getFileNameAndExtension(fileName)[1]
+      const isRemote = file.isRemote || false
+
+      const fileID = Utils.generateFileID(fileName)
+      const fileTypeGeneral = fileType[0]
+      const fileTypeSpecific = fileType[1]
+
+      const newFile = {
+        source: file.source || '',
+        id: fileID,
+        name: fileName,
+        extension: fileExtension || '',
+        meta: {
+          name: fileName
+        },
+        type: {
+          general: fileTypeGeneral,
+          specific: fileTypeSpecific
+        },
+        data: file.data,
+        progress: {
+          percentage: 0,
+          uploadComplete: false,
+          uploadStarted: false
+        },
+        size: file.data.size || 'N/A',
+        isRemote: isRemote,
+        remote: file.remote || '',
+        preview: file.preview
+      }
 
-    if (fileTypeGeneral === 'image' && !isRemote) {
-      newFile.preview = Utils.getThumbnail(file)
-    }
+      if (Utils.isPreviewSupported(fileTypeSpecific) && !isRemote) {
+        newFile.preview = Utils.getThumbnail(file)
+      }
 
-    updatedFiles[fileID] = newFile
-    this.setState({files: updatedFiles})
+      updatedFiles[fileID] = newFile
+      this.setState({files: updatedFiles})
 
-    this.bus.emit('file-added', fileID)
-    this.log(`Added file: ${fileName}, ${fileID}, mime type: ${fileType}`)
+      this.bus.emit('file-added', fileID)
+      this.log(`Added file: ${fileName}, ${fileID}, mime type: ${fileType}`)
 
-    if (this.opts.autoProceed && !this.scheduledAutoProceed) {
-      this.scheduledAutoProceed = setTimeout(() => {
-        this.scheduledAutoProceed = null
-        this.upload().catch((err) => {
-          console.error(err.stack || err.message)
-        })
-      }, 4)
-    }
+      if (this.opts.autoProceed && !this.scheduledAutoProceed) {
+        this.scheduledAutoProceed = setTimeout(() => {
+          this.scheduledAutoProceed = null
+          this.upload().catch((err) => {
+            console.error(err.stack || err.message)
+          })
+        }, 4)
+      }
+    })
   }
 
   removeFile (fileID) {
@@ -254,14 +254,6 @@ class Uppy {
     this.setState({
       totalProgress: totalProgress
     })
-
-    // if (totalProgress === 100) {
-    //   const completeFiles = Object.keys(updatedFiles).filter((file) => {
-    //     // this should be `uploadComplete`
-    //     return updatedFiles[file].progress.percentage === 100
-    //   })
-    //   this.emit('core:success', completeFiles.length)
-    // }
   }
 
   /**
@@ -553,14 +545,6 @@ class Uppy {
     return this.socket
   }
 
-  // installAll () {
-  //   Object.keys(this.plugins).forEach((pluginType) => {
-  //     this.plugins[pluginType].forEach((plugin) => {
-  //       plugin.install(this)
-  //     })
-  //   })
-  // }
-
 /**
  * Initializes actions, installs all plugins (by iterating on them and calling `install`), sets options
  *

+ 72 - 51
src/core/Utils.js

@@ -1,6 +1,8 @@
 const throttle = require('lodash.throttle')
-// import mime from 'mime-types'
-// import pica from 'pica'
+// we inline file-type module, as opposed to using the NPM version,
+// because of this https://github.com/sindresorhus/file-type/issues/78
+// and https://github.com/sindresorhus/copy-text-to-clipboard/issues/5
+const fileType = require('../vendor/file-type')
 
 /**
  * A collection of small utility functions that help with dom manipulation, adding listeners,
@@ -147,9 +149,73 @@ function runPromiseSequence (functions, ...args) {
 //   return (!f && 'not a function') || (s && s[1] || 'anonymous')
 // }
 
+function isPreviewSupported (fileTypeSpecific) {
+  // list of images that browsers can preview
+  if (/^(jpeg|gif|png|svg|svg\+xml|bmp)$/.test(fileTypeSpecific)) {
+    return true
+  }
+  return false
+}
+
+function getArrayBuffer (chunk) {
+  return new Promise(function (resolve, reject) {
+    var reader = new FileReader()
+    reader.addEventListener('load', function (e) {
+      // e.target.result is an ArrayBuffer
+      resolve(e.target.result)
+    })
+    reader.addEventListener('error', function (err) {
+      console.error('FileReader error' + err)
+      reject(err)
+    })
+    // file-type only needs the first 4100 bytes
+    reader.readAsArrayBuffer(chunk)
+  })
+}
+
 function getFileType (file) {
-  return file.type ? file.type.split('/') : ['', '']
-  // return mime.lookup(file.name)
+  const emptyFileType = ['', '']
+  const extensionsToMime = {
+    'md': 'text/markdown',
+    'markdown': 'text/markdown',
+    'mp4': 'video/mp4',
+    'mp3': 'audio/mp3'
+  }
+
+  const fileExtension = getFileNameAndExtension(file.name)[1]
+
+  // 1. try to determine file type from magic bytes with file-type module
+  // this should be the most trustworthy way
+  const chunk = file.data.slice(0, 4100)
+  return getArrayBuffer(chunk)
+    .then((buffer) => {
+      const type = fileType(buffer)
+      if (type && type.mime) {
+        return type.mime.split('/')
+      }
+
+      // 2. if that’s no good, check if mime type is set in the file object
+      if (file.type) {
+        return file.type.split('/')
+      }
+
+      // 3. if that’s no good, see if we can map extension to a mime type
+      if (extensionsToMime[fileExtension]) {
+        return extensionsToMime[fileExtension].split('/')
+      }
+
+      // if all fails, well, return empty
+      return emptyFileType
+    })
+    .catch(() => {
+      return emptyFileType
+    })
+
+    // if (file.type) {
+    //   return Promise.resolve(file.type.split('/'))
+    // }
+    // return mime.lookup(file.name)
+    // return file.type ? file.type.split('/') : ['', '']
 }
 
 // TODO Check which types are actually supported in browsers. Chrome likes webm
@@ -268,33 +334,6 @@ function copyToClipboard (textToCopy, fallbackString) {
   })
 }
 
-// function createInlineWorker (workerFunction) {
-//   let code = workerFunction.toString()
-//   code = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}'))
-//
-//   const blob = new Blob([code], {type: 'application/javascript'})
-//   const worker = new Worker(URL.createObjectURL(blob))
-//
-//   return worker
-// }
-
-// function makeWorker (script) {
-//   var URL = window.URL || window.webkitURL
-//   var Blob = window.Blob
-//   var Worker = window.Worker
-//
-//   if (!URL || !Blob || !Worker || !script) {
-//     return null
-//   }
-//
-//   let code = script.toString()
-//   code = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}'))
-//
-//   var blob = new Blob([code])
-//   var worker = new Worker(URL.createObjectURL(blob))
-//   return worker
-// }
-
 function getSpeed (fileProgress) {
   if (!fileProgress.bytesUploaded) return 0
 
@@ -332,22 +371,6 @@ function prettyETA (seconds) {
   return `${hoursStr}${minutesStr}${secondsStr}`
 }
 
-// function makeCachingFunction () {
-//   let cachedEl = null
-//   let lastUpdate = Date.now()
-//
-//   return function cacheElement (el, time) {
-//     if (Date.now() - lastUpdate < time) {
-//       return cachedEl
-//     }
-//
-//     cachedEl = el
-//     lastUpdate = Date.now()
-//
-//     return el
-//   }
-// }
-
 /**
  * Check if an object is a DOM element. Duck-typing based on `nodeType`.
  *
@@ -403,8 +426,6 @@ module.exports = {
   every,
   flatten,
   groupBy,
-  // $,
-  // $$,
   extend,
   runPromiseSequence,
   supportsMediaRecorder,
@@ -413,6 +434,8 @@ module.exports = {
   truncateString,
   getFileTypeExtension,
   getFileType,
+  getArrayBuffer,
+  isPreviewSupported,
   getThumbnail,
   secondsToTime,
   dataURItoBlob,
@@ -420,8 +443,6 @@ module.exports = {
   getSpeed,
   getBytesRemaining,
   getETA,
-  // makeWorker,
-  // makeCachingFunction,
   copyToClipboard,
   prettyETA,
   findDOMElement,

+ 0 - 11
src/plugins/Dashboard/FileCard.js

@@ -2,17 +2,6 @@ const html = require('yo-yo')
 const getFileTypeIcon = require('./getFileTypeIcon')
 const { checkIcon } = require('./icons')
 
-// function getIconByMime (fileTypeGeneral) {
-//   switch (fileTypeGeneral) {
-//     case 'text':
-//       return iconText()
-//     case 'audio':
-//       return iconAudio()
-//     default:
-//       return iconFile()
-//   }
-// }
-
 module.exports = function fileCard (props) {
   const file = props.fileCardFor ? props.files[props.fileCardFor] : false
   const meta = {}

+ 559 - 0
src/vendor/file-type/index.js

@@ -0,0 +1,559 @@
+'use strict';
+
+module.exports = input => {
+	const buf = new Uint8Array(input);
+
+	if (!(buf && buf.length > 1)) {
+		return null;
+	}
+
+	const check = (header, opts) => {
+		opts = Object.assign({
+			offset: 0
+		}, opts);
+
+		for (let i = 0; i < header.length; i++) {
+			if (header[i] !== buf[i + opts.offset]) {
+				return false;
+			}
+		}
+
+		return true;
+	};
+
+	if (check([0xFF, 0xD8, 0xFF])) {
+		return {
+			ext: 'jpg',
+			mime: 'image/jpeg'
+		};
+	}
+
+	if (check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) {
+		return {
+			ext: 'png',
+			mime: 'image/png'
+		};
+	}
+
+	if (check([0x47, 0x49, 0x46])) {
+		return {
+			ext: 'gif',
+			mime: 'image/gif'
+		};
+	}
+
+	if (check([0x57, 0x45, 0x42, 0x50], {offset: 8})) {
+		return {
+			ext: 'webp',
+			mime: 'image/webp'
+		};
+	}
+
+	if (check([0x46, 0x4C, 0x49, 0x46])) {
+		return {
+			ext: 'flif',
+			mime: 'image/flif'
+		};
+	}
+
+	// Needs to be before `tif` check
+	if (
+		(check([0x49, 0x49, 0x2A, 0x0]) || check([0x4D, 0x4D, 0x0, 0x2A])) &&
+		check([0x43, 0x52], {offset: 8})
+	) {
+		return {
+			ext: 'cr2',
+			mime: 'image/x-canon-cr2'
+		};
+	}
+
+	if (
+		check([0x49, 0x49, 0x2A, 0x0]) ||
+		check([0x4D, 0x4D, 0x0, 0x2A])
+	) {
+		return {
+			ext: 'tif',
+			mime: 'image/tiff'
+		};
+	}
+
+	if (check([0x42, 0x4D])) {
+		return {
+			ext: 'bmp',
+			mime: 'image/bmp'
+		};
+	}
+
+	if (check([0x49, 0x49, 0xBC])) {
+		return {
+			ext: 'jxr',
+			mime: 'image/vnd.ms-photo'
+		};
+	}
+
+	if (check([0x38, 0x42, 0x50, 0x53])) {
+		return {
+			ext: 'psd',
+			mime: 'image/vnd.adobe.photoshop'
+		};
+	}
+
+	// Needs to be before the `zip` check
+	if (
+		check([0x50, 0x4B, 0x3, 0x4]) &&
+		check([0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x65, 0x70, 0x75, 0x62, 0x2B, 0x7A, 0x69, 0x70], {offset: 30})
+	) {
+		return {
+			ext: 'epub',
+			mime: 'application/epub+zip'
+		};
+	}
+
+	// Needs to be before `zip` check
+	// Assumes signed `.xpi` from addons.mozilla.org
+	if (
+		check([0x50, 0x4B, 0x3, 0x4]) &&
+		check([0x4D, 0x45, 0x54, 0x41, 0x2D, 0x49, 0x4E, 0x46, 0x2F, 0x6D, 0x6F, 0x7A, 0x69, 0x6C, 0x6C, 0x61, 0x2E, 0x72, 0x73, 0x61], {offset: 30})
+	) {
+		return {
+			ext: 'xpi',
+			mime: 'application/x-xpinstall'
+		};
+	}
+
+	if (
+		check([0x50, 0x4B]) &&
+		(buf[2] === 0x3 || buf[2] === 0x5 || buf[2] === 0x7) &&
+		(buf[3] === 0x4 || buf[3] === 0x6 || buf[3] === 0x8)
+	) {
+		return {
+			ext: 'zip',
+			mime: 'application/zip'
+		};
+	}
+
+	if (check([0x75, 0x73, 0x74, 0x61, 0x72], {offset: 257})) {
+		return {
+			ext: 'tar',
+			mime: 'application/x-tar'
+		};
+	}
+
+	if (
+		check([0x52, 0x61, 0x72, 0x21, 0x1A, 0x7]) &&
+		(buf[6] === 0x0 || buf[6] === 0x1)
+	) {
+		return {
+			ext: 'rar',
+			mime: 'application/x-rar-compressed'
+		};
+	}
+
+	if (check([0x1F, 0x8B, 0x8])) {
+		return {
+			ext: 'gz',
+			mime: 'application/gzip'
+		};
+	}
+
+	if (check([0x42, 0x5A, 0x68])) {
+		return {
+			ext: 'bz2',
+			mime: 'application/x-bzip2'
+		};
+	}
+
+	if (check([0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])) {
+		return {
+			ext: '7z',
+			mime: 'application/x-7z-compressed'
+		};
+	}
+
+	if (check([0x78, 0x01])) {
+		return {
+			ext: 'dmg',
+			mime: 'application/x-apple-diskimage'
+		};
+	}
+
+	if (
+		(
+			check([0x0, 0x0, 0x0]) &&
+			(buf[3] === 0x18 || buf[3] === 0x20) &&
+			check([0x66, 0x74, 0x79, 0x70], {offset: 4})
+		) ||
+		check([0x33, 0x67, 0x70, 0x35]) ||
+		(
+			check([0x0, 0x0, 0x0, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x6D, 0x70, 0x34, 0x32]) &&
+			check([0x6D, 0x70, 0x34, 0x31, 0x6D, 0x70, 0x34, 0x32, 0x69, 0x73, 0x6F, 0x6D], {offset: 16})
+		) ||
+		check([0x0, 0x0, 0x0, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6F, 0x6D]) ||
+		check([0x0, 0x0, 0x0, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x6D, 0x70, 0x34, 0x32, 0x0, 0x0, 0x0, 0x0])
+	) {
+		return {
+			ext: 'mp4',
+			mime: 'video/mp4'
+		};
+	}
+
+	if (check([0x0, 0x0, 0x0, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x56])) {
+		return {
+			ext: 'm4v',
+			mime: 'video/x-m4v'
+		};
+	}
+
+	if (check([0x4D, 0x54, 0x68, 0x64])) {
+		return {
+			ext: 'mid',
+			mime: 'audio/midi'
+		};
+	}
+
+	// https://github.com/threatstack/libmagic/blob/master/magic/Magdir/matroska
+	if (check([0x1A, 0x45, 0xDF, 0xA3])) {
+		const sliced = buf.subarray(4, 4 + 4096);
+		const idPos = sliced.findIndex((el, i, arr) => arr[i] === 0x42 && arr[i + 1] === 0x82);
+
+		if (idPos >= 0) {
+			const docTypePos = idPos + 3;
+			const findDocType = type => Array.from(type).every((c, i) => sliced[docTypePos + i] === c.charCodeAt(0));
+
+			if (findDocType('matroska')) {
+				return {
+					ext: 'mkv',
+					mime: 'video/x-matroska'
+				};
+			}
+
+			if (findDocType('webm')) {
+				return {
+					ext: 'webm',
+					mime: 'video/webm'
+				};
+			}
+		}
+	}
+
+	if (check([0x0, 0x0, 0x0, 0x14, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20]) ||
+		check([0x66, 0x72, 0x65, 0x65], {offset: 4}) ||
+		check([0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20], {offset: 4}) ||
+		check([0x6D, 0x64, 0x61, 0x74], {offset: 4}) || // MJPEG
+		check([0x77, 0x69, 0x64, 0x65], {offset: 4})) {
+		return {
+			ext: 'mov',
+			mime: 'video/quicktime'
+		};
+	}
+
+	if (
+		check([0x52, 0x49, 0x46, 0x46]) &&
+		check([0x41, 0x56, 0x49], {offset: 8})
+	) {
+		return {
+			ext: 'avi',
+			mime: 'video/x-msvideo'
+		};
+	}
+
+	if (check([0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9])) {
+		return {
+			ext: 'wmv',
+			mime: 'video/x-ms-wmv'
+		};
+	}
+
+	if (check([0x0, 0x0, 0x1, 0xBA])) {
+		return {
+			ext: 'mpg',
+			mime: 'video/mpeg'
+		};
+	}
+
+	if (
+		check([0x49, 0x44, 0x33]) ||
+		check([0xFF, 0xFB])
+	) {
+		return {
+			ext: 'mp3',
+			mime: 'audio/mpeg'
+		};
+	}
+
+	if (
+		check([0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x41], {offset: 4}) ||
+		check([0x4D, 0x34, 0x41, 0x20])
+	) {
+		return {
+			ext: 'm4a',
+			mime: 'audio/m4a'
+		};
+	}
+
+	// Needs to be before `ogg` check
+	if (check([0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64], {offset: 28})) {
+		return {
+			ext: 'opus',
+			mime: 'audio/opus'
+		};
+	}
+
+	if (check([0x4F, 0x67, 0x67, 0x53])) {
+		return {
+			ext: 'ogg',
+			mime: 'audio/ogg'
+		};
+	}
+
+	if (check([0x66, 0x4C, 0x61, 0x43])) {
+		return {
+			ext: 'flac',
+			mime: 'audio/x-flac'
+		};
+	}
+
+	if (
+		check([0x52, 0x49, 0x46, 0x46]) &&
+		check([0x57, 0x41, 0x56, 0x45], {offset: 8})
+	) {
+		return {
+			ext: 'wav',
+			mime: 'audio/x-wav'
+		};
+	}
+
+	if (check([0x23, 0x21, 0x41, 0x4D, 0x52, 0x0A])) {
+		return {
+			ext: 'amr',
+			mime: 'audio/amr'
+		};
+	}
+
+	if (check([0x25, 0x50, 0x44, 0x46])) {
+		return {
+			ext: 'pdf',
+			mime: 'application/pdf'
+		};
+	}
+
+	if (check([0x4D, 0x5A])) {
+		return {
+			ext: 'exe',
+			mime: 'application/x-msdownload'
+		};
+	}
+
+	if (
+		(buf[0] === 0x43 || buf[0] === 0x46) &&
+		check([0x57, 0x53], {offset: 1})
+	) {
+		return {
+			ext: 'swf',
+			mime: 'application/x-shockwave-flash'
+		};
+	}
+
+	if (check([0x7B, 0x5C, 0x72, 0x74, 0x66])) {
+		return {
+			ext: 'rtf',
+			mime: 'application/rtf'
+		};
+	}
+
+	if (check([0x00, 0x61, 0x73, 0x6D])) {
+		return {
+			ext: 'wasm',
+			mime: 'application/wasm'
+		};
+	}
+
+	if (
+		check([0x77, 0x4F, 0x46, 0x46]) &&
+		(
+			check([0x00, 0x01, 0x00, 0x00], {offset: 4}) ||
+			check([0x4F, 0x54, 0x54, 0x4F], {offset: 4})
+		)
+	) {
+		return {
+			ext: 'woff',
+			mime: 'font/woff'
+		};
+	}
+
+	if (
+		check([0x77, 0x4F, 0x46, 0x32]) &&
+		(
+			check([0x00, 0x01, 0x00, 0x00], {offset: 4}) ||
+			check([0x4F, 0x54, 0x54, 0x4F], {offset: 4})
+		)
+	) {
+		return {
+			ext: 'woff2',
+			mime: 'font/woff2'
+		};
+	}
+
+	if (
+		check([0x4C, 0x50], {offset: 34}) &&
+		(
+			check([0x00, 0x00, 0x01], {offset: 8}) ||
+			check([0x01, 0x00, 0x02], {offset: 8}) ||
+			check([0x02, 0x00, 0x02], {offset: 8})
+		)
+	) {
+		return {
+			ext: 'eot',
+			mime: 'application/octet-stream'
+		};
+	}
+
+	if (check([0x00, 0x01, 0x00, 0x00, 0x00])) {
+		return {
+			ext: 'ttf',
+			mime: 'font/ttf'
+		};
+	}
+
+	if (check([0x4F, 0x54, 0x54, 0x4F, 0x00])) {
+		return {
+			ext: 'otf',
+			mime: 'font/otf'
+		};
+	}
+
+	if (check([0x00, 0x00, 0x01, 0x00])) {
+		return {
+			ext: 'ico',
+			mime: 'image/x-icon'
+		};
+	}
+
+	if (check([0x46, 0x4C, 0x56, 0x01])) {
+		return {
+			ext: 'flv',
+			mime: 'video/x-flv'
+		};
+	}
+
+	if (check([0x25, 0x21])) {
+		return {
+			ext: 'ps',
+			mime: 'application/postscript'
+		};
+	}
+
+	if (check([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) {
+		return {
+			ext: 'xz',
+			mime: 'application/x-xz'
+		};
+	}
+
+	if (check([0x53, 0x51, 0x4C, 0x69])) {
+		return {
+			ext: 'sqlite',
+			mime: 'application/x-sqlite3'
+		};
+	}
+
+	if (check([0x4E, 0x45, 0x53, 0x1A])) {
+		return {
+			ext: 'nes',
+			mime: 'application/x-nintendo-nes-rom'
+		};
+	}
+
+	if (check([0x43, 0x72, 0x32, 0x34])) {
+		return {
+			ext: 'crx',
+			mime: 'application/x-google-chrome-extension'
+		};
+	}
+
+	if (
+		check([0x4D, 0x53, 0x43, 0x46]) ||
+		check([0x49, 0x53, 0x63, 0x28])
+	) {
+		return {
+			ext: 'cab',
+			mime: 'application/vnd.ms-cab-compressed'
+		};
+	}
+
+	// Needs to be before `ar` check
+	if (check([0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E, 0x0A, 0x64, 0x65, 0x62, 0x69, 0x61, 0x6E, 0x2D, 0x62, 0x69, 0x6E, 0x61, 0x72, 0x79])) {
+		return {
+			ext: 'deb',
+			mime: 'application/x-deb'
+		};
+	}
+
+	if (check([0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E])) {
+		return {
+			ext: 'ar',
+			mime: 'application/x-unix-archive'
+		};
+	}
+
+	if (check([0xED, 0xAB, 0xEE, 0xDB])) {
+		return {
+			ext: 'rpm',
+			mime: 'application/x-rpm'
+		};
+	}
+
+	if (
+		check([0x1F, 0xA0]) ||
+		check([0x1F, 0x9D])
+	) {
+		return {
+			ext: 'Z',
+			mime: 'application/x-compress'
+		};
+	}
+
+	if (check([0x4C, 0x5A, 0x49, 0x50])) {
+		return {
+			ext: 'lz',
+			mime: 'application/x-lzip'
+		};
+	}
+
+	if (check([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1])) {
+		return {
+			ext: 'msi',
+			mime: 'application/x-msi'
+		};
+	}
+
+	if (check([0x06, 0x0E, 0x2B, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0D, 0x01, 0x02, 0x01, 0x01, 0x02])) {
+		return {
+			ext: 'mxf',
+			mime: 'application/mxf'
+		};
+	}
+
+	if (check([0x47], {offset: 4}) && (check([0x47], {offset: 192}) || check([0x47], {offset: 196}))) {
+		return {
+			ext: 'mts',
+			mime: 'video/mp2t'
+		};
+	}
+
+	if (check([0x42, 0x4C, 0x45, 0x4E, 0x44, 0x45, 0x52])) {
+		return {
+			ext: 'blend',
+			mime: 'application/x-blender'
+		};
+	}
+
+	if (check([0x42, 0x50, 0x47, 0xFB])) {
+		return {
+			ext: 'bpg',
+			mime: 'image/bpg'
+		};
+	}
+
+	return null;
+};

+ 21 - 0
src/vendor/file-type/license

@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

+ 109 - 0
src/vendor/file-type/package.json

@@ -0,0 +1,109 @@
+{
+  "name": "file-type",
+  "version": "5.2.0",
+  "description": "Detect the file type of a Buffer/Uint8Array",
+  "license": "MIT",
+  "repository": "sindresorhus/file-type",
+  "author": {
+    "name": "Sindre Sorhus",
+    "email": "sindresorhus@gmail.com",
+    "url": "sindresorhus.com"
+  },
+  "engines": {
+    "node": ">=4"
+  },
+  "scripts": {
+    "test": "xo && ava"
+  },
+  "files": [
+    "index.js"
+  ],
+  "keywords": [
+    "mime",
+    "file",
+    "type",
+    "archive",
+    "image",
+    "img",
+    "pic",
+    "picture",
+    "flash",
+    "photo",
+    "video",
+    "type",
+    "detect",
+    "check",
+    "is",
+    "exif",
+    "exe",
+    "binary",
+    "buffer",
+    "uint8array",
+    "jpg",
+    "png",
+    "gif",
+    "webp",
+    "flif",
+    "cr2",
+    "tif",
+    "bmp",
+    "jxr",
+    "psd",
+    "zip",
+    "tar",
+    "rar",
+    "gz",
+    "bz2",
+    "7z",
+    "dmg",
+    "mp4",
+    "m4v",
+    "mid",
+    "mkv",
+    "webm",
+    "mov",
+    "avi",
+    "mpg",
+    "mp3",
+    "m4a",
+    "ogg",
+    "opus",
+    "flac",
+    "wav",
+    "amr",
+    "pdf",
+    "epub",
+    "exe",
+    "swf",
+    "rtf",
+    "woff",
+    "woff2",
+    "eot",
+    "ttf",
+    "otf",
+    "ico",
+    "flv",
+    "ps",
+    "xz",
+    "sqlite",
+    "xpi",
+    "cab",
+    "deb",
+    "ar",
+    "rpm",
+    "Z",
+    "lz",
+    "msi",
+    "mxf",
+    "mts",
+    "wasm",
+    "webassembly",
+    "blend",
+    "bpg"
+  ],
+  "devDependencies": {
+    "ava": "*",
+    "read-chunk": "^2.0.0",
+    "xo": "*"
+  }
+}