From f16ce580d7ab2146105efbb31af3880c8faa7aa4 Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Sun, 21 May 2023 02:25:46 +0800 Subject: [PATCH] feat: support file relative to wiki folder --- src/services/native/index.ts | 3 ++- src/services/native/interface.ts | 2 +- src/services/view/setupViewSession.ts | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/services/native/index.ts b/src/services/native/index.ts index fa596848..4a9e6361 100644 --- a/src/services/native/index.ts +++ b/src/services/native/index.ts @@ -203,7 +203,7 @@ ${message.message} } } - public async handleFileProtocol(request: Electron.ProtocolRequest, callback: (response: string | Electron.ProtocolResponse) => void): Promise { + public async handleFileProtocol(request: { url: string }, callback: (response: string) => void): Promise { logger.info('handleFileProtocol() getting url', { url: request.url }); const pathname = decodeURI(request.url.replace('open://', '').replace('file://', '')); logger.info('handleFileProtocol() handle file:// or open:// This url will open file in-wiki', { pathname }); @@ -240,6 +240,7 @@ ${message.message} } public registerFileProtocol(): boolean { + // this normally nor called. In wiki file:// image will use `handleFileLink()` in `src\services\view\setupViewSession.ts`. const succeed = protocol.registerFileProtocol('file', this.handleFileProtocol.bind(this)); return succeed; } diff --git a/src/services/native/interface.ts b/src/services/native/interface.ts index b13cd524..4c57e853 100644 --- a/src/services/native/interface.ts +++ b/src/services/native/interface.ts @@ -17,7 +17,7 @@ export interface INativeService { */ executeZxScript$(zxWorkerArguments: IZxFileInput, wikiFolderLocation?: string): Observable; getLocalHostUrlWithActualIP(url: string): Promise; - handleFileProtocol(request: Electron.ProtocolRequest, callback: (response: string | Electron.ProtocolResponse) => void): Promise; + handleFileProtocol(request: { url: string }, callback: (response: string) => void): Promise; log(level: string, message: string, meta?: Record): Promise; open(uri: string, isDirectory?: boolean): Promise; openInEditor(filePath: string, editorName?: string | undefined): Promise; diff --git a/src/services/view/setupViewSession.ts b/src/services/view/setupViewSession.ts index 1a11af3e..09cb61fd 100644 --- a/src/services/view/setupViewSession.ts +++ b/src/services/view/setupViewSession.ts @@ -34,6 +34,15 @@ export function setupViewSession(workspace: IWorkspace, preferences: IPreference assignAdminAuthToken(workspace.id, details, authService, viewContext); callback({ cancel: false, requestHeaders: details.requestHeaders }); }); + sessionOfView.webRequest.onBeforeRequest((details, callback) => { + if (details.url.startsWith('file://') || details.url.startsWith('open://')) { + void handleFileLink(details, nativeService, callback); + } else { + callback({ + cancel: false, + }); + } + }); handleFileProtocol(sessionOfView, nativeService); return sessionOfView; } @@ -60,5 +69,21 @@ function assignAdminAuthToken(workspaceID: string, details: Electron.OnBeforeSen } function handleFileProtocol(sessionOfView: Electron.Session, nativeService: INativeService) { + // this normally nor called. In wiki file:// image will use `handleFileLink()` below. sessionOfView.protocol.registerFileProtocol('file', nativeService.handleFileProtocol.bind(nativeService)); } + +async function handleFileLink(details: Electron.OnBeforeRequestListenerDetails, nativeService: INativeService, callback: (response: Electron.CallbackResponse) => void) { + await nativeService.handleFileProtocol({ url: details.url }, (redirectURL: string) => { + if (redirectURL === details.url) { + callback({ + cancel: false, + }); + } else { + callback({ + cancel: false, + redirectURL, + }); + } + }); +}