@theia/filesystem 1.53.0-next.4 → 1.53.0-next.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/file-resource.d.ts +2 -0
  3. package/lib/browser/file-resource.d.ts.map +1 -1
  4. package/lib/browser/file-resource.js +11 -0
  5. package/lib/browser/file-resource.js.map +1 -1
  6. package/lib/browser/file-resource.spec.d.ts +2 -0
  7. package/lib/browser/file-resource.spec.d.ts.map +1 -0
  8. package/lib/browser/file-resource.spec.js +191 -0
  9. package/lib/browser/file-resource.spec.js.map +1 -0
  10. package/lib/browser/file-service.d.ts +1 -0
  11. package/lib/browser/file-service.d.ts.map +1 -1
  12. package/lib/browser/file-service.js +3 -0
  13. package/lib/browser/file-service.js.map +1 -1
  14. package/package.json +4 -4
  15. package/src/browser/breadcrumbs/filepath-breadcrumb.ts +43 -43
  16. package/src/browser/breadcrumbs/filepath-breadcrumbs-container.ts +65 -65
  17. package/src/browser/breadcrumbs/filepath-breadcrumbs-contribution.ts +129 -129
  18. package/src/browser/download/file-download-command-contribution.ts +83 -83
  19. package/src/browser/download/file-download-frontend-module.ts +25 -25
  20. package/src/browser/download/file-download-service.ts +179 -179
  21. package/src/browser/file-dialog/file-dialog-container.ts +67 -67
  22. package/src/browser/file-dialog/file-dialog-hidden-files-renderer.tsx +59 -59
  23. package/src/browser/file-dialog/file-dialog-model.ts +96 -96
  24. package/src/browser/file-dialog/file-dialog-module.ts +44 -44
  25. package/src/browser/file-dialog/file-dialog-service.ts +99 -99
  26. package/src/browser/file-dialog/file-dialog-tree-filters-renderer.tsx +100 -100
  27. package/src/browser/file-dialog/file-dialog-tree.ts +89 -89
  28. package/src/browser/file-dialog/file-dialog-widget.ts +75 -75
  29. package/src/browser/file-dialog/file-dialog.ts +434 -434
  30. package/src/browser/file-dialog/index.ts +20 -20
  31. package/src/browser/file-resource.spec.ts +255 -0
  32. package/src/browser/file-resource.ts +402 -390
  33. package/src/browser/file-selection.ts +44 -44
  34. package/src/browser/file-service.ts +1845 -1841
  35. package/src/browser/file-tree/file-tree-container.ts +36 -36
  36. package/src/browser/file-tree/file-tree-decorator-adapter.ts +159 -159
  37. package/src/browser/file-tree/file-tree-label-provider.ts +53 -53
  38. package/src/browser/file-tree/file-tree-model.ts +212 -212
  39. package/src/browser/file-tree/file-tree-widget.tsx +327 -327
  40. package/src/browser/file-tree/file-tree.ts +183 -183
  41. package/src/browser/file-tree/index.ts +22 -22
  42. package/src/browser/file-upload-service.ts +547 -547
  43. package/src/browser/filesystem-frontend-contribution.ts +396 -396
  44. package/src/browser/filesystem-frontend-module.ts +77 -77
  45. package/src/browser/filesystem-preferences.ts +139 -139
  46. package/src/browser/filesystem-saveable-service.ts +138 -138
  47. package/src/browser/filesystem-watcher-error-handler.ts +60 -60
  48. package/src/browser/index.ts +21 -21
  49. package/src/browser/location/index.ts +18 -18
  50. package/src/browser/location/location-renderer.tsx +406 -406
  51. package/src/browser/location/location-service.ts +22 -22
  52. package/src/browser/remote-file-service-contribution.ts +38 -38
  53. package/src/browser/style/file-dialog.css +208 -208
  54. package/src/browser/style/file-icons.css +64 -64
  55. package/src/browser/style/filepath-breadcrumbs.css +20 -20
  56. package/src/browser/style/index.css +36 -36
  57. package/src/browser-only/browser-only-filesystem-frontend-module.ts +38 -38
  58. package/src/browser-only/browser-only-filesystem-provider-server.ts +32 -32
  59. package/src/browser-only/browserfs-filesystem-initialization.ts +61 -61
  60. package/src/browser-only/browserfs-filesystem-provider.ts +462 -462
  61. package/src/common/delegating-file-system-provider.ts +226 -226
  62. package/src/common/download/README.md +30 -30
  63. package/src/common/download/file-download-data.ts +27 -27
  64. package/src/common/file-upload.ts +17 -17
  65. package/src/common/files.spec.ts +51 -51
  66. package/src/common/files.ts +997 -997
  67. package/src/common/filesystem-utils.spec.ts +411 -411
  68. package/src/common/filesystem-utils.ts +64 -64
  69. package/src/common/filesystem-watcher-protocol.ts +96 -96
  70. package/src/common/filesystem.ts +43 -43
  71. package/src/common/index.ts +18 -18
  72. package/src/common/io.ts +150 -150
  73. package/src/common/remote-file-system-provider.ts +549 -549
  74. package/src/electron-browser/file-dialog/electron-file-dialog-module.ts +24 -24
  75. package/src/electron-browser/file-dialog/electron-file-dialog-service.ts +165 -165
  76. package/src/electron-browser/preload.ts +31 -31
  77. package/src/electron-common/electron-api.ts +55 -55
  78. package/src/electron-main/electron-api-main.ts +78 -78
  79. package/src/electron-main/electron-main-module.ts +23 -23
  80. package/src/node/disk-file-system-provider.spec.ts +142 -142
  81. package/src/node/disk-file-system-provider.ts +915 -915
  82. package/src/node/download/directory-archiver.spec.ts +104 -104
  83. package/src/node/download/directory-archiver.ts +126 -126
  84. package/src/node/download/file-download-backend-module.ts +32 -32
  85. package/src/node/download/file-download-cache.ts +88 -88
  86. package/src/node/download/file-download-endpoint.ts +63 -63
  87. package/src/node/download/file-download-handler.ts +304 -304
  88. package/src/node/download/test/mock-directory-archiver.ts +30 -30
  89. package/src/node/file-change-collection.spec.ts +110 -110
  90. package/src/node/file-change-collection.ts +78 -78
  91. package/src/node/filesystem-backend-module.ts +140 -140
  92. package/src/node/filesystem-watcher-client.ts +72 -72
  93. package/src/node/filesystem-watcher-dispatcher.ts +82 -82
  94. package/src/node/node-file-upload-service.ts +80 -80
  95. package/src/node/nsfw-watcher/index.ts +45 -45
  96. package/src/node/nsfw-watcher/nsfw-filesystem-service.ts +481 -481
  97. package/src/node/nsfw-watcher/nsfw-filesystem-watcher.spec.ts +182 -182
  98. package/src/node/nsfw-watcher/nsfw-options.ts +23 -23
  99. package/src/typings/dom.webkit.d.ts +77 -77
  100. package/src/typings/mv/index.d.ts +21 -21
  101. package/src/typings/nsfw/index.d.ts +18 -18
  102. package/src/typings/trash/index.d.ts +20 -20
