MOBILE-4688 file: Fix files with unencoded % in their name

main
Dani Palou 2024-11-13 12:52:06 +01:00
parent 84781a7658
commit 423838c58d
2 changed files with 56 additions and 10 deletions

View File

@ -149,7 +149,19 @@ export class CoreFileProvider {
await this.init();
this.logger.debug('Get file: ' + path);
return <FileEntry> await File.resolveLocalFilesystemUrl(this.addBasePathIfNeeded(path));
try {
return <FileEntry> 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 <FileEntry> 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 <FileEntry | DirectoryEntry> 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 <FileEntry | DirectoryEntry> 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);

View File

@ -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);