UNPKG

18.4 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3 typeof define === 'function' && define.amd ? define(['exports'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.fileSelector = {}));
5})(this, (function (exports) { 'use strict';
6
7 /******************************************************************************
8 Copyright (c) Microsoft Corporation.
9
10 Permission to use, copy, modify, and/or distribute this software for any
11 purpose with or without fee is hereby granted.
12
13 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15 AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18 OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19 PERFORMANCE OF THIS SOFTWARE.
20 ***************************************************************************** */
21
22 function __awaiter(thisArg, _arguments, P, generator) {
23 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24 return new (P || (P = Promise))(function (resolve, reject) {
25 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28 step((generator = generator.apply(thisArg, _arguments || [])).next());
29 });
30 }
31
32 function __generator(thisArg, body) {
33 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
34 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
35 function verb(n) { return function (v) { return step([n, v]); }; }
36 function step(op) {
37 if (f) throw new TypeError("Generator is already executing.");
38 while (_) try {
39 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
40 if (y = 0, t) op = [op[0] & 2, t.value];
41 switch (op[0]) {
42 case 0: case 1: t = op; break;
43 case 4: _.label++; return { value: op[1], done: false };
44 case 5: _.label++; y = op[1]; op = [0]; continue;
45 case 7: op = _.ops.pop(); _.trys.pop(); continue;
46 default:
47 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
48 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
49 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
50 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
51 if (t[2]) _.ops.pop();
52 _.trys.pop(); continue;
53 }
54 op = body.call(thisArg, _);
55 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
56 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
57 }
58 }
59
60 function __read(o, n) {
61 var m = typeof Symbol === "function" && o[Symbol.iterator];
62 if (!m) return o;
63 var i = m.call(o), r, ar = [], e;
64 try {
65 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
66 }
67 catch (error) { e = { error: error }; }
68 finally {
69 try {
70 if (r && !r.done && (m = i["return"])) m.call(i);
71 }
72 finally { if (e) throw e.error; }
73 }
74 return ar;
75 }
76
77 function __spreadArray(to, from, pack) {
78 if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
79 if (ar || !(i in from)) {
80 if (!ar) ar = Array.prototype.slice.call(from, 0, i);
81 ar[i] = from[i];
82 }
83 }
84 return to.concat(ar || Array.prototype.slice.call(from));
85 }
86
87 var COMMON_MIME_TYPES = new Map([
88 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
89 ['aac', 'audio/aac'],
90 ['abw', 'application/x-abiword'],
91 ['arc', 'application/x-freearc'],
92 ['avif', 'image/avif'],
93 ['avi', 'video/x-msvideo'],
94 ['azw', 'application/vnd.amazon.ebook'],
95 ['bin', 'application/octet-stream'],
96 ['bmp', 'image/bmp'],
97 ['bz', 'application/x-bzip'],
98 ['bz2', 'application/x-bzip2'],
99 ['cda', 'application/x-cdf'],
100 ['csh', 'application/x-csh'],
101 ['css', 'text/css'],
102 ['csv', 'text/csv'],
103 ['doc', 'application/msword'],
104 ['docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
105 ['eot', 'application/vnd.ms-fontobject'],
106 ['epub', 'application/epub+zip'],
107 ['gz', 'application/gzip'],
108 ['gif', 'image/gif'],
109 ['heic', 'image/heic'],
110 ['heif', 'image/heif'],
111 ['htm', 'text/html'],
112 ['html', 'text/html'],
113 ['ico', 'image/vnd.microsoft.icon'],
114 ['ics', 'text/calendar'],
115 ['jar', 'application/java-archive'],
116 ['jpeg', 'image/jpeg'],
117 ['jpg', 'image/jpeg'],
118 ['js', 'text/javascript'],
119 ['json', 'application/json'],
120 ['jsonld', 'application/ld+json'],
121 ['mid', 'audio/midi'],
122 ['midi', 'audio/midi'],
123 ['mjs', 'text/javascript'],
124 ['mp3', 'audio/mpeg'],
125 ['mp4', 'video/mp4'],
126 ['mpeg', 'video/mpeg'],
127 ['mpkg', 'application/vnd.apple.installer+xml'],
128 ['odp', 'application/vnd.oasis.opendocument.presentation'],
129 ['ods', 'application/vnd.oasis.opendocument.spreadsheet'],
130 ['odt', 'application/vnd.oasis.opendocument.text'],
131 ['oga', 'audio/ogg'],
132 ['ogv', 'video/ogg'],
133 ['ogx', 'application/ogg'],
134 ['opus', 'audio/opus'],
135 ['otf', 'font/otf'],
136 ['png', 'image/png'],
137 ['pdf', 'application/pdf'],
138 ['php', 'application/x-httpd-php'],
139 ['ppt', 'application/vnd.ms-powerpoint'],
140 ['pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
141 ['rar', 'application/vnd.rar'],
142 ['rtf', 'application/rtf'],
143 ['sh', 'application/x-sh'],
144 ['svg', 'image/svg+xml'],
145 ['swf', 'application/x-shockwave-flash'],
146 ['tar', 'application/x-tar'],
147 ['tif', 'image/tiff'],
148 ['tiff', 'image/tiff'],
149 ['ts', 'video/mp2t'],
150 ['ttf', 'font/ttf'],
151 ['txt', 'text/plain'],
152 ['vsd', 'application/vnd.visio'],
153 ['wav', 'audio/wav'],
154 ['weba', 'audio/webm'],
155 ['webm', 'video/webm'],
156 ['webp', 'image/webp'],
157 ['woff', 'font/woff'],
158 ['woff2', 'font/woff2'],
159 ['xhtml', 'application/xhtml+xml'],
160 ['xls', 'application/vnd.ms-excel'],
161 ['xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
162 ['xml', 'application/xml'],
163 ['xul', 'application/vnd.mozilla.xul+xml'],
164 ['zip', 'application/zip'],
165 ['7z', 'application/x-7z-compressed'],
166 // Others
167 ['mkv', 'video/x-matroska'],
168 ['mov', 'video/quicktime'],
169 ['msg', 'application/vnd.ms-outlook']
170 ]);
171 function toFileWithPath(file, path) {
172 var f = withMimeType(file);
173 if (typeof f.path !== 'string') { // on electron, path is already set to the absolute path
174 var webkitRelativePath = file.webkitRelativePath;
175 Object.defineProperty(f, 'path', {
176 value: typeof path === 'string'
177 ? path
178 // If <input webkitdirectory> is set,
179 // the File will have a {webkitRelativePath} property
180 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory
181 : typeof webkitRelativePath === 'string' && webkitRelativePath.length > 0
182 ? webkitRelativePath
183 : file.name,
184 writable: false,
185 configurable: false,
186 enumerable: true
187 });
188 }
189 return f;
190 }
191 function withMimeType(file) {
192 var name = file.name;
193 var hasExtension = name && name.lastIndexOf('.') !== -1;
194 if (hasExtension && !file.type) {
195 var ext = name.split('.')
196 .pop().toLowerCase();
197 var type = COMMON_MIME_TYPES.get(ext);
198 if (type) {
199 Object.defineProperty(file, 'type', {
200 value: type,
201 writable: false,
202 configurable: false,
203 enumerable: true
204 });
205 }
206 }
207 return file;
208 }
209
210 var FILES_TO_IGNORE = [
211 // Thumbnail cache files for macOS and Windows
212 '.DS_Store',
213 'Thumbs.db' // Windows
214 ];
215 /**
216 * Convert a DragEvent's DataTrasfer object to a list of File objects
217 * NOTE: If some of the items are folders,
218 * everything will be flattened and placed in the same list but the paths will be kept as a {path} property.
219 *
220 * EXPERIMENTAL: A list of https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle objects can also be passed as an arg
221 * and a list of File objects will be returned.
222 *
223 * @param evt
224 */
225 function fromEvent(evt) {
226 return __awaiter(this, void 0, void 0, function () {
227 return __generator(this, function (_a) {
228 if (isObject(evt) && isDataTransfer(evt.dataTransfer)) {
229 return [2 /*return*/, getDataTransferFiles(evt.dataTransfer, evt.type)];
230 }
231 else if (isChangeEvt(evt)) {
232 return [2 /*return*/, getInputFiles(evt)];
233 }
234 else if (Array.isArray(evt) && evt.every(function (item) { return 'getFile' in item && typeof item.getFile === 'function'; })) {
235 return [2 /*return*/, getFsHandleFiles(evt)];
236 }
237 return [2 /*return*/, []];
238 });
239 });
240 }
241 function isDataTransfer(value) {
242 return isObject(value);
243 }
244 function isChangeEvt(value) {
245 return isObject(value) && isObject(value.target);
246 }
247 function isObject(v) {
248 return typeof v === 'object' && v !== null;
249 }
250 function getInputFiles(evt) {
251 return fromList(evt.target.files).map(function (file) { return toFileWithPath(file); });
252 }
253 // Ee expect each handle to be https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle
254 function getFsHandleFiles(handles) {
255 return __awaiter(this, void 0, void 0, function () {
256 var files;
257 return __generator(this, function (_a) {
258 switch (_a.label) {
259 case 0: return [4 /*yield*/, Promise.all(handles.map(function (h) { return h.getFile(); }))];
260 case 1:
261 files = _a.sent();
262 return [2 /*return*/, files.map(function (file) { return toFileWithPath(file); })];
263 }
264 });
265 });
266 }
267 function getDataTransferFiles(dt, type) {
268 return __awaiter(this, void 0, void 0, function () {
269 var items, files;
270 return __generator(this, function (_a) {
271 switch (_a.label) {
272 case 0:
273 if (!dt.items) return [3 /*break*/, 2];
274 items = fromList(dt.items)
275 .filter(function (item) { return item.kind === 'file'; });
276 // According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents,
277 // only 'dragstart' and 'drop' has access to the data (source node)
278 if (type !== 'drop') {
279 return [2 /*return*/, items];
280 }
281 return [4 /*yield*/, Promise.all(items.map(toFilePromises))];
282 case 1:
283 files = _a.sent();
284 return [2 /*return*/, noIgnoredFiles(flatten(files))];
285 case 2: return [2 /*return*/, noIgnoredFiles(fromList(dt.files)
286 .map(function (file) { return toFileWithPath(file); }))];
287 }
288 });
289 });
290 }
291 function noIgnoredFiles(files) {
292 return files.filter(function (file) { return FILES_TO_IGNORE.indexOf(file.name) === -1; });
293 }
294 // IE11 does not support Array.from()
295 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Browser_compatibility
296 // https://developer.mozilla.org/en-US/docs/Web/API/FileList
297 // https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItemList
298 function fromList(items) {
299 if (items === null) {
300 return [];
301 }
302 var files = [];
303 // tslint:disable: prefer-for-of
304 for (var i = 0; i < items.length; i++) {
305 var file = items[i];
306 files.push(file);
307 }
308 return files;
309 }
310 // https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem
311 function toFilePromises(item) {
312 if (typeof item.webkitGetAsEntry !== 'function') {
313 return fromDataTransferItem(item);
314 }
315 var entry = item.webkitGetAsEntry();
316 // Safari supports dropping an image node from a different window and can be retrieved using
317 // the DataTransferItem.getAsFile() API
318 // NOTE: FileSystemEntry.file() throws if trying to get the file
319 if (entry && entry.isDirectory) {
320 return fromDirEntry(entry);
321 }
322 return fromDataTransferItem(item);
323 }
324 function flatten(items) {
325 return items.reduce(function (acc, files) { return __spreadArray(__spreadArray([], __read(acc), false), __read((Array.isArray(files) ? flatten(files) : [files])), false); }, []);
326 }
327 function fromDataTransferItem(item) {
328 var file = item.getAsFile();
329 if (!file) {
330 return Promise.reject("".concat(item, " is not a File"));
331 }
332 var fwp = toFileWithPath(file);
333 return Promise.resolve(fwp);
334 }
335 // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemEntry
336 function fromEntry(entry) {
337 return __awaiter(this, void 0, void 0, function () {
338 return __generator(this, function (_a) {
339 return [2 /*return*/, entry.isDirectory ? fromDirEntry(entry) : fromFileEntry(entry)];
340 });
341 });
342 }
343 // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry
344 function fromDirEntry(entry) {
345 var reader = entry.createReader();
346 return new Promise(function (resolve, reject) {
347 var entries = [];
348 function readEntries() {
349 var _this = this;
350 // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry/createReader
351 // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryReader/readEntries
352 reader.readEntries(function (batch) { return __awaiter(_this, void 0, void 0, function () {
353 var files, err_1, items;
354 return __generator(this, function (_a) {
355 switch (_a.label) {
356 case 0:
357 if (!!batch.length) return [3 /*break*/, 5];
358 _a.label = 1;
359 case 1:
360 _a.trys.push([1, 3, , 4]);
361 return [4 /*yield*/, Promise.all(entries)];
362 case 2:
363 files = _a.sent();
364 resolve(files);
365 return [3 /*break*/, 4];
366 case 3:
367 err_1 = _a.sent();
368 reject(err_1);
369 return [3 /*break*/, 4];
370 case 4: return [3 /*break*/, 6];
371 case 5:
372 items = Promise.all(batch.map(fromEntry));
373 entries.push(items);
374 // Continue reading
375 readEntries();
376 _a.label = 6;
377 case 6: return [2 /*return*/];
378 }
379 });
380 }); }, function (err) {
381 reject(err);
382 });
383 }
384 readEntries();
385 });
386 }
387 // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry
388 function fromFileEntry(entry) {
389 return __awaiter(this, void 0, void 0, function () {
390 return __generator(this, function (_a) {
391 return [2 /*return*/, new Promise(function (resolve, reject) {
392 entry.file(function (file) {
393 var fwp = toFileWithPath(file, entry.fullPath);
394 resolve(fwp);
395 }, function (err) {
396 reject(err);
397 });
398 })];
399 });
400 });
401 }
402
403 exports.fromEvent = fromEvent;
404
405 Object.defineProperty(exports, '__esModule', { value: true });
406
407}));
408//# sourceMappingURL=file-selector.umd.js.map