@theia/filesystem 1.53.0-next.5 → 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,304 +1,304 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2018 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
- import * as os from 'os';
18
- import * as fs from '@theia/core/shared/fs-extra';
19
- import * as path from 'path';
20
- import { generateUuid } from '@theia/core/lib/common/uuid';
21
- import { Request, Response } from '@theia/core/shared/express';
22
- import { inject, injectable } from '@theia/core/shared/inversify';
23
- import { OK, BAD_REQUEST, METHOD_NOT_ALLOWED, NOT_FOUND, INTERNAL_SERVER_ERROR, REQUESTED_RANGE_NOT_SATISFIABLE, PARTIAL_CONTENT } from 'http-status-codes';
24
- import URI from '@theia/core/lib/common/uri';
25
- import { isEmpty } from '@theia/core/lib/common/objects';
26
- import { ILogger } from '@theia/core/lib/common/logger';
27
- import { FileUri } from '@theia/core/lib/common/file-uri';
28
- import { DirectoryArchiver } from './directory-archiver';
29
- import { FileDownloadData } from '../../common/download/file-download-data';
30
- import { FileDownloadCache, DownloadStorageItem } from './file-download-cache';
31
-
32
- interface PrepareDownloadOptions {
33
- filePath: string;
34
- downloadId: string;
35
- remove: boolean;
36
- root?: string;
37
- }
38
-
39
- @injectable()
40
- export abstract class FileDownloadHandler {
41
-
42
- @inject(ILogger)
43
- protected readonly logger: ILogger;
44
-
45
- @inject(DirectoryArchiver)
46
- protected readonly directoryArchiver: DirectoryArchiver;
47
-
48
- @inject(FileDownloadCache)
49
- protected readonly fileDownloadCache: FileDownloadCache;
50
-
51
- public abstract handle(request: Request, response: Response): Promise<void>;
52
-
53
- /**
54
- * Prepares the file and the link for download
55
- */
56
- protected async prepareDownload(request: Request, response: Response, options: PrepareDownloadOptions): Promise<void> {
57
- const name = path.basename(options.filePath);
58
- try {
59
- await fs.access(options.filePath, fs.constants.R_OK);
60
- const stat = await fs.stat(options.filePath);
61
- this.fileDownloadCache.addDownload(options.downloadId, { file: options.filePath, remove: options.remove, size: stat.size, root: options.root });
62
- // do not send filePath but instead use the downloadId
63
- const data = { name, id: options.downloadId };
64
- response.status(OK).send(data).end();
65
- } catch (e) {
66
- this.handleError(response, e, INTERNAL_SERVER_ERROR);
67
- }
68
- }
69
-
70
- protected async download(request: Request, response: Response, downloadInfo: DownloadStorageItem, id: string): Promise<void> {
71
- const filePath = downloadInfo.file;
72
- const statSize = downloadInfo.size;
73
- // this sets the content-disposition and content-type automatically
74
- response.attachment(filePath);
75
- try {
76
- await fs.access(filePath, fs.constants.R_OK);
77
- response.setHeader('Accept-Ranges', 'bytes');
78
- // parse range header and combine multiple ranges
79
- const range = this.parseRangeHeader(request.headers['range'], statSize);
80
- if (!range) {
81
- response.setHeader('Content-Length', statSize);
82
- this.streamDownload(OK, response, fs.createReadStream(filePath), id);
83
- } else {
84
- const rangeStart = range.start;
85
- const rangeEnd = range.end;
86
- if (rangeStart >= statSize || rangeEnd >= statSize) {
87
- response.setHeader('Content-Range', `bytes */${statSize}`);
88
- // Return the 416 'Requested Range Not Satisfiable'.
89
- response.status(REQUESTED_RANGE_NOT_SATISFIABLE).end();
90
- return;
91
- }
92
- response.setHeader('Content-Range', `bytes ${rangeStart}-${rangeEnd}/${statSize}`);
93
- response.setHeader('Content-Length', rangeStart === rangeEnd ? 0 : (rangeEnd - rangeStart + 1));
94
- response.setHeader('Cache-Control', 'no-cache');
95
- this.streamDownload(PARTIAL_CONTENT, response, fs.createReadStream(filePath, { start: rangeStart, end: rangeEnd }), id);
96
- }
97
- } catch (e) {
98
- this.fileDownloadCache.deleteDownload(id);
99
- this.handleError(response, e, INTERNAL_SERVER_ERROR);
100
- }
101
- }
102
- /**
103
- * Streams the file and pipe it to the Response to avoid any OOM issues
104
- */
105
- protected streamDownload(status: number, response: Response, stream: fs.ReadStream, id: string): void {
106
- response.status(status);
107
- stream.on('error', error => {
108
- this.fileDownloadCache.deleteDownload(id);
109
- this.handleError(response, error, INTERNAL_SERVER_ERROR);
110
- });
111
- response.on('error', error => {
112
- this.fileDownloadCache.deleteDownload(id);
113
- this.handleError(response, error, INTERNAL_SERVER_ERROR);
114
- });
115
- response.on('close', () => {
116
- stream.destroy();
117
- });
118
- stream.pipe(response);
119
- }
120
- protected parseRangeHeader(range: string | string[] | undefined, statSize: number): { start: number, end: number } | undefined {
121
- if (!range || range.length === 0 || Array.isArray(range)) {
122
- return;
123
- }
124
- const index = range.indexOf('=');
125
- if (index === -1) {
126
- return;
127
- }
128
- const rangeType = range.slice(0, index);
129
- if (rangeType !== 'bytes') {
130
- return;
131
- }
132
- const [start, end] = range.slice(index + 1).split('-').map(r => parseInt(r, 10));
133
- return {
134
- start: isNaN(start) ? 0 : start,
135
- end: (isNaN(end) || end > statSize - 1) ? (statSize - 1) : end
136
- };
137
- }
138
- protected async archive(inputPath: string, outputPath: string = path.join(os.tmpdir(), generateUuid()), entries?: string[]): Promise<string> {
139
- await this.directoryArchiver.archive(inputPath, outputPath, entries);
140
- return outputPath;
141
- }
142
-
143
- protected async createTempDir(downloadId: string = generateUuid()): Promise<string> {
144
- const outputPath = path.join(os.tmpdir(), downloadId);
145
- await fs.mkdir(outputPath);
146
- return outputPath;
147
- }
148
-
149
- protected async handleError(response: Response, reason: string | Error, status: number = INTERNAL_SERVER_ERROR): Promise<void> {
150
- this.logger.error(reason);
151
- response.status(status).send('Unable to download file.').end();
152
- }
153
-
154
- }
155
-
156
- export namespace FileDownloadHandler {
157
- export const SINGLE: symbol = Symbol('single');
158
- export const MULTI: symbol = Symbol('multi');
159
- export const DOWNLOAD_LINK: symbol = Symbol('download');
160
- }
161
-
162
- @injectable()
163
- export class DownloadLinkHandler extends FileDownloadHandler {
164
-
165
- async handle(request: Request, response: Response): Promise<void> {
166
- const { method, query } = request;
167
- if (method !== 'GET' && method !== 'HEAD') {
168
- this.handleError(response, `Unexpected HTTP method. Expected GET got '${method}' instead.`, METHOD_NOT_ALLOWED);
169
- return;
170
- }
171
- if (query === undefined || query.id === undefined || typeof query.id !== 'string') {
172
- this.handleError(response, `Cannot access the 'id' query from the request. The query was: ${JSON.stringify(query)}.`, BAD_REQUEST);
173
- return;
174
- }
175
- const cancelDownload = query.cancel;
176
- const downloadInfo = this.fileDownloadCache.getDownload(query.id);
177
- if (!downloadInfo) {
178
- this.handleError(response, `Cannot find the file from the request. The query was: ${JSON.stringify(query)}.`, NOT_FOUND);
179
- return;
180
- }
181
- // allow head request to determine the content length for parallel downloaders
182
- if (method === 'HEAD') {
183
- response.setHeader('Content-Length', downloadInfo.size);
184
- response.status(OK).end();
185
- return;
186
- }
187
- if (!cancelDownload) {
188
- this.download(request, response, downloadInfo, query.id);
189
- } else {
190
- this.logger.info('Download', query.id, 'has been cancelled');
191
- this.fileDownloadCache.deleteDownload(query.id);
192
- }
193
- }
194
- }
195
-
196
- @injectable()
197
- export class SingleFileDownloadHandler extends FileDownloadHandler {
198
-
199
- async handle(request: Request, response: Response): Promise<void> {
200
- const { method, body, query } = request;
201
- if (method !== 'GET') {
202
- this.handleError(response, `Unexpected HTTP method. Expected GET got '${method}' instead.`, METHOD_NOT_ALLOWED);
203
- return;
204
- }
205
- if (body !== undefined && !isEmpty(body)) {
206
- this.handleError(response, `The request body must either undefined or empty when downloading a single file. The body was: ${JSON.stringify(body)}.`, BAD_REQUEST);
207
- return;
208
- }
209
- if (query === undefined || query.uri === undefined || typeof query.uri !== 'string') {
210
- this.handleError(response, `Cannot access the 'uri' query from the request. The query was: ${JSON.stringify(query)}.`, BAD_REQUEST);
211
- return;
212
- }
213
- const uri = new URI(query.uri).toString(true);
214
- const filePath = FileUri.fsPath(uri);
215
-
216
- let stat: fs.Stats;
217
- try {
218
- stat = await fs.stat(filePath);
219
- } catch {
220
- this.handleError(response, `The file does not exist. URI: ${uri}.`, NOT_FOUND);
221
- return;
222
- }
223
- try {
224
- const downloadId = generateUuid();
225
- const options: PrepareDownloadOptions = { filePath, downloadId, remove: false };
226
- if (!stat.isDirectory()) {
227
- await this.prepareDownload(request, response, options);
228
- } else {
229
- const outputRootPath = await this.createTempDir(downloadId);
230
- const outputPath = path.join(outputRootPath, `${path.basename(filePath)}.tar`);
231
- await this.archive(filePath, outputPath);
232
- options.filePath = outputPath;
233
- options.remove = true;
234
- options.root = outputRootPath;
235
- await this.prepareDownload(request, response, options);
236
- }
237
- } catch (e) {
238
- this.handleError(response, e, INTERNAL_SERVER_ERROR);
239
- }
240
- }
241
-
242
- }
243
-
244
- @injectable()
245
- export class MultiFileDownloadHandler extends FileDownloadHandler {
246
-
247
- async handle(request: Request, response: Response): Promise<void> {
248
- const { method, body } = request;
249
- if (method !== 'PUT') {
250
- this.handleError(response, `Unexpected HTTP method. Expected PUT got '${method}' instead.`, METHOD_NOT_ALLOWED);
251
- return;
252
- }
253
- if (body === undefined) {
254
- this.handleError(response, 'The request body must be defined when downloading multiple files.', BAD_REQUEST);
255
- return;
256
- }
257
- if (!FileDownloadData.is(body)) {
258
- this.handleError(response, `Unexpected body format. Cannot extract the URIs from the request body. Body was: ${JSON.stringify(body)}.`, BAD_REQUEST);
259
- return;
260
- }
261
- if (body.uris.length === 0) {
262
- this.handleError(response, `Insufficient body format. No URIs were defined by the request body. Body was: ${JSON.stringify(body)}.`, BAD_REQUEST);
263
- return;
264
- }
265
- for (const uri of body.uris) {
266
- try {
267
- await fs.access(FileUri.fsPath(uri));
268
- } catch {
269
- this.handleError(response, `The file does not exist. URI: ${uri}.`, NOT_FOUND);
270
- return;
271
- }
272
- }
273
- try {
274
- const downloadId = generateUuid();
275
- const outputRootPath = await this.createTempDir(downloadId);
276
- const distinctUris = Array.from(new Set(body.uris.map(uri => new URI(uri))));
277
- const tarPaths = [];
278
- // We should have one key in the map per FS drive.
279
- for (const [rootUri, uris] of (await this.directoryArchiver.findCommonParents(distinctUris)).entries()) {
280
- const rootPath = FileUri.fsPath(rootUri);
281
- const entries = uris.map(FileUri.fsPath).map(p => path.relative(rootPath, p));
282
- const outputPath = path.join(outputRootPath, `${path.basename(rootPath)}.tar`);
283
- await this.archive(rootPath, outputPath, entries);
284
- tarPaths.push(outputPath);
285
- }
286
- const options: PrepareDownloadOptions = { filePath: '', downloadId, remove: true, root: outputRootPath };
287
- if (tarPaths.length === 1) {
288
- // tslint:disable-next-line:whitespace
289
- const [outputPath,] = tarPaths;
290
- options.filePath = outputPath;
291
- await this.prepareDownload(request, response, options);
292
- } else {
293
- // We need to tar the tars.
294
- const outputPath = path.join(outputRootPath, `theia-archive-${Date.now()}.tar`);
295
- options.filePath = outputPath;
296
- await this.archive(outputRootPath, outputPath, tarPaths.map(p => path.relative(outputRootPath, p)));
297
- await this.prepareDownload(request, response, options);
298
- }
299
- } catch (e) {
300
- this.handleError(response, e, INTERNAL_SERVER_ERROR);
301
- }
302
- }
303
-
304
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2018 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
+ import * as os from 'os';
18
+ import * as fs from '@theia/core/shared/fs-extra';
19
+ import * as path from 'path';
20
+ import { generateUuid } from '@theia/core/lib/common/uuid';
21
+ import { Request, Response } from '@theia/core/shared/express';
22
+ import { inject, injectable } from '@theia/core/shared/inversify';
23
+ import { OK, BAD_REQUEST, METHOD_NOT_ALLOWED, NOT_FOUND, INTERNAL_SERVER_ERROR, REQUESTED_RANGE_NOT_SATISFIABLE, PARTIAL_CONTENT } from 'http-status-codes';
24
+ import URI from '@theia/core/lib/common/uri';
25
+ import { isEmpty } from '@theia/core/lib/common/objects';
26
+ import { ILogger } from '@theia/core/lib/common/logger';
27
+ import { FileUri } from '@theia/core/lib/common/file-uri';
28
+ import { DirectoryArchiver } from './directory-archiver';
29
+ import { FileDownloadData } from '../../common/download/file-download-data';
30
+ import { FileDownloadCache, DownloadStorageItem } from './file-download-cache';
31
+
32
+ interface PrepareDownloadOptions {
33
+ filePath: string;
34
+ downloadId: string;
35
+ remove: boolean;
36
+ root?: string;
37
+ }
38
+
39
+ @injectable()
40
+ export abstract class FileDownloadHandler {
41
+
42
+ @inject(ILogger)
43
+ protected readonly logger: ILogger;
44
+
45
+ @inject(DirectoryArchiver)
46
+ protected readonly directoryArchiver: DirectoryArchiver;
47
+
48
+ @inject(FileDownloadCache)
49
+ protected readonly fileDownloadCache: FileDownloadCache;
50
+
51
+ public abstract handle(request: Request, response: Response): Promise<void>;
52
+
53
+ /**
54
+ * Prepares the file and the link for download
55
+ */
56
+ protected async prepareDownload(request: Request, response: Response, options: PrepareDownloadOptions): Promise<void> {
57
+ const name = path.basename(options.filePath);
58
+ try {
59
+ await fs.access(options.filePath, fs.constants.R_OK);
60
+ const stat = await fs.stat(options.filePath);
61
+ this.fileDownloadCache.addDownload(options.downloadId, { file: options.filePath, remove: options.remove, size: stat.size, root: options.root });
62
+ // do not send filePath but instead use the downloadId
63
+ const data = { name, id: options.downloadId };
64
+ response.status(OK).send(data).end();
65
+ } catch (e) {
66
+ this.handleError(response, e, INTERNAL_SERVER_ERROR);
67
+ }
68
+ }
69
+
70
+ protected async download(request: Request, response: Response, downloadInfo: DownloadStorageItem, id: string): Promise<void> {
71
+ const filePath = downloadInfo.file;
72
+ const statSize = downloadInfo.size;
73
+ // this sets the content-disposition and content-type automatically
74
+ response.attachment(filePath);
75
+ try {
76
+ await fs.access(filePath, fs.constants.R_OK);
77
+ response.setHeader('Accept-Ranges', 'bytes');
78
+ // parse range header and combine multiple ranges
79
+ const range = this.parseRangeHeader(request.headers['range'], statSize);
80
+ if (!range) {
81
+ response.setHeader('Content-Length', statSize);
82
+ this.streamDownload(OK, response, fs.createReadStream(filePath), id);
83
+ } else {
84
+ const rangeStart = range.start;
85
+ const rangeEnd = range.end;
86
+ if (rangeStart >= statSize || rangeEnd >= statSize) {
87
+ response.setHeader('Content-Range', `bytes */${statSize}`);
88
+ // Return the 416 'Requested Range Not Satisfiable'.
89
+ response.status(REQUESTED_RANGE_NOT_SATISFIABLE).end();
90
+ return;
91
+ }
92
+ response.setHeader('Content-Range', `bytes ${rangeStart}-${rangeEnd}/${statSize}`);
93
+ response.setHeader('Content-Length', rangeStart === rangeEnd ? 0 : (rangeEnd - rangeStart + 1));
94
+ response.setHeader('Cache-Control', 'no-cache');
95
+ this.streamDownload(PARTIAL_CONTENT, response, fs.createReadStream(filePath, { start: rangeStart, end: rangeEnd }), id);
96
+ }
97
+ } catch (e) {
98
+ this.fileDownloadCache.deleteDownload(id);
99
+ this.handleError(response, e, INTERNAL_SERVER_ERROR);
100
+ }
101
+ }
102
+ /**
103
+ * Streams the file and pipe it to the Response to avoid any OOM issues
104
+ */
105
+ protected streamDownload(status: number, response: Response, stream: fs.ReadStream, id: string): void {
106
+ response.status(status);
107
+ stream.on('error', error => {
108
+ this.fileDownloadCache.deleteDownload(id);
109
+ this.handleError(response, error, INTERNAL_SERVER_ERROR);
110
+ });
111
+ response.on('error', error => {
112
+ this.fileDownloadCache.deleteDownload(id);
113
+ this.handleError(response, error, INTERNAL_SERVER_ERROR);
114
+ });
115
+ response.on('close', () => {
116
+ stream.destroy();
117
+ });
118
+ stream.pipe(response);
119
+ }
120
+ protected parseRangeHeader(range: string | string[] | undefined, statSize: number): { start: number, end: number } | undefined {
121
+ if (!range || range.length === 0 || Array.isArray(range)) {
122
+ return;
123
+ }
124
+ const index = range.indexOf('=');
125
+ if (index === -1) {
126
+ return;
127
+ }
128
+ const rangeType = range.slice(0, index);
129
+ if (rangeType !== 'bytes') {
130
+ return;
131
+ }
132
+ const [start, end] = range.slice(index + 1).split('-').map(r => parseInt(r, 10));
133
+ return {
134
+ start: isNaN(start) ? 0 : start,
135
+ end: (isNaN(end) || end > statSize - 1) ? (statSize - 1) : end
136
+ };
137
+ }
138
+ protected async archive(inputPath: string, outputPath: string = path.join(os.tmpdir(), generateUuid()), entries?: string[]): Promise<string> {
139
+ await this.directoryArchiver.archive(inputPath, outputPath, entries);
140
+ return outputPath;
141
+ }
142
+
143
+ protected async createTempDir(downloadId: string = generateUuid()): Promise<string> {
144
+ const outputPath = path.join(os.tmpdir(), downloadId);
145
+ await fs.mkdir(outputPath);
146
+ return outputPath;
147
+ }
148
+
149
+ protected async handleError(response: Response, reason: string | Error, status: number = INTERNAL_SERVER_ERROR): Promise<void> {
150
+ this.logger.error(reason);
151
+ response.status(status).send('Unable to download file.').end();
152
+ }
153
+
154
+ }
155
+
156
+ export namespace FileDownloadHandler {
157
+ export const SINGLE: symbol = Symbol('single');
158
+ export const MULTI: symbol = Symbol('multi');
159
+ export const DOWNLOAD_LINK: symbol = Symbol('download');
160
+ }
161
+
162
+ @injectable()
163
+ export class DownloadLinkHandler extends FileDownloadHandler {
164
+
165
+ async handle(request: Request, response: Response): Promise<void> {
166
+ const { method, query } = request;
167
+ if (method !== 'GET' && method !== 'HEAD') {
168
+ this.handleError(response, `Unexpected HTTP method. Expected GET got '${method}' instead.`, METHOD_NOT_ALLOWED);
169
+ return;
170
+ }
171
+ if (query === undefined || query.id === undefined || typeof query.id !== 'string') {
172
+ this.handleError(response, `Cannot access the 'id' query from the request. The query was: ${JSON.stringify(query)}.`, BAD_REQUEST);
173
+ return;
174
+ }
175
+ const cancelDownload = query.cancel;
176
+ const downloadInfo = this.fileDownloadCache.getDownload(query.id);
177
+ if (!downloadInfo) {
178
+ this.handleError(response, `Cannot find the file from the request. The query was: ${JSON.stringify(query)}.`, NOT_FOUND);
179
+ return;
180
+ }
181
+ // allow head request to determine the content length for parallel downloaders
182
+ if (method === 'HEAD') {
183
+ response.setHeader('Content-Length', downloadInfo.size);
184
+ response.status(OK).end();
185
+ return;
186
+ }
187
+ if (!cancelDownload) {
188
+ this.download(request, response, downloadInfo, query.id);
189
+ } else {
190
+ this.logger.info('Download', query.id, 'has been cancelled');
191
+ this.fileDownloadCache.deleteDownload(query.id);
192
+ }
193
+ }
194
+ }
195
+
196
+ @injectable()
197
+ export class SingleFileDownloadHandler extends FileDownloadHandler {
198
+
199
+ async handle(request: Request, response: Response): Promise<void> {
200
+ const { method, body, query } = request;
201
+ if (method !== 'GET') {
202
+ this.handleError(response, `Unexpected HTTP method. Expected GET got '${method}' instead.`, METHOD_NOT_ALLOWED);
203
+ return;
204
+ }
205
+ if (body !== undefined && !isEmpty(body)) {
206
+ this.handleError(response, `The request body must either undefined or empty when downloading a single file. The body was: ${JSON.stringify(body)}.`, BAD_REQUEST);
207
+ return;
208
+ }
209
+ if (query === undefined || query.uri === undefined || typeof query.uri !== 'string') {
210
+ this.handleError(response, `Cannot access the 'uri' query from the request. The query was: ${JSON.stringify(query)}.`, BAD_REQUEST);
211
+ return;
212
+ }
213
+ const uri = new URI(query.uri).toString(true);
214
+ const filePath = FileUri.fsPath(uri);
215
+
216
+ let stat: fs.Stats;
217
+ try {
218
+ stat = await fs.stat(filePath);
219
+ } catch {
220
+ this.handleError(response, `The file does not exist. URI: ${uri}.`, NOT_FOUND);
221
+ return;
222
+ }
223
+ try {
224
+ const downloadId = generateUuid();
225
+ const options: PrepareDownloadOptions = { filePath, downloadId, remove: false };
226
+ if (!stat.isDirectory()) {
227
+ await this.prepareDownload(request, response, options);
228
+ } else {
229
+ const outputRootPath = await this.createTempDir(downloadId);
230
+ const outputPath = path.join(outputRootPath, `${path.basename(filePath)}.tar`);
231
+ await this.archive(filePath, outputPath);
232
+ options.filePath = outputPath;
233
+ options.remove = true;
234
+ options.root = outputRootPath;
235
+ await this.prepareDownload(request, response, options);
236
+ }
237
+ } catch (e) {
238
+ this.handleError(response, e, INTERNAL_SERVER_ERROR);
239
+ }
240
+ }
241
+
242
+ }
243
+
244
+ @injectable()
245
+ export class MultiFileDownloadHandler extends FileDownloadHandler {
246
+
247
+ async handle(request: Request, response: Response): Promise<void> {
248
+ const { method, body } = request;
249
+ if (method !== 'PUT') {
250
+ this.handleError(response, `Unexpected HTTP method. Expected PUT got '${method}' instead.`, METHOD_NOT_ALLOWED);
251
+ return;
252
+ }
253
+ if (body === undefined) {
254
+ this.handleError(response, 'The request body must be defined when downloading multiple files.', BAD_REQUEST);
255
+ return;
256
+ }
257
+ if (!FileDownloadData.is(body)) {
258
+ this.handleError(response, `Unexpected body format. Cannot extract the URIs from the request body. Body was: ${JSON.stringify(body)}.`, BAD_REQUEST);
259
+ return;
260
+ }
261
+ if (body.uris.length === 0) {
262
+ this.handleError(response, `Insufficient body format. No URIs were defined by the request body. Body was: ${JSON.stringify(body)}.`, BAD_REQUEST);
263
+ return;
264
+ }
265
+ for (const uri of body.uris) {
266
+ try {
267
+ await fs.access(FileUri.fsPath(uri));
268
+ } catch {
269
+ this.handleError(response, `The file does not exist. URI: ${uri}.`, NOT_FOUND);
270
+ return;
271
+ }
272
+ }
273
+ try {
274
+ const downloadId = generateUuid();
275
+ const outputRootPath = await this.createTempDir(downloadId);
276
+ const distinctUris = Array.from(new Set(body.uris.map(uri => new URI(uri))));
277
+ const tarPaths = [];
278
+ // We should have one key in the map per FS drive.
279
+ for (const [rootUri, uris] of (await this.directoryArchiver.findCommonParents(distinctUris)).entries()) {
280
+ const rootPath = FileUri.fsPath(rootUri);
281
+ const entries = uris.map(FileUri.fsPath).map(p => path.relative(rootPath, p));
282
+ const outputPath = path.join(outputRootPath, `${path.basename(rootPath)}.tar`);
283
+ await this.archive(rootPath, outputPath, entries);
284
+ tarPaths.push(outputPath);
285
+ }
286
+ const options: PrepareDownloadOptions = { filePath: '', downloadId, remove: true, root: outputRootPath };
287
+ if (tarPaths.length === 1) {
288
+ // tslint:disable-next-line:whitespace
289
+ const [outputPath,] = tarPaths;
290
+ options.filePath = outputPath;
291
+ await this.prepareDownload(request, response, options);
292
+ } else {
293
+ // We need to tar the tars.
294
+ const outputPath = path.join(outputRootPath, `theia-archive-${Date.now()}.tar`);
295
+ options.filePath = outputPath;
296
+ await this.archive(outputRootPath, outputPath, tarPaths.map(p => path.relative(outputRootPath, p)));
297
+ await this.prepareDownload(request, response, options);
298
+ }
299
+ } catch (e) {
300
+ this.handleError(response, e, INTERNAL_SERVER_ERROR);
301
+ }
302
+ }
303
+
304
+ }