@@ -1,547 +1,547 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2019 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- /* eslint-disable @typescript-eslint/no-explicit-any */
18
-
19
- import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
20
- import URI from '@theia/core/lib/common/uri';
21
- import { CancellationTokenSource, CancellationToken, checkCancelled, cancelled, isCancelled } from '@theia/core/lib/common/cancellation';
22
- import { Deferred } from '@theia/core/lib/common/promise-util';
23
- import { MessageService } from '@theia/core/lib/common/message-service';
24
- import { Progress } from '@theia/core/lib/common/message-service-protocol';
25
- import { Endpoint } from '@theia/core/lib/browser/endpoint';
26
- import throttle = require('@theia/core/shared/lodash.throttle');
27
- import { HTTP_FILE_UPLOAD_PATH } from '../common/file-upload';
28
- import { Semaphore } from 'async-mutex';
29
- import { FileSystemPreferences } from './filesystem-preferences';
30
- import { FileService } from './file-service';
31
- import { ConfirmDialog, Dialog } from '@theia/core/lib/browser';
32
- import { nls } from '@theia/core/lib/common/nls';
33
- import { Emitter, Event } from '@theia/core/lib/common/event';
34
-
35
- export const HTTP_UPLOAD_URL: string = new Endpoint({ path: HTTP_FILE_UPLOAD_PATH }).getRestUrl().toString(true);
36
-
37
- export type CustomDataTransfer = Iterable<readonly [string, CustomDataTransferItem]>;
38
-
39
- export interface CustomDataTransferItem {
40
- asFile(): {
41
- readonly id: string;
42
- readonly name: string;
43
- data(): Promise<Uint8Array>;
44
- } | undefined
45
- }
46
- export interface FileUploadParams {
47
- source?: DataTransfer | CustomDataTransfer
48
- progress?: FileUploadProgressParams
49
- onDidUpload?: (uri: string) => void;
50
- leaveInTemp?: boolean // dont move file out of the initial tmp directory
51
- }
52
- export interface FileUploadProgressParams {
53
- text: string
54
- }
55
-
56
- export interface FileUploadResult {
57
- uploaded: string[]
58
- }
59
-
60
- @injectable()
61
- export class FileUploadService {
62
-
63
- static TARGET = 'target';
64
- static UPLOAD = 'upload';
65
-
66
- protected readonly onDidUploadEmitter = new Emitter<string[]>();
67
-
68
- get onDidUpload(): Event<string[]> {
69
- return this.onDidUploadEmitter.event;
70
- }
71
-
72
- protected uploadForm: FileUploadService.Form;
73
- protected deferredUpload?: Deferred<FileUploadResult>;
74
-
75
- @inject(MessageService)
76
- protected readonly messageService: MessageService;
77
-
78
- @inject(FileSystemPreferences)
79
- protected fileSystemPreferences: FileSystemPreferences;
80
-
81
- @inject(FileService)
82
- protected fileService: FileService;
83
-
84
- get maxConcurrentUploads(): number {
85
- const maxConcurrentUploads = this.fileSystemPreferences['files.maxConcurrentUploads'];
86
- return maxConcurrentUploads > 0 ? maxConcurrentUploads : Infinity;
87
- }
88
-
89
- @postConstruct()
90
- protected init(): void {
91
- this.uploadForm = this.createUploadForm();
92
- }
93
-
94
- protected createUploadForm(): FileUploadService.Form {
95
- const targetInput = document.createElement('input');
96
- targetInput.type = 'text';
97
- targetInput.spellcheck = false;
98
- targetInput.name = FileUploadService.TARGET;
99
- targetInput.classList.add('theia-input');
100
-
101
- const fileInput = document.createElement('input');
102
- fileInput.type = 'file';
103
- fileInput.classList.add('theia-input');
104
- fileInput.name = FileUploadService.UPLOAD;
105
- fileInput.multiple = true;
106
-
107
- const form = document.createElement('form');
108
- form.style.display = 'none';
109
- form.enctype = 'multipart/form-data';
110
- form.append(targetInput);
111
- form.append(fileInput);
112
-
113
- document.body.appendChild(form);
114
-
115
- fileInput.addEventListener('change', () => {
116
- if (this.deferredUpload && fileInput.value) {
117
- const source: FileUploadService.Source = new FormData(form);
118
- // clean up to allow upload to the same folder twice
119
- fileInput.value = '';
120
- const targetUri = new URI(<string>source.get(FileUploadService.TARGET));
121
- const { resolve, reject } = this.deferredUpload;
122
- this.deferredUpload = undefined;
123
- const { onDidUpload } = this.uploadForm;
124
- this.withProgress(
125
- (progress, token) => this.uploadAll(targetUri, { source, progress, token, onDidUpload }),
126
- this.uploadForm.progress
127
- ).then(resolve, reject);
128
- }
129
- });
130
- return { targetInput, fileInput };
131
- }
132
-
133
- async upload(targetUri: string | URI, params: FileUploadParams = {}): Promise<FileUploadResult> {
134
- const { source, onDidUpload, leaveInTemp } = params;
135
- if (source) {
136
- return this.withProgress(
137
- (progress, token) => this.uploadAll(
138
- typeof targetUri === 'string' ? new URI(targetUri) : targetUri,
139
- { source, progress, token, leaveInTemp, onDidUpload }
140
- ),
141
- params.progress,
142
- );
143
- }
144
- this.deferredUpload = new Deferred<FileUploadResult>();
145
- this.uploadForm.targetInput.value = String(targetUri);
146
- this.uploadForm.fileInput.click();
147
- this.uploadForm.progress = params.progress;
148
- this.uploadForm.onDidUpload = params.onDidUpload;
149
- return this.deferredUpload.promise;
150
- }
151
-
152
- protected getUploadUrl(): string {
153
- return HTTP_UPLOAD_URL;
154
- }
155
-
156
- protected async uploadAll(targetUri: URI, params: FileUploadService.UploadParams): Promise<FileUploadResult> {
157
- const responses: Promise<void>[] = [];
158
- const status = new Map<File, {
159
- total: number
160
- done: number
161
- uploaded?: boolean
162
- }>();
163
- const result: FileUploadResult = {
164
- uploaded: []
165
- };
166
- /**
167
- * When `false`: display the uploading progress.
168
- * When `true`: display the server-processing progress.
169
- */
170
- let waitingForResponses = false;
171
- const report = throttle(() => {
172
- if (waitingForResponses) {
173
- /** Number of files being processed. */
174
- const total = status.size;
175
- /** Number of files uploaded and processed. */
176
- let done = 0;
177
- for (const item of status.values()) {
178
- if (item.uploaded) {
179
- done += 1;
180
- }
181
- }
182
- params.progress.report({
183
- message: nls.localize('theia/filesystem/processedOutOf', 'Processed {0} out of {1}', done, total),
184
- work: { total, done }
185
- });
186
- } else {
187
- /** Total number of bytes being uploaded. */
188
- let total = 0;
189
- /** Current number of bytes uploaded. */
190
- let done = 0;
191
- for (const item of status.values()) {
192
- total += item.total;
193
- done += item.done;
194
- }
195
- params.progress.report({
196
- message: nls.localize('theia/filesystem/uploadedOutOf', 'Uploaded {0} out of {1}', result.uploaded.length, status.size),
197
- work: { total, done }
198
- });
199
- }
200
- }, 100);
201
- const uploads: Promise<void>[] = [];
202
- const uploadSemaphore = new Semaphore(this.maxConcurrentUploads);
203
- try {
204
- await this.index(targetUri, params.source, {
205
- token: params.token,
206
- progress: params.progress,
207
- accept: async item => {
208
- if (await this.fileService.exists(item.uri) && !await this.confirmOverwrite(item.uri)) {
209
- return;
210
- }
211
- // Track and initialize the file in the status map:
212
- status.set(item.file, { total: item.file.size, done: 0 });
213
- report();
214
- // Don't await here: the semaphore will organize the uploading tasks, not the async indexer.
215
- uploads.push(uploadSemaphore.runExclusive(async () => {
216
- checkCancelled(params.token);
217
- const { upload, response } = this.uploadFile(item.file, item.uri, params.token, params.leaveInTemp, (total, done) => {
218
- const entry = status.get(item.file);
219
- if (entry) {
220
- entry.total = total;
221
- entry.done = done;
222
- report();
223
- }
224
- });
225
- function onError(error: Error): void {
226
- status.delete(item.file);
227
- throw error;
228
- }
229
- responses.push(response
230
- .then(() => {
231
- checkCancelled(params.token);
232
- // Consider the file uploaded once the server sends OK back.
233
- result.uploaded.push(item.uri.toString(true));
234
- const entry = status.get(item.file);
235
- if (entry) {
236
- entry.uploaded = true;
237
- report();
238
- }
239
- })
240
- .catch(onError)
241
- );
242
- // Have the queue wait for the upload only.
243
- return upload
244
- .catch(onError);
245
- }));
246
- }
247
- });
248
- checkCancelled(params.token);
249
- await Promise.all(uploads);
250
- checkCancelled(params.token);
251
- waitingForResponses = true;
252
- report();
253
- await Promise.all(responses);
254
- } catch (error) {
255
- uploadSemaphore.cancel();
256
- if (!isCancelled(error)) {
257
- throw error;
258
- }
259
- }
260
- this.onDidUploadEmitter.fire(result.uploaded);
261
- return result;
262
- }
263
-
264
- protected async confirmOverwrite(fileUri: URI): Promise<boolean> {
265
- const dialog = new ConfirmDialog({
266
- title: nls.localizeByDefault('Replace'),
267
- msg: nls.localizeByDefault("A file or folder with the name '{0}' already exists in the destination folder. Do you want to replace it?", fileUri.path.base),
268
- ok: nls.localizeByDefault('Replace'),
269
- cancel: Dialog.CANCEL
270
- });
271
- return !!await dialog.open();
272
- }
273
-
274
- protected uploadFile(
275
- file: File,
276
- targetUri: URI,
277
- token: CancellationToken,
278
- leaveInTemp: boolean | undefined,
279
- onProgress: (total: number, done: number) => void
280
- ): {
281
- /**
282
- * Promise that resolves once the uploading is finished.
283
- *
284
- * Rejects on network error.
285
- * Rejects if status is not OK (200).
286
- * Rejects if cancelled.
287
- */
288
- upload: Promise<void>
289
- /**
290
- * Promise that resolves after the uploading step, once the server answers back.
291
- *
292
- * Rejects on network error.
293
- * Rejects if status is not OK (200).
294
- * Rejects if cancelled.
295
- */
296
- response: Promise<void>
297
- } {
298
- const data = new FormData();
299
- data.set('uri', targetUri.toString(true));
300
- data.set('file', file);
301
- if (leaveInTemp) {
302
- data.set('leaveInTemp', 'true');
303
- }
304
- // TODO: Use Fetch API once it supports upload monitoring.
305
- const xhr = new XMLHttpRequest();
306
- token.onCancellationRequested(() => xhr.abort());
307
- const upload = new Promise<void>((resolve, reject) => {
308
- this.registerEvents(xhr.upload, unregister => ({
309
- progress: (event: ProgressEvent<XMLHttpRequestEventTarget>) => {
310
- if (event.total === event.loaded) {
311
- unregister();
312
- resolve();
313
- } else {
314
- onProgress(event.total, event.loaded);
315
- }
316
- },
317
- abort: () => {
318
- unregister();
319
- reject(cancelled());
320
- },
321
- error: () => {
322
- unregister();
323
- reject(new Error('POST upload error'));
324
- },
325
- // `load` fires once the response is received, not when the upload is finished.
326
- // `resolve` should be called earlier within `progress` but this is a safety catch.
327
- load: () => {
328
- unregister();
329
- if (xhr.status === 200) {
330
- resolve();
331
- } else {
332
- reject(new Error(`POST request failed: ${xhr.status} ${xhr.statusText}`));
333
- }
334
- },
335
- }));
336
- });
337
- const response = new Promise<void>((resolve, reject) => {
338
- this.registerEvents(xhr, unregister => ({
339
- abort: () => {
340
- unregister();
341
- reject(cancelled());
342
- },
343
- error: () => {
344
- unregister();
345
- reject(new Error('POST request error'));
346
- },
347
- load: () => {
348
- unregister();
349
- if (xhr.status === 200) {
350
- resolve();
351
- } else {
352
- reject(new Error(`POST request failed: ${xhr.status} ${xhr.statusText}`));
353
- }
354
- }
355
- }));
356
- });
357
- xhr.open('POST', this.getUploadUrl(), /* async: */ true);
358
- xhr.send(data);
359
- return {
360
- upload,
361
- response
362
- };
363
- }
364
-
365
- /**
366
- * Utility function to attach events and get a callback to unregister those.
367
- *
368
- * You may not call `unregister` in the same tick as `register` is invoked.
369
- */
370
- protected registerEvents(
371
- target: EventTarget,
372
- register: (unregister: () => void) => Record<string, EventListenerOrEventListenerObject>
373
- ): void {
374
- const events = register(() => {
375
- for (const [event, fn] of Object.entries(events)) {
376
- target.removeEventListener(event, fn);
377
- }
378
- });
379
- for (const [event, fn] of Object.entries(events)) {
380
- target.addEventListener(event, fn);
381
- }
382
- }
383
-
384
- protected async withProgress<T>(
385
- cb: (progress: Progress, token: CancellationToken) => Promise<T>,
386
- { text }: FileUploadProgressParams = { text: nls.localize('theia/filesystem/uploadFiles', 'Uploading Files') }
387
- ): Promise<T> {
388
- const cancellationSource = new CancellationTokenSource();
389
- const { token } = cancellationSource;
390
- const progress = await this.messageService.showProgress(
391
- { text, options: { cancelable: true } },
392
- () => cancellationSource.cancel()
393
- );
394
- try {
395
- return await cb(progress, token);
396
- } finally {
397
- progress.cancel();
398
- }
399
- }
400
-
401
- protected async index(targetUri: URI, source: FileUploadService.Source, context: FileUploadService.Context): Promise<void> {
402
- if (source instanceof FormData) {
403
- await this.indexFormData(targetUri, source, context);
404
- } else if (source instanceof DataTransfer) {
405
- await this.indexDataTransfer(targetUri, source, context);
406
- } else {
407
- await this.indexCustomDataTransfer(targetUri, source, context);
408
- }
409
- }
410
-
411
- protected async indexFormData(targetUri: URI, formData: FormData, context: FileUploadService.Context): Promise<void> {
412
- for (const entry of formData.getAll(FileUploadService.UPLOAD)) {
413
- if (entry instanceof File) {
414
- await this.indexFile(targetUri, entry, context);
415
- }
416
- }
417
- }
418
-
419
- protected async indexDataTransfer(targetUri: URI, dataTransfer: DataTransfer, context: FileUploadService.Context): Promise<void> {
420
- checkCancelled(context.token);
421
- if (dataTransfer.items) {
422
- await this.indexDataTransferItemList(targetUri, dataTransfer.items, context);
423
- } else {
424
- await this.indexFileList(targetUri, dataTransfer.files, context);
425
- }
426
- }
427
-
428
- protected async indexCustomDataTransfer(targetUri: URI, dataTransfer: CustomDataTransfer, context: FileUploadService.Context): Promise<void> {
429
- for (const [_, item] of dataTransfer) {
430
- const fileInfo = item.asFile();
431
- if (fileInfo) {
432
- await this.indexFile(targetUri, new File([await fileInfo.data()], fileInfo.id), context);
433
- }
434
- }
435
- }
436
-
437
- protected async indexFileList(targetUri: URI, files: FileList, context: FileUploadService.Context): Promise<void> {
438
- for (let i = 0; i < files.length; i++) {
439
- const file = files[i];
440
- if (file) {
441
- await this.indexFile(targetUri, file, context);
442
- }
443
- }
444
- }
445
-
446
- protected async indexFile(targetUri: URI, file: File, context: FileUploadService.Context): Promise<void> {
447
- await context.accept({
448
- uri: targetUri.resolve(file.name),
449
- file
450
- });
451
- }
452
-
453
- protected async indexDataTransferItemList(targetUri: URI, items: DataTransferItemList, context: FileUploadService.Context): Promise<void> {
454
- checkCancelled(context.token);
455
- const entries: WebKitEntry[] = [];
456
- for (let i = 0; i < items.length; i++) {
457
- const entry = items[i].webkitGetAsEntry() as WebKitEntry;
458
- entries.push(entry);
459
- }
460
- await this.indexEntries(targetUri, entries, context);
461
-
462
- }
463
-
464
- protected async indexEntry(targetUri: URI, entry: WebKitEntry | null, context: FileUploadService.Context): Promise<void> {
465
- checkCancelled(context.token);
466
- if (!entry) {
467
- return;
468
- }
469
- if (entry.isDirectory) {
470
- await this.indexDirectoryEntry(targetUri, entry as WebKitDirectoryEntry, context);
471
- } else {
472
- await this.indexFileEntry(targetUri, entry as WebKitFileEntry, context);
473
- }
474
- }
475
-
476
- /**
477
- * Read all entries within a folder by block of 100 files or folders until the
478
- * whole folder has been read.
479
- */
480
- protected async indexDirectoryEntry(targetUri: URI, entry: WebKitDirectoryEntry, context: FileUploadService.Context): Promise<void> {
481
- checkCancelled(context.token);
482
- const newTargetUri = targetUri.resolve(entry.name);
483
- return new Promise<void>(async (resolve, reject) => {
484
- const reader = entry.createReader();
485
- const getEntries = () => reader.readEntries(async results => {
486
- try {
487
- if (!context.token.isCancellationRequested && results && results.length) {
488
- await this.indexEntries(newTargetUri, results, context);
489
- getEntries(); // loop to read all getEntries
490
- } else {
491
- resolve();
492
- }
493
- } catch (e) {
494
- reject(e);
495
- }
496
- }, reject);
497
- getEntries();
498
- });
499
- }
500
-
501
- protected async indexEntries(targetUri: URI, entries: WebKitEntry[], context: FileUploadService.Context): Promise<void> {
502
- checkCancelled(context.token);
503
- for (let i = 0; i < entries.length; i++) {
504
- await this.indexEntry(targetUri, entries[i], context);
505
- }
506
- }
507
-
508
- protected async indexFileEntry(targetUri: URI, entry: WebKitFileEntry, context: FileUploadService.Context): Promise<void> {
509
- await new Promise<void>((resolve, reject) => {
510
- try {
511
- entry.file(
512
- file => this.indexFile(targetUri, file, context).then(resolve, reject),
513
- reject,
514
- );
515
- } catch (e) {
516
- reject(e);
517
- }
518
- });
519
- }
520
-
521
- }
522
-
523
- export namespace FileUploadService {
524
- export type Source = FormData | DataTransfer | CustomDataTransfer;
525
- export interface UploadEntry {
526
- file: File
527
- uri: URI
528
- }
529
- export interface Context {
530
- progress: Progress
531
- token: CancellationToken
532
- accept: (entry: UploadEntry) => Promise<void>
533
- }
534
- export interface Form {
535
- targetInput: HTMLInputElement
536
- fileInput: HTMLInputElement
537
- progress?: FileUploadProgressParams
538
- onDidUpload?: (uri: string) => void
539
- }
540
- export interface UploadParams {
541
- source: FileUploadService.Source,
542
- progress: Progress,
543
- token: CancellationToken,
544
- onDidUpload?: (uri: string) => void,
545
- leaveInTemp?: boolean
546
- }
547
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2019 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ /* eslint-disable @typescript-eslint/no-explicit-any */
18
+
19
+ import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
20
+ import URI from '@theia/core/lib/common/uri';
21
+ import { CancellationTokenSource, CancellationToken, checkCancelled, cancelled, isCancelled } from '@theia/core/lib/common/cancellation';
22
+ import { Deferred } from '@theia/core/lib/common/promise-util';
23
+ import { MessageService } from '@theia/core/lib/common/message-service';
24
+ import { Progress } from '@theia/core/lib/common/message-service-protocol';
25
+ import { Endpoint } from '@theia/core/lib/browser/endpoint';
26
+ import throttle = require('@theia/core/shared/lodash.throttle');
27
+ import { HTTP_FILE_UPLOAD_PATH } from '../common/file-upload';
28
+ import { Semaphore } from 'async-mutex';
29
+ import { FileSystemPreferences } from './filesystem-preferences';
30
+ import { FileService } from './file-service';
31
+ import { ConfirmDialog, Dialog } from '@theia/core/lib/browser';
32
+ import { nls } from '@theia/core/lib/common/nls';
33
+ import { Emitter, Event } from '@theia/core/lib/common/event';
34
+
35
+ export const HTTP_UPLOAD_URL: string = new Endpoint({ path: HTTP_FILE_UPLOAD_PATH }).getRestUrl().toString(true);
36
+
37
+ export type CustomDataTransfer = Iterable<readonly [string, CustomDataTransferItem]>;
38
+
39
+ export interface CustomDataTransferItem {
40
+ asFile(): {
41
+ readonly id: string;
42
+ readonly name: string;
43
+ data(): Promise<Uint8Array>;
44
+ } | undefined
45
+ }
46
+ export interface FileUploadParams {
47
+ source?: DataTransfer | CustomDataTransfer
48
+ progress?: FileUploadProgressParams
49
+ onDidUpload?: (uri: string) => void;
50
+ leaveInTemp?: boolean // dont move file out of the initial tmp directory
51
+ }
52
+ export interface FileUploadProgressParams {
53
+ text: string
54
+ }
55
+
56
+ export interface FileUploadResult {
57
+ uploaded: string[]
58
+ }
59
+
60
+ @injectable()
61
+ export class FileUploadService {
62
+
63
+ static TARGET = 'target';
64
+ static UPLOAD = 'upload';
65
+
66
+ protected readonly onDidUploadEmitter = new Emitter<string[]>();
67
+
68
+ get onDidUpload(): Event<string[]> {
69
+ return this.onDidUploadEmitter.event;
70
+ }
71
+
72
+ protected uploadForm: FileUploadService.Form;
73
+ protected deferredUpload?: Deferred<FileUploadResult>;
74
+
75
+ @inject(MessageService)
76
+ protected readonly messageService: MessageService;
77
+
78
+ @inject(FileSystemPreferences)
79
+ protected fileSystemPreferences: FileSystemPreferences;
80
+
81
+ @inject(FileService)
82
+ protected fileService: FileService;
83
+
84
+ get maxConcurrentUploads(): number {
85
+ const maxConcurrentUploads = this.fileSystemPreferences['files.maxConcurrentUploads'];
86
+ return maxConcurrentUploads > 0 ? maxConcurrentUploads : Infinity;
87
+ }
88
+
89
+ @postConstruct()
90
+ protected init(): void {
91
+ this.uploadForm = this.createUploadForm();
92
+ }
93
+
94
+ protected createUploadForm(): FileUploadService.Form {
95
+ const targetInput = document.createElement('input');
96
+ targetInput.type = 'text';
97
+ targetInput.spellcheck = false;
98
+ targetInput.name = FileUploadService.TARGET;
99
+ targetInput.classList.add('theia-input');
100
+
101
+ const fileInput = document.createElement('input');
102
+ fileInput.type = 'file';
103
+ fileInput.classList.add('theia-input');
104
+ fileInput.name = FileUploadService.UPLOAD;
105
+ fileInput.multiple = true;
106
+
107
+ const form = document.createElement('form');
108
+ form.style.display = 'none';
109
+ form.enctype = 'multipart/form-data';
110
+ form.append(targetInput);
111
+ form.append(fileInput);
112
+
113
+ document.body.appendChild(form);
114
+
115
+ fileInput.addEventListener('change', () => {
116
+ if (this.deferredUpload && fileInput.value) {
117
+ const source: FileUploadService.Source = new FormData(form);
118
+ // clean up to allow upload to the same folder twice
119
+ fileInput.value = '';
120
+ const targetUri = new URI(<string>source.get(FileUploadService.TARGET));
121
+ const { resolve, reject } = this.deferredUpload;
122
+ this.deferredUpload = undefined;
123
+ const { onDidUpload } = this.uploadForm;
124
+ this.withProgress(
125
+ (progress, token) => this.uploadAll(targetUri, { source, progress, token, onDidUpload }),
126
+ this.uploadForm.progress
127
+ ).then(resolve, reject);
128
+ }
129
+ });
130
+ return { targetInput, fileInput };
131
+ }
132
+
133
+ async upload(targetUri: string | URI, params: FileUploadParams = {}): Promise<FileUploadResult> {
134
+ const { source, onDidUpload, leaveInTemp } = params;
135
+ if (source) {
136
+ return this.withProgress(
137
+ (progress, token) => this.uploadAll(
138
+ typeof targetUri === 'string' ? new URI(targetUri) : targetUri,
139
+ { source, progress, token, leaveInTemp, onDidUpload }
140
+ ),
141
+ params.progress,
142
+ );
143
+ }
144
+ this.deferredUpload = new Deferred<FileUploadResult>();
145
+ this.uploadForm.targetInput.value = String(targetUri);
146
+ this.uploadForm.fileInput.click();
147
+ this.uploadForm.progress = params.progress;
148
+ this.uploadForm.onDidUpload = params.onDidUpload;
149
+ return this.deferredUpload.promise;
150
+ }
151
+
152
+ protected getUploadUrl(): string {
153
+ return HTTP_UPLOAD_URL;
154
+ }
155
+
156
+ protected async uploadAll(targetUri: URI, params: FileUploadService.UploadParams): Promise<FileUploadResult> {
157
+ const responses: Promise<void>[] = [];
158
+ const status = new Map<File, {
159
+ total: number
160
+ done: number
161
+ uploaded?: boolean
162
+ }>();
163
+ const result: FileUploadResult = {
164
+ uploaded: []
165
+ };
166
+ /**
167
+ * When `false`: display the uploading progress.
168
+ * When `true`: display the server-processing progress.
169
+ */
170
+ let waitingForResponses = false;
171
+ const report = throttle(() => {
172
+ if (waitingForResponses) {
173
+ /** Number of files being processed. */
174
+ const total = status.size;
175
+ /** Number of files uploaded and processed. */
176
+ let done = 0;
177
+ for (const item of status.values()) {
178
+ if (item.uploaded) {
179
+ done += 1;
180
+ }
181
+ }
182
+ params.progress.report({
183
+ message: nls.localize('theia/filesystem/processedOutOf', 'Processed {0} out of {1}', done, total),
184
+ work: { total, done }
185
+ });
186
+ } else {
187
+ /** Total number of bytes being uploaded. */
188
+ let total = 0;
189
+ /** Current number of bytes uploaded. */
190
+ let done = 0;
191
+ for (const item of status.values()) {
192
+ total += item.total;
193
+ done += item.done;
194
+ }
195
+ params.progress.report({
196
+ message: nls.localize('theia/filesystem/uploadedOutOf', 'Uploaded {0} out of {1}', result.uploaded.length, status.size),
197
+ work: { total, done }
198
+ });
199
+ }
200
+ }, 100);
201
+ const uploads: Promise<void>[] = [];
202
+ const uploadSemaphore = new Semaphore(this.maxConcurrentUploads);
203
+ try {
204
+ await this.index(targetUri, params.source, {
205
+ token: params.token,
206
+ progress: params.progress,
207
+ accept: async item => {
208
+ if (await this.fileService.exists(item.uri) && !await this.confirmOverwrite(item.uri)) {
209
+ return;
210
+ }
211
+ // Track and initialize the file in the status map:
212
+ status.set(item.file, { total: item.file.size, done: 0 });
213
+ report();
214
+ // Don't await here: the semaphore will organize the uploading tasks, not the async indexer.
215
+ uploads.push(uploadSemaphore.runExclusive(async () => {
216
+ checkCancelled(params.token);
217
+ const { upload, response } = this.uploadFile(item.file, item.uri, params.token, params.leaveInTemp, (total, done) => {
218
+ const entry = status.get(item.file);
219
+ if (entry) {
220
+ entry.total = total;
221
+ entry.done = done;
222
+ report();
223
+ }
224
+ });
225
+ function onError(error: Error): void {
226
+ status.delete(item.file);
227
+ throw error;
228
+ }
229
+ responses.push(response
230
+ .then(() => {
231
+ checkCancelled(params.token);
232
+ // Consider the file uploaded once the server sends OK back.
233
+ result.uploaded.push(item.uri.toString(true));
234
+ const entry = status.get(item.file);
235
+ if (entry) {
236
+ entry.uploaded = true;
237
+ report();
238
+ }
239
+ })
240
+ .catch(onError)
241
+ );
242
+ // Have the queue wait for the upload only.
243
+ return upload
244
+ .catch(onError);
245
+ }));
246
+ }
247
+ });
248
+ checkCancelled(params.token);
249
+ await Promise.all(uploads);
250
+ checkCancelled(params.token);
251
+ waitingForResponses = true;
252
+ report();
253
+ await Promise.all(responses);
254
+ } catch (error) {
255
+ uploadSemaphore.cancel();
256
+ if (!isCancelled(error)) {
257
+ throw error;
258
+ }
259
+ }
260
+ this.onDidUploadEmitter.fire(result.uploaded);
261
+ return result;
262
+ }
263
+
264
+ protected async confirmOverwrite(fileUri: URI): Promise<boolean> {
265
+ const dialog = new ConfirmDialog({
266
+ title: nls.localizeByDefault('Replace'),
267
+ msg: nls.localizeByDefault("A file or folder with the name '{0}' already exists in the destination folder. Do you want to replace it?", fileUri.path.base),
268
+ ok: nls.localizeByDefault('Replace'),
269
+ cancel: Dialog.CANCEL
270
+ });
271
+ return !!await dialog.open();
272
+ }
273
+
274
+ protected uploadFile(
275
+ file: File,
276
+ targetUri: URI,
277
+ token: CancellationToken,
278
+ leaveInTemp: boolean | undefined,
279
+ onProgress: (total: number, done: number) => void
280
+ ): {
281
+ /**
282
+ * Promise that resolves once the uploading is finished.
283
+ *
284
+ * Rejects on network error.
285
+ * Rejects if status is not OK (200).
286
+ * Rejects if cancelled.
287
+ */
288
+ upload: Promise<void>
289
+ /**
290
+ * Promise that resolves after the uploading step, once the server answers back.
291
+ *
292
+ * Rejects on network error.
293
+ * Rejects if status is not OK (200).
294
+ * Rejects if cancelled.
295
+ */
296
+ response: Promise<void>
297
+ } {
298
+ const data = new FormData();
299
+ data.set('uri', targetUri.toString(true));
300
+ data.set('file', file);
301
+ if (leaveInTemp) {
302
+ data.set('leaveInTemp', 'true');
303
+ }
304
+ // TODO: Use Fetch API once it supports upload monitoring.
305
+ const xhr = new XMLHttpRequest();
306
+ token.onCancellationRequested(() => xhr.abort());
307
+ const upload = new Promise<void>((resolve, reject) => {
308
+ this.registerEvents(xhr.upload, unregister => ({
309
+ progress: (event: ProgressEvent<XMLHttpRequestEventTarget>) => {
310
+ if (event.total === event.loaded) {
311
+ unregister();
312
+ resolve();
313
+ } else {
314
+ onProgress(event.total, event.loaded);
315
+ }
316
+ },
317
+ abort: () => {
318
+ unregister();
319
+ reject(cancelled());
320
+ },
321
+ error: () => {
322
+ unregister();
323
+ reject(new Error('POST upload error'));
324
+ },
325
+ // `load` fires once the response is received, not when the upload is finished.
326
+ // `resolve` should be called earlier within `progress` but this is a safety catch.
327
+ load: () => {
328
+ unregister();
329
+ if (xhr.status === 200) {
330
+ resolve();
331
+ } else {
332
+ reject(new Error(`POST request failed: ${xhr.status} ${xhr.statusText}`));
333
+ }
334
+ },
335
+ }));
336
+ });
337
+ const response = new Promise<void>((resolve, reject) => {
338
+ this.registerEvents(xhr, unregister => ({
339
+ abort: () => {
340
+ unregister();
341
+ reject(cancelled());
342
+ },
343
+ error: () => {
344
+ unregister();
345
+ reject(new Error('POST request error'));
346
+ },
347
+ load: () => {
348
+ unregister();
349
+ if (xhr.status === 200) {
350
+ resolve();
351
+ } else {
352
+ reject(new Error(`POST request failed: ${xhr.status} ${xhr.statusText}`));
353
+ }
354
+ }
355
+ }));
356
+ });
357
+ xhr.open('POST', this.getUploadUrl(), /* async: */ true);
358
+ xhr.send(data);
359
+ return {
360
+ upload,
361
+ response
362
+ };
363
+ }
364
+
365
+ /**
366
+ * Utility function to attach events and get a callback to unregister those.
367
+ *
368
+ * You may not call `unregister` in the same tick as `register` is invoked.
369
+ */
370
+ protected registerEvents(
371
+ target: EventTarget,
372
+ register: (unregister: () => void) => Record<string, EventListenerOrEventListenerObject>
373
+ ): void {
374
+ const events = register(() => {
375
+ for (const [event, fn] of Object.entries(events)) {
376
+ target.removeEventListener(event, fn);
377
+ }
378
+ });
379
+ for (const [event, fn] of Object.entries(events)) {
380
+ target.addEventListener(event, fn);
381
+ }
382
+ }
383
+
384
+ protected async withProgress<T>(
385
+ cb: (progress: Progress, token: CancellationToken) => Promise<T>,
386
+ { text }: FileUploadProgressParams = { text: nls.localize('theia/filesystem/uploadFiles', 'Uploading Files') }
387
+ ): Promise<T> {
388
+ const cancellationSource = new CancellationTokenSource();
389
+ const { token } = cancellationSource;
390
+ const progress = await this.messageService.showProgress(
391
+ { text, options: { cancelable: true } },
392
+ () => cancellationSource.cancel()
393
+ );
394
+ try {
395
+ return await cb(progress, token);
396
+ } finally {
397
+ progress.cancel();
398
+ }
399
+ }
400
+
401
+ protected async index(targetUri: URI, source: FileUploadService.Source, context: FileUploadService.Context): Promise<void> {
402
+ if (source instanceof FormData) {
403
+ await this.indexFormData(targetUri, source, context);
404
+ } else if (source instanceof DataTransfer) {
405
+ await this.indexDataTransfer(targetUri, source, context);
406
+ } else {
407
+ await this.indexCustomDataTransfer(targetUri, source, context);
408
+ }
409
+ }
410
+
411
+ protected async indexFormData(targetUri: URI, formData: FormData, context: FileUploadService.Context): Promise<void> {
412
+ for (const entry of formData.getAll(FileUploadService.UPLOAD)) {
413
+ if (entry instanceof File) {
414
+ await this.indexFile(targetUri, entry, context);
415
+ }
416
+ }
417
+ }
418
+
419
+ protected async indexDataTransfer(targetUri: URI, dataTransfer: DataTransfer, context: FileUploadService.Context): Promise<void> {
420
+ checkCancelled(context.token);
421
+ if (dataTransfer.items) {
422
+ await this.indexDataTransferItemList(targetUri, dataTransfer.items, context);
423
+ } else {
424
+ await this.indexFileList(targetUri, dataTransfer.files, context);
425
+ }
426
+ }
427
+
428
+ protected async indexCustomDataTransfer(targetUri: URI, dataTransfer: CustomDataTransfer, context: FileUploadService.Context): Promise<void> {
429
+ for (const [_, item] of dataTransfer) {
430
+ const fileInfo = item.asFile();
431
+ if (fileInfo) {
432
+ await this.indexFile(targetUri, new File([await fileInfo.data()], fileInfo.id), context);
433
+ }
434
+ }
435
+ }
436
+
437
+ protected async indexFileList(targetUri: URI, files: FileList, context: FileUploadService.Context): Promise<void> {
438
+ for (let i = 0; i < files.length; i++) {
439
+ const file = files[i];
440
+ if (file) {
441
+ await this.indexFile(targetUri, file, context);
442
+ }
443
+ }
444
+ }
445
+
446
+ protected async indexFile(targetUri: URI, file: File, context: FileUploadService.Context): Promise<void> {
447
+ await context.accept({
448
+ uri: targetUri.resolve(file.name),
449
+ file
450
+ });
451
+ }
452
+
453
+ protected async indexDataTransferItemList(targetUri: URI, items: DataTransferItemList, context: FileUploadService.Context): Promise<void> {
454
+ checkCancelled(context.token);
455
+ const entries: WebKitEntry[] = [];
456
+ for (let i = 0; i < items.length; i++) {
457
+ const entry = items[i].webkitGetAsEntry() as WebKitEntry;
458
+ entries.push(entry);
459
+ }
460
+ await this.indexEntries(targetUri, entries, context);
461
+
462
+ }
463
+
464
+ protected async indexEntry(targetUri: URI, entry: WebKitEntry | null, context: FileUploadService.Context): Promise<void> {
465
+ checkCancelled(context.token);
466
+ if (!entry) {
467
+ return;
468
+ }
469
+ if (entry.isDirectory) {
470
+ await this.indexDirectoryEntry(targetUri, entry as WebKitDirectoryEntry, context);
471
+ } else {
472
+ await this.indexFileEntry(targetUri, entry as WebKitFileEntry, context);
473
+ }
474
+ }
475
+
476
+ /**
477
+ * Read all entries within a folder by block of 100 files or folders until the
478
+ * whole folder has been read.
479
+ */
480
+ protected async indexDirectoryEntry(targetUri: URI, entry: WebKitDirectoryEntry, context: FileUploadService.Context): Promise<void> {
481
+ checkCancelled(context.token);
482
+ const newTargetUri = targetUri.resolve(entry.name);
483
+ return new Promise<void>(async (resolve, reject) => {
484
+ const reader = entry.createReader();
485
+ const getEntries = () => reader.readEntries(async results => {
486
+ try {
487
+ if (!context.token.isCancellationRequested && results && results.length) {
488
+ await this.indexEntries(newTargetUri, results, context);
489
+ getEntries(); // loop to read all getEntries
490
+ } else {
491
+ resolve();
492
+ }
493
+ } catch (e) {
494
+ reject(e);
495
+ }
496
+ }, reject);
497
+ getEntries();
498
+ });
499
+ }
500
+
501
+ protected async indexEntries(targetUri: URI, entries: WebKitEntry[], context: FileUploadService.Context): Promise<void> {
502
+ checkCancelled(context.token);
503
+ for (let i = 0; i < entries.length; i++) {
504
+ await this.indexEntry(targetUri, entries[i], context);
505
+ }
506
+ }
507
+
508
+ protected async indexFileEntry(targetUri: URI, entry: WebKitFileEntry, context: FileUploadService.Context): Promise<void> {
509
+ await new Promise<void>((resolve, reject) => {
510
+ try {
511
+ entry.file(
512
+ file => this.indexFile(targetUri, file, context).then(resolve, reject),
513
+ reject,
514
+ );
515
+ } catch (e) {
516
+ reject(e);
517
+ }
518
+ });
519
+ }
520
+
521
+ }
522
+
523
+ export namespace FileUploadService {
524
+ export type Source = FormData | DataTransfer | CustomDataTransfer;
525
+ export interface UploadEntry {
526
+ file: File
527
+ uri: URI
528
+ }
529
+ export interface Context {
530
+ progress: Progress
531
+ token: CancellationToken
532
+ accept: (entry: UploadEntry) => Promise<void>
533
+ }
534
+ export interface Form {
535
+ targetInput: HTMLInputElement
536
+ fileInput: HTMLInputElement
537
+ progress?: FileUploadProgressParams
538
+ onDidUpload?: (uri: string) => void
539
+ }
540
+ export interface UploadParams {
541
+ source: FileUploadService.Source,
542
+ progress: Progress,
543
+ token: CancellationToken,
544
+ onDidUpload?: (uri: string) => void,
545
+ leaveInTemp?: boolean
546
+ }
547
+ }