Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOBILE-4482 core: Fix treat draft URLs failing because of encoded chars #4172

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/core/services/file-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ export class CoreFileHelperProvider {
if (filename.indexOf('?') != -1) {
filename = filename.substring(0, filename.indexOf('?'));
}
filename = CoreUrl.decodeURIComponent(filename);

if (pluginfileMap[filename]) {
replaceMap[url] = pluginfileMap[filename];
Expand Down Expand Up @@ -611,23 +612,24 @@ export class CoreFileHelperProvider {
const draftfileUrlRegexPrefix = CoreText.escapeForRegex(draftfileUrl) + '/[^/]+/[^/]+/[^/]+/[^/]+/';

files.forEach((file) => {
if (!file.filename) {
return;
// Get the file name from the URL instead of using file.filename because the URL can have encoded characters.
// encodeURIComponent doesn't encode parenthesis, so it's better to rely on the name from the URL.
const url = CoreFileHelper.getFileUrl(file);
let filename = url.substring(url.lastIndexOf('/') + 1);
if (filename.indexOf('?') != -1) {
filename = filename.substring(0, filename.indexOf('?'));
}

// Search the draftfile URL in the original text.
const matches = originalText.match(
new RegExp(draftfileUrlRegexPrefix + CoreText.escapeForRegex(file.filename) + '[^\'" ]*', 'i'),
new RegExp(draftfileUrlRegexPrefix + CoreText.escapeForRegex(filename) + '[^\'" ]*', 'i'),
);

if (!matches || !matches[0]) {
return; // Original URL not found, skip.
}

treatedText = treatedText.replace(
new RegExp(CoreText.escapeForRegex(CoreFileHelper.getFileUrl(file)), 'g'),
matches[0],
);
treatedText = treatedText.replace(new RegExp(CoreText.escapeForRegex(url), 'g'), matches[0]);
});

return treatedText;
Expand Down