Debian Codegraph @

install/freeipa-webui/node_modules/file-selector/dist/es2015/file-selector.js

raw
1 import { __awaiter } from "tslib";
2 import { toFileWithPath } from './file';
3 const FILES_TO_IGNORE = [
4 // Thumbnail cache files for macOS and Windows
5 '.DS_Store', // macOs
6 'Thumbs.db' // Windows
7 ];
8 /**
9 * Convert a DragEvent's DataTrasfer object to a list of File objects
10 * NOTE: If some of the items are folders,
11 * everything will be flattened and placed in the same list but the paths will be kept as a {path} property.
12 *
13 * EXPERIMENTAL: A list of https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle objects can also be passed as an arg
14 * and a list of File objects will be returned.
15 *
16 * @param evt
17 */
18 export function fromEvent(evt) {
19 return __awaiter(this, void 0, void 0, function* () {
20 if (isObject(evt) && isDataTransfer(evt.dataTransfer)) {
21 return getDataTransferFiles(evt.dataTransfer, evt.type);
22 }
23 else if (isChangeEvt(evt)) {
24 return getInputFiles(evt);
25 }
26 else if (Array.isArray(evt) && evt.every(item => 'getFile' in item && typeof item.getFile === 'function')) {
27 return getFsHandleFiles(evt);
28 }
29 return [];
30 });
31 }
32 function isDataTransfer(value) {
33 return isObject(value);
34 }
35 function isChangeEvt(value) {
36 return isObject(value) && isObject(value.target);
37 }
38 function isObject(v) {
39 return typeof v === 'object' && v !== null;
40 }
41 function getInputFiles(evt) {
42 return fromList(evt.target.files).map(file => toFileWithPath(file));
43 }
44 // Ee expect each handle to be https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle
45 function getFsHandleFiles(handles) {
46 return __awaiter(this, void 0, void 0, function* () {
47 const files = yield Promise.all(handles.map(h => h.getFile()));
48 return files.map(file => toFileWithPath(file));
49 });
50 }
51 function getDataTransferFiles(dt, type) {
52 return __awaiter(this, void 0, void 0, function* () {
53 // IE11 does not support dataTransfer.items
54 // See https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/items#Browser_compatibility
55 if (dt.items) {
56 const items = fromList(dt.items)
57 .filter(item => item.kind === 'file');
58 // According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents,
59 // only 'dragstart' and 'drop' has access to the data (source node)
60 if (type !== 'drop') {
61 return items;
62 }
63 const files = yield Promise.all(items.map(toFilePromises));
64 return noIgnoredFiles(flatten(files));
65 }
66 return noIgnoredFiles(fromList(dt.files)
67 .map(file => toFileWithPath(file)));
68 });
69 }
70 function noIgnoredFiles(files) {
71 return files.filter(file => FILES_TO_IGNORE.indexOf(file.name) === -1);
72 }
73 // IE11 does not support Array.from()
74 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Browser_compatibility
75 // https://developer.mozilla.org/en-US/docs/Web/API/FileList
76 // https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItemList
77 function fromList(items) {
78 if (items === null) {
79 return [];
80 }
81 const files = [];
82 // tslint:disable: prefer-for-of
83 for (let i = 0; i < items.length; i++) {
84 const file = items[i];
85 files.push(file);
86 }
87 return files;
88 }
89 // https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem
90 function toFilePromises(item) {
91 if (typeof item.webkitGetAsEntry !== 'function') {
92 return fromDataTransferItem(item);
93 }
94 const entry = item.webkitGetAsEntry();
95 // Safari supports dropping an image node from a different window and can be retrieved using
96 // the DataTransferItem.getAsFile() API
97 // NOTE: FileSystemEntry.file() throws if trying to get the file
98 if (entry && entry.isDirectory) {
99 return fromDirEntry(entry);
100 }
101 return fromDataTransferItem(item, entry);
102 }
103 function flatten(items) {
104 return items.reduce((acc, files) => [
105 ...acc,
106 ...(Array.isArray(files) ? flatten(files) : [files])
107 ], []);
108 }
109 function fromDataTransferItem(item, entry) {
110 return __awaiter(this, void 0, void 0, function* () {
111 var _a;
112 // Check if we're in a secure context; due to a bug in Chrome (as far as we know)
113 // the browser crashes when calling this API (yet to be confirmed as a consistent behaviour).
114 //
115 // See:
116 // - https://issues.chromium.org/issues/40186242
117 // - https://github.com/react-dropzone/react-dropzone/issues/1397
118 if (globalThis.isSecureContext && typeof item.getAsFileSystemHandle === 'function') {
119 const h = yield item.getAsFileSystemHandle();
120 if (h === null) {
121 throw new Error(`${item} is not a File`);
122 }
123 // It seems that the handle can be `undefined` (see https://github.com/react-dropzone/file-selector/issues/120),
124 // so we check if it isn't; if it is, the code path continues to the next API (`getAsFile`).
125 if (h !== undefined) {
126 const file = yield h.getFile();
127 file.handle = h;
128 return toFileWithPath(file);
129 }
130 }
131 const file = item.getAsFile();
132 if (!file) {
133 throw new Error(`${item} is not a File`);
134 }
135 const fwp = toFileWithPath(file, (_a = entry === null || entry === void 0 ? void 0 : entry.fullPath) !== null && _a !== void 0 ? _a : undefined);
136 return fwp;
137 });
138 }
139 // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemEntry
140 function fromEntry(entry) {
141 return __awaiter(this, void 0, void 0, function* () {
142 return entry.isDirectory ? fromDirEntry(entry) : fromFileEntry(entry);
143 });
144 }
145 // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry
146 function fromDirEntry(entry) {
147 const reader = entry.createReader();
148 return new Promise((resolve, reject) => {
149 const entries = [];
150 function readEntries() {
151 // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry/createReader
152 // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryReader/readEntries
153 reader.readEntries((batch) => __awaiter(this, void 0, void 0, function* () {
154 if (!batch.length) {
155 // Done reading directory
156 try {
157 const files = yield Promise.all(entries);
158 resolve(files);
159 }
160 catch (err) {
161 reject(err);
162 }
163 }
164 else {
165 const items = Promise.all(batch.map(fromEntry));
166 entries.push(items);
167 // Continue reading
168 readEntries();
169 }
170 }), (err) => {
171 reject(err);
172 });
173 }
174 readEntries();
175 });
176 }
177 // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry
178 function fromFileEntry(entry) {
179 return __awaiter(this, void 0, void 0, function* () {
180 return new Promise((resolve, reject) => {
181 entry.file((file) => {
182 const fwp = toFileWithPath(file, entry.fullPath);
183 resolve(fwp);
184 }, (err) => {
185 reject(err);
186 });
187 });
188 });
189 }
190 //# sourceMappingURL=file-selector.js.map