Skip to content

Commit

Permalink
MOBILE-4688 file: Fix files with unencoded % in their name
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Nov 13, 2024
1 parent 9a970da commit 9db4782
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
42 changes: 39 additions & 3 deletions src/core/services/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,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;
}
}

/**
Expand All @@ -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;
}
}

/**
Expand Down Expand Up @@ -831,7 +855,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);

Expand Down
24 changes: 17 additions & 7 deletions src/core/services/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 9db4782

Please sign in to comment.