|
1 | | -import mimeTypes from 'mime-types'; |
2 | | - |
3 | 1 | const DEFAULT_FILES_TO_IGNORE = [ |
4 | 2 | '.DS_Store', // OSX indexing file |
5 | 3 | 'Thumbs.db' // Windows indexing file |
6 | 4 | ]; |
7 | 5 |
|
| 6 | +// map of common (mostly media types) mime types to use when the browser does not supply the mime type |
| 7 | +const EXTENSION_TO_MIME_TYPE_MAP = { |
| 8 | + avi: 'video/avi', |
| 9 | + gif: 'image/gif', |
| 10 | + ico: 'image/x-icon', |
| 11 | + jpeg: 'image/jpeg', |
| 12 | + jpg: 'image/jpeg', |
| 13 | + mkv: 'video/x-matroska', |
| 14 | + mov: 'video/quicktime', |
| 15 | + mp4: 'video/mp4', |
| 16 | + pdf: 'application/pdf', |
| 17 | + png: 'image/png', |
| 18 | + zip: 'application/zip' |
| 19 | +}; |
| 20 | + |
8 | 21 | function shouldIgnoreFile(file) { |
9 | 22 | return DEFAULT_FILES_TO_IGNORE.indexOf(file.name) >= 0; |
10 | 23 | } |
11 | 24 |
|
| 25 | +function copyString(aString) { |
| 26 | + return ` ${aString}`.slice(1); |
| 27 | +} |
| 28 | + |
12 | 29 | function traverseDirectory(entry) { |
13 | 30 | const reader = entry.createReader(); |
14 | 31 | // Resolved when the entire directory is traversed |
@@ -48,14 +65,18 @@ function packageFile(file, entry) { |
48 | 65 | // handle some browsers sometimes missing mime types for dropped files |
49 | 66 | const hasExtension = file.name && file.name.lastIndexOf('.') !== -1; |
50 | 67 | if (hasExtension && !file.type) { |
51 | | - fileTypeOverride = mimeTypes.lookup(file.name); |
| 68 | + const fileExtension = (file.name || '').split('.').pop(); |
| 69 | + fileTypeOverride = EXTENSION_TO_MIME_TYPE_MAP[fileExtension]; |
52 | 70 | } |
53 | 71 | return { |
54 | | - fileObject: file, |
55 | | - type: file.type ? file.type : fileTypeOverride, |
| 72 | + fileObject: file, // provide access to the raw File object (required for uploading) |
| 73 | + fullPath: entry ? copyString(entry.fullPath) : file.name, |
| 74 | + lastModified: file.lastModified, |
| 75 | + lastModifiedDate: file.lastModifiedDate, |
56 | 76 | name: file.name, |
57 | 77 | size: file.size, |
58 | | - fullPath: entry ? entry.fullPath : file.name |
| 78 | + type: file.type ? file.type : fileTypeOverride, |
| 79 | + webkitRelativePath: file.webkitRelativePath |
59 | 80 | }; |
60 | 81 | } |
61 | 82 |
|
|
0 commit comments