diff --git a/src/core/services/file.ts b/src/core/services/file.ts index 9e3f4f19b..90ec50d44 100644 --- a/src/core/services/file.ts +++ b/src/core/services/file.ts @@ -149,7 +149,19 @@ export class CoreFileProvider { await this.init(); this.logger.debug('Get file: ' + path); - return await File.resolveLocalFilesystemUrl(this.addBasePathIfNeeded(path)); + try { + return await File.resolveLocalFilesystemUrl(this.addBasePathIfNeeded(path)); + } catch (error) { + if (error && error.code === FileError.NOT_FOUND_ERR) { + // Cannot read some files if the path contains the % character and it's not an encoded char. Try encoding it. + const encodedPath = encodeURI(path); + if (encodedPath !== path) { + return await File.resolveLocalFilesystemUrl(this.addBasePathIfNeeded(encodedPath)); + } + } + + throw error; + } } /** @@ -163,7 +175,19 @@ export class CoreFileProvider { this.logger.debug('Get directory: ' + path); - return await File.resolveDirectoryUrl(this.addBasePathIfNeeded(path)); + try { + return await File.resolveDirectoryUrl(this.addBasePathIfNeeded(path)); + } catch (error) { + if (error && error.code === FileError.NOT_FOUND_ERR) { + // Cannot read some files if the path contains the % character and it's not an encoded char. Try encoding it. + const encodedPath = encodeURI(path); + if (encodedPath !== path) { + return await File.resolveDirectoryUrl(this.addBasePathIfNeeded(encodedPath)); + } + } + + throw error; + } } /** @@ -828,7 +852,19 @@ export class CoreFileProvider { return entry; } catch (error) { - // The copy can fail if the path has encoded characters. Try again if that's the case. + try { + // The copy/move can fail if the final path contains the % character and it's not an encoded char. Try encoding it. + const encodedTo = encodeURI(to); + if (to !== encodedTo) { + const entry = await moveCopyFn(this.basePath, from, this.basePath, encodedTo); + + return entry; + } + } catch { + // Still failing, continue with next fallback. + } + + // The copy/move can fail if the path has encoded characters. Try again if that's the case. const decodedFrom = decodeURI(from); const decodedTo = decodeURI(to); diff --git a/src/core/singletons/opener.ts b/src/core/singletons/opener.ts index da0233182..df73a3270 100644 --- a/src/core/singletons/opener.ts +++ b/src/core/singletons/opener.ts @@ -128,17 +128,27 @@ export class CoreOpener { // Error, use the original path. } - const openFile = async (mimetype?: string) => { - if (CoreOpener.shouldOpenWithDialog(options)) { - await FileOpener.showOpenWithDialog(path, mimetype || ''); - } else { - await FileOpener.open(path, mimetype || ''); + const openFile = async (path: string, mimetype?: string) => { + try { + if (CoreOpener.shouldOpenWithDialog(options)) { + await FileOpener.showOpenWithDialog(path, mimetype || ''); + } else { + await FileOpener.open(path, mimetype || ''); + } + } catch (error) { + // If the file contains the % character without encoding the open can fail. Try again encoding it. + const encodedPath = encodeURI(path); + if (path !== encodedPath) { + return await openFile(encodedPath, mimetype); + } + + throw error; } }; try { try { - await openFile(mimetype); + await openFile(path, mimetype); } catch (error) { if (!extension || !error || Number(error.status) !== 9) { throw error; @@ -150,7 +160,7 @@ export class CoreOpener { throw error; } - await openFile(deprecatedMimetype); + await openFile(path, deprecatedMimetype); } } catch (error) { CoreOpener.logger.error('Error opening file ' + path + ' with mimetype ' + mimetype);