Skip to content

Commit 310c23c

Browse files
Remove depricated registerFileProtocol
1 parent bf3ea6a commit 310c23c

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

application/holder/src/service/electron/protocol.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import * as fs from 'fs';
55

66
export class Protocol {
77
public register() {
8-
protocol.registerFileProtocol('attachment', this.attachment.bind(this));
8+
protocol.handle('attachment', this.attachment.bind(this));
99
}
1010

11-
protected attachment(
12-
request: Electron.ProtocolRequest,
13-
callback: (response: string | Electron.ProtocolResponse) => void,
14-
): void {
15-
const path = request.url.slice('attachment://'.length);
16-
if (fs.existsSync(path)) {
17-
callback(url.fileURLToPath(`file://${path}`));
18-
} else {
19-
callback('attachment://file_does_not_exist');
11+
protected async attachment(request: Request): Promise<Response> {
12+
const path = url.fileURLToPath(new URL(request.url.replace(/^attachment:/, 'file:')));
13+
if (!fs.existsSync(path)) {
14+
return Promise.reject(new Error(`File doesn't exist`));
15+
}
16+
try {
17+
const buffer = await fs.promises.readFile(path);
18+
return new Response(buffer);
19+
} catch (_err) {
20+
return new Response('File not found', { status: 404 });
2021
}
2122
}
2223
}

0 commit comments

Comments
 (0)