diff --git a/src/core/services/file.ts b/src/core/services/file.ts index 07c11679ebe..d900ad18593 100644 --- a/src/core/services/file.ts +++ b/src/core/services/file.ts @@ -152,7 +152,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; + } } /** @@ -166,7 +178,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; + } } /** @@ -831,7 +855,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/services/utils/utils.ts b/src/core/services/utils/utils.ts index 05fe91cece2..3a58fc88f6e 100644 --- a/src/core/services/utils/utils.ts +++ b/src/core/services/utils/utils.ts @@ -1003,17 +1003,27 @@ export class CoreUtilsProvider { // Error, use the original path. } - const openFile = async (mimetype?: string) => { - if (this.shouldOpenWithDialog(options)) { - await FileOpener.showOpenWithDialog(path, mimetype || ''); - } else { - await FileOpener.open(path, mimetype || ''); + const openFile = async (path: string, mimetype?: string) => { + try { + if (this.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; @@ -1025,7 +1035,7 @@ export class CoreUtilsProvider { throw error; } - await openFile(deprecatedMimetype); + await openFile(path, deprecatedMimetype); } } catch (error) { this.logger.error('Error opening file ' + path + ' with mimetype ' + mimetype);