-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
mini-browser-endpoint.ts
303 lines (254 loc) · 10.7 KB
/
mini-browser-endpoint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import * as fs from 'fs-extra';
import { lookup } from 'mime-types';
import { injectable, inject, named } from 'inversify';
import { Application, Request, Response } from 'express';
import URI from '@theia/core/lib/common/uri';
import { FileUri } from '@theia/core/lib/node/file-uri';
import { ILogger } from '@theia/core/lib/common/logger';
import { MaybePromise } from '@theia/core/lib/common/types';
import { FileSystem, FileStat } from '@theia/filesystem/lib/common';
import { ContributionProvider } from '@theia/core/lib/common/contribution-provider';
import { BackendApplicationContribution } from '@theia/core/lib/node/backend-application';
import { MiniBrowserService } from '../common/mini-browser-service';
/**
* The return type of the `FileSystem#resolveContent` method.
*/
export interface FileStatWithContent {
/**
* The file stat.
*/
readonly stat: FileStat;
/**
* The content of the file as a UTF-8 encoded string.
*/
readonly content: string;
}
/**
* Endpoint handler contribution for the `MiniBrowserEndpoint`.
*/
export const MiniBrowserEndpointHandler = Symbol('MiniBrowserEndpointHandler');
export interface MiniBrowserEndpointHandler {
/**
* Returns with or resolves to the file extensions supported by the current `mini-browser` endpoint handler.
* The file extension must not start with the leading `.` (dot). For instance; `'html'` or `['jpg', 'jpeg']`.
* The file extensions are case insensitive.
*/
supportedExtensions(): MaybePromise<string | string[]>;
/**
* Returns a number representing the priority between all the available handlers for the same file extension.
*/
priority(): number;
/**
* Responds back to the sender.
*/
respond(statWithContent: FileStatWithContent, response: Response): MaybePromise<Response>;
}
@injectable()
export class MiniBrowserEndpoint implements BackendApplicationContribution, MiniBrowserService {
/**
* Endpoint path to handle the request for the given resource.
*/
static HANDLE_PATH = '/mini-browser/';
@inject(ILogger)
protected readonly logger: ILogger;
@inject(FileSystem)
protected readonly fileSystem: FileSystem;
@inject(ContributionProvider)
@named(MiniBrowserEndpointHandler)
protected readonly contributions: ContributionProvider<MiniBrowserEndpointHandler>;
protected readonly handlers: Map<string, MiniBrowserEndpointHandler> = new Map();
configure(app: Application): void {
app.get(`${MiniBrowserEndpoint.HANDLE_PATH}*`, async (request, response) => this.response(await this.getUri(request), response));
}
async onStart(): Promise<void> {
for (const handler of this.getContributions()) {
const extensions = await handler.supportedExtensions();
for (const extension of (Array.isArray(extensions) ? extensions : [extensions]).map(e => e.toLocaleLowerCase())) {
const existingHandler = this.handlers.get(extension);
if (!existingHandler || handler.priority > existingHandler.priority) {
this.handlers.set(extension, handler);
}
}
}
}
async supportedFileExtensions(): Promise<Readonly<{ extension: string, priority: number }>[]> {
return Array.from(this.handlers.entries()).map(([extension, handler]) => ({ extension, priority: handler.priority() }));
}
protected async response(uri: string, response: Response): Promise<Response> {
const exists = await this.fileSystem.exists(uri);
if (!exists) {
return this.missingResourceHandler()(uri, response);
}
const statWithContent = await this.readContent(uri);
try {
if (!statWithContent.stat.isDirectory) {
const extension = uri.split('.').pop();
if (!extension) {
return this.defaultHandler()(statWithContent, response);
}
const handler = this.handlers.get(extension.toString().toLocaleLowerCase());
if (!handler) {
return this.defaultHandler()(statWithContent, response);
}
return handler.respond(statWithContent, response);
}
} catch (e) {
return this.errorHandler()(e, uri, response);
}
return this.defaultHandler()(statWithContent, response);
}
protected getContributions(): MiniBrowserEndpointHandler[] {
return this.contributions.getContributions();
}
protected getUri(request: Request): MaybePromise<string> {
const decodedPath = request.path.substr(MiniBrowserEndpoint.HANDLE_PATH.length);
return new URI(FileUri.create(decodedPath).toString(true)).toString(true);
}
protected async readContent(uri: string): Promise<FileStatWithContent> {
return this.fileSystem.resolveContent(uri);
}
// tslint:disable-next-line:no-any
protected errorHandler(): (error: any, uri: string, response: Response) => MaybePromise<Response> {
// tslint:disable-next-line:no-any
return async (error: any, uri: string, response: Response) => {
const details = error.toString ? error.toString() : error;
this.logger.error(`Error occurred while handling request for ${uri}. Details: ${details}`);
if (error instanceof Error) {
let message = error.message;
if (error.stack) {
message += `\n${error.stack}`;
}
this.logger.error(message);
} else if (typeof error === 'string') {
this.logger.error(error);
} else {
this.logger.error(`${error}`);
}
return response.send(500);
};
}
protected missingResourceHandler(): (uri: string, response: Response) => MaybePromise<Response> {
return async (uri: string, response: Response) => {
this.logger.error(`Cannot handle missing resource. URI: ${uri}.`);
return response.send();
};
}
protected defaultHandler(): (statWithContent: FileStatWithContent, response: Response) => MaybePromise<Response> {
return async (statWithContent: FileStatWithContent, response: Response) => {
const { stat, content } = statWithContent;
const mimeType = lookup(FileUri.fsPath(stat.uri));
if (!mimeType) {
this.logger.warn(`Cannot handle unexpected resource. URI: ${statWithContent.stat.uri}.`);
return response.send();
}
response.contentType(mimeType);
return response.send(content);
};
}
}
// See `EditorManager#canHandle`.
const CODE_EDITOR_PRIORITY = 100;
/**
* Endpoint handler contribution for HTML files.
*/
@injectable()
export class HtmlHandler implements MiniBrowserEndpointHandler {
supportedExtensions(): string {
return 'html';
}
priority(): number {
// Prefer Code Editor over Mini Browser
// https://github.com/theia-ide/theia/issues/2051
return 1;
}
respond(statWithContent: FileStatWithContent, response: Response): MaybePromise<Response> {
response.contentType('text/html');
return response.send(statWithContent.content);
}
}
/**
* Handler for JPG resources.
*/
@injectable()
export class ImageHandler implements MiniBrowserEndpointHandler {
supportedExtensions(): string[] {
return ['jpg', 'jpeg', 'png', 'bmp', 'gif'];
}
priority(): number {
return CODE_EDITOR_PRIORITY + 1;
}
respond(statWithContent: FileStatWithContent, response: Response): MaybePromise<Response> {
fs.readFile(FileUri.fsPath(statWithContent.stat.uri), (error, data) => {
if (error) {
throw error;
}
response.contentType('image/jpeg');
response.send(data);
});
return response;
}
}
/**
* PDF endpoint handler.
*/
@injectable()
export class PdfHandler implements MiniBrowserEndpointHandler {
supportedExtensions(): string {
return 'pdf';
}
priority(): number {
return CODE_EDITOR_PRIORITY + 1;
}
respond(statWithContent: FileStatWithContent, response: Response): MaybePromise<Response> {
// https://stackoverflow.com/questions/11598274/display-pdf-in-browser-using-express-js
const encodeRFC5987ValueChars = (input: string) =>
encodeURIComponent(input).
// Note that although RFC3986 reserves "!", RFC5987 does not, so we do not need to escape it.
replace(/['()]/g, escape). // i.e., %27 %28 %29
replace(/\*/g, '%2A').
// The following are not required for percent-encoding per RFC5987, so we can allow for a little better readability over the wire: |`^.
replace(/%(?:7C|60|5E)/g, unescape);
const fileName = FileUri.create(statWithContent.stat.uri).path.base;
fs.readFile(FileUri.fsPath(statWithContent.stat.uri), (error, data) => {
if (error) {
throw error;
}
// Change `inline` to `attachment` if you would like to force downloading the PDF instead of previewing in the browser.
response.setHeader('Content-disposition', `inline; filename*=UTF-8''${encodeRFC5987ValueChars(fileName)}`);
response.contentType('application/pdf');
response.send(data);
});
return response;
}
}
/**
* Endpoint contribution for SVG resources.
*/
@injectable()
export class SvgHandler implements MiniBrowserEndpointHandler {
supportedExtensions(): string {
return 'svg';
}
priority(): number {
return 1;
}
respond(statWithContent: FileStatWithContent, response: Response): MaybePromise<Response> {
response.contentType('image/svg+xml');
return response.send(statWithContent.content);
}
}