From 6da0946a7c0fbe19fc6a11bdfddef2e04317a1bb Mon Sep 17 00:00:00 2001 From: linonetwo Date: Wed, 21 Jun 2023 13:04:56 +0800 Subject: [PATCH] fix: file path --- src/services/native/index.ts | 2 +- src/services/view/setupViewFileProtocol.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/services/native/index.ts b/src/services/native/index.ts index bfaa5c9c..77773c05 100644 --- a/src/services/native/index.ts +++ b/src/services/native/index.ts @@ -256,7 +256,7 @@ ${message.message} * urlWithFileProtocol: `file://./files/xxx.png` * hostname: `.`, pathname: `/files/xxx.png` */ - const filePath = `${hostname}${pathname}`; + const filePath = decodeURIComponent(`${hostname}${pathname}`); logger.info('handle file:// or open:// This url will open file in-wiki', { hostname, pathname, filePath, function: 'formatFileUrlToAbsolutePath' }); let fileExists = fs.existsSync(filePath); logger.info(`This file (decodeURI) ${fileExists ? '' : 'not '}exists`, { filePath, function: 'formatFileUrlToAbsolutePath' }); diff --git a/src/services/view/setupViewFileProtocol.ts b/src/services/view/setupViewFileProtocol.ts index 22b0192b..84b9f06b 100644 --- a/src/services/view/setupViewFileProtocol.ts +++ b/src/services/view/setupViewFileProtocol.ts @@ -30,10 +30,9 @@ export function handleOpenFileExternalLink(nextUrl: string): INewWindowAction | * Handle file protocol in webview to request file content and show in the view. */ export function handleViewFileContentLoading(view: BrowserView) { - const nativeService = container.get(serviceIdentifier.NativeService); view.webContents.session.webRequest.onBeforeRequest((details, callback) => { if (details.url.startsWith('file://') || details.url.startsWith('open://')) { - handleFileLink(details, nativeService, callback); + handleFileLink(details, callback); } else { callback({ cancel: false, @@ -42,18 +41,19 @@ export function handleViewFileContentLoading(view: BrowserView) { }); } -function handleFileLink(details: Electron.OnBeforeRequestListenerDetails, nativeService: INativeService, callback: (response: Electron.CallbackResponse) => void) { - const redirectURL = nativeService.formatFileUrlToAbsolutePath(decodeURI(details.url)); - // DEBUG: console redirectURL - console.log(`redirectURL`, redirectURL); +function handleFileLink(details: Electron.OnBeforeRequestListenerDetails, callback: (response: Electron.CallbackResponse) => void) { + const nativeService = container.get(serviceIdentifier.NativeService); + const redirectURL = nativeService.formatFileUrlToAbsolutePath(details.url); if (redirectURL === details.url) { callback({ cancel: false, + // prevent redirect loop, remove file:// prefix, so it never comes back to this function from `handleViewFileContentLoading` again. + redirectURL: redirectURL.replace('file://', '').replace('open://', ''), }); } else { callback({ cancel: false, - redirectURL, + redirectURL: encodeURI(redirectURL), }); } }