A couple of small tweaks of the getPdfFilenameFromUrl helper

- Shorten the `getURL` function slightly, by re-factoring the try-catch blocks.
 - Change how the `decode` function looks for a decoded ".pdf" name, to skip the regular expression matching when it's not needed and to allow re-using the already defined `pdfRegex`.
This commit is contained in:
Jonas Jenwald 2026-05-05 12:01:51 +02:00
parent a55cec4a0f
commit 8fc56772e8

View File

@ -347,22 +347,19 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
const getURL = urlString => {
try {
return new URL(urlString);
} catch {
try {
return new URL(decodeURIComponent(urlString));
} catch {
try {
// Attempt to parse the URL using the document's base URI.
return new URL(urlString, "https://foo.bar");
} catch {
try {
return new URL(decodeURIComponent(urlString), "https://foo.bar");
} catch {
return null;
}
}
}
}
} catch {}
try {
return new URL(decodeURIComponent(urlString));
} catch {}
try {
// Attempt to parse the URL using the document's base URI.
return new URL(urlString, "https://foo.bar");
} catch {}
try {
return new URL(decodeURIComponent(urlString), "https://foo.bar");
} catch {}
return null;
};
const newURL = getURL(url);
@ -376,7 +373,8 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
let decoded = decodeURIComponent(name);
if (decoded.includes("/")) {
decoded = stripPath(decoded);
if (/^\.pdf$/i.test(decoded)) {
// Ignore the decoded name if it's identical to ".pdf".
if (decoded.length === 4 && pdfRegex.test(decoded)) {
return name;
}
}