fix: infinite redirect when using file:// protocol to load image

This commit is contained in:
lin onetwo 2024-06-29 19:18:09 +08:00
parent e241ff3693
commit 5b3e03aac9
2 changed files with 8 additions and 2 deletions

View file

@ -270,7 +270,7 @@ ${message.message}
* hostname: `.`, pathname: `/files/xxx.png`
*/
let filePath = decodeURIComponent(`${hostname}${pathname}`);
// get "/D:/" on windows
// get "D:/" instead of "/D:/" on windows
if (process.platform === 'win32' && filePath.startsWith('/')) {
filePath = filePath.substring(1);
}

View file

@ -70,7 +70,13 @@ function handleFileLink(details: Electron.OnBeforeRequestListenerDetails, callba
const nativeService = container.get<INativeService>(serviceIdentifier.NativeService);
const absolutePath: string | undefined = nativeService.formatFileUrlToAbsolutePath(details.url);
// When details.url is an absolute route, we just load it, don't need any redirect
if (`file://${absolutePath}` === decodeURI(details.url) || absolutePath === decodeURI(details.url)) {
if (
`file://${absolutePath}` === decodeURI(details.url) ||
absolutePath === decodeURI(details.url) ||
// also allow malformed `file:///` on `details.url` on windows, prevent infinite redirect when this check failed.
(process.platform === 'win32' && `file:///${absolutePath}` === decodeURI(details.url))
) {
logger.debug(`Open file protocol to ${String(absolutePath)}`, { function: 'handleFileLink' });
callback({
cancel: false,
});