UNPKG

4.42 kBJavaScriptView Raw
1export const COMMON_MIME_TYPES = new Map([
2 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
3 ['aac', 'audio/aac'],
4 ['abw', 'application/x-abiword'],
5 ['arc', 'application/x-freearc'],
6 ['avif', 'image/avif'],
7 ['avi', 'video/x-msvideo'],
8 ['azw', 'application/vnd.amazon.ebook'],
9 ['bin', 'application/octet-stream'],
10 ['bmp', 'image/bmp'],
11 ['bz', 'application/x-bzip'],
12 ['bz2', 'application/x-bzip2'],
13 ['cda', 'application/x-cdf'],
14 ['csh', 'application/x-csh'],
15 ['css', 'text/css'],
16 ['csv', 'text/csv'],
17 ['doc', 'application/msword'],
18 ['docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
19 ['eot', 'application/vnd.ms-fontobject'],
20 ['epub', 'application/epub+zip'],
21 ['gz', 'application/gzip'],
22 ['gif', 'image/gif'],
23 ['heic', 'image/heic'],
24 ['heif', 'image/heif'],
25 ['htm', 'text/html'],
26 ['html', 'text/html'],
27 ['ico', 'image/vnd.microsoft.icon'],
28 ['ics', 'text/calendar'],
29 ['jar', 'application/java-archive'],
30 ['jpeg', 'image/jpeg'],
31 ['jpg', 'image/jpeg'],
32 ['js', 'text/javascript'],
33 ['json', 'application/json'],
34 ['jsonld', 'application/ld+json'],
35 ['mid', 'audio/midi'],
36 ['midi', 'audio/midi'],
37 ['mjs', 'text/javascript'],
38 ['mp3', 'audio/mpeg'],
39 ['mp4', 'video/mp4'],
40 ['mpeg', 'video/mpeg'],
41 ['mpkg', 'application/vnd.apple.installer+xml'],
42 ['odp', 'application/vnd.oasis.opendocument.presentation'],
43 ['ods', 'application/vnd.oasis.opendocument.spreadsheet'],
44 ['odt', 'application/vnd.oasis.opendocument.text'],
45 ['oga', 'audio/ogg'],
46 ['ogv', 'video/ogg'],
47 ['ogx', 'application/ogg'],
48 ['opus', 'audio/opus'],
49 ['otf', 'font/otf'],
50 ['png', 'image/png'],
51 ['pdf', 'application/pdf'],
52 ['php', 'application/x-httpd-php'],
53 ['ppt', 'application/vnd.ms-powerpoint'],
54 ['pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
55 ['rar', 'application/vnd.rar'],
56 ['rtf', 'application/rtf'],
57 ['sh', 'application/x-sh'],
58 ['svg', 'image/svg+xml'],
59 ['swf', 'application/x-shockwave-flash'],
60 ['tar', 'application/x-tar'],
61 ['tif', 'image/tiff'],
62 ['tiff', 'image/tiff'],
63 ['ts', 'video/mp2t'],
64 ['ttf', 'font/ttf'],
65 ['txt', 'text/plain'],
66 ['vsd', 'application/vnd.visio'],
67 ['wav', 'audio/wav'],
68 ['weba', 'audio/webm'],
69 ['webm', 'video/webm'],
70 ['webp', 'image/webp'],
71 ['woff', 'font/woff'],
72 ['woff2', 'font/woff2'],
73 ['xhtml', 'application/xhtml+xml'],
74 ['xls', 'application/vnd.ms-excel'],
75 ['xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
76 ['xml', 'application/xml'],
77 ['xul', 'application/vnd.mozilla.xul+xml'],
78 ['zip', 'application/zip'],
79 ['7z', 'application/x-7z-compressed'],
80 // Others
81 ['mkv', 'video/x-matroska'],
82 ['mov', 'video/quicktime'],
83 ['msg', 'application/vnd.ms-outlook']
84]);
85export function toFileWithPath(file, path) {
86 const f = withMimeType(file);
87 if (typeof f.path !== 'string') { // on electron, path is already set to the absolute path
88 const { webkitRelativePath } = file;
89 Object.defineProperty(f, 'path', {
90 value: typeof path === 'string'
91 ? path
92 // If <input webkitdirectory> is set,
93 // the File will have a {webkitRelativePath} property
94 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory
95 : typeof webkitRelativePath === 'string' && webkitRelativePath.length > 0
96 ? webkitRelativePath
97 : file.name,
98 writable: false,
99 configurable: false,
100 enumerable: true
101 });
102 }
103 return f;
104}
105function withMimeType(file) {
106 const { name } = file;
107 const hasExtension = name && name.lastIndexOf('.') !== -1;
108 if (hasExtension && !file.type) {
109 const ext = name.split('.')
110 .pop().toLowerCase();
111 const type = COMMON_MIME_TYPES.get(ext);
112 if (type) {
113 Object.defineProperty(file, 'type', {
114 value: type,
115 writable: false,
116 configurable: false,
117 enumerable: true
118 });
119 }
120 }
121 return file;
122}
123//# sourceMappingURL=file.js.map
\No newline at end of file