mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Move the getPdfFilenameFromUrl
helper function from web/ui_utils.js
and into src/display/display_utils.js
It seems reasonable to place this alongside the *similar* `getFilenameFromUrl` helper function. This way, with the changes in the next patch, we also avoid having to expose the `isDataScheme` function in the API itself and we instead expose `getPdfFilenameFromUrl` in the API (which feels overall more appropriate).
This commit is contained in:
parent
a164941351
commit
bd9dee1544
8 changed files with 225 additions and 226 deletions
|
@ -451,13 +451,23 @@ function addLinkAttributes(link, { url, target, rel, enabled = true } = {}) {
|
|||
link.rel = typeof rel === "string" ? rel : DEFAULT_LINK_REL;
|
||||
}
|
||||
|
||||
function isDataScheme(url) {
|
||||
const ii = url.length;
|
||||
let i = 0;
|
||||
while (i < ii && url[i].trim() === "") {
|
||||
i++;
|
||||
}
|
||||
return url.substring(i, i + 5).toLowerCase() === "data:";
|
||||
}
|
||||
|
||||
function isPdfFile(filename) {
|
||||
return typeof filename === "string" && /\.pdf$/i.test(filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the file name from a given URL.
|
||||
* Gets the filename from a given URL.
|
||||
* @param {string} url
|
||||
* @returns {string}
|
||||
*/
|
||||
function getFilenameFromUrl(url) {
|
||||
const anchor = url.indexOf("#");
|
||||
|
@ -469,6 +479,48 @@ function getFilenameFromUrl(url) {
|
|||
return url.substring(url.lastIndexOf("/", end) + 1, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the filename or guessed filename from the url (see issue 3455).
|
||||
* @param {string} url - The original PDF location.
|
||||
* @param {string} defaultFilename - The value returned if the filename is
|
||||
* unknown, or the protocol is unsupported.
|
||||
* @returns {string} Guessed PDF filename.
|
||||
*/
|
||||
function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
|
||||
if (typeof url !== "string") {
|
||||
return defaultFilename;
|
||||
}
|
||||
if (isDataScheme(url)) {
|
||||
warn('getPdfFilenameFromUrl: ignore "data:"-URL for performance reasons.');
|
||||
return defaultFilename;
|
||||
}
|
||||
const reURI = /^(?:(?:[^:]+:)?\/\/[^/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
|
||||
// SCHEME HOST 1.PATH 2.QUERY 3.REF
|
||||
// Pattern to get last matching NAME.pdf
|
||||
const reFilename = /[^/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
|
||||
const splitURI = reURI.exec(url);
|
||||
let suggestedFilename =
|
||||
reFilename.exec(splitURI[1]) ||
|
||||
reFilename.exec(splitURI[2]) ||
|
||||
reFilename.exec(splitURI[3]);
|
||||
if (suggestedFilename) {
|
||||
suggestedFilename = suggestedFilename[0];
|
||||
if (suggestedFilename.includes("%")) {
|
||||
// URL-encoded %2Fpath%2Fto%2Ffile.pdf should be file.pdf
|
||||
try {
|
||||
suggestedFilename = reFilename.exec(
|
||||
decodeURIComponent(suggestedFilename)
|
||||
)[0];
|
||||
} catch (ex) {
|
||||
// Possible (extremely rare) errors:
|
||||
// URIError "Malformed URI", e.g. for "%AA.pdf"
|
||||
// TypeError "null has no properties", e.g. for "%2F.pdf"
|
||||
}
|
||||
}
|
||||
}
|
||||
return suggestedFilename || defaultFilename;
|
||||
}
|
||||
|
||||
class StatTimer {
|
||||
constructor() {
|
||||
this.started = Object.create(null);
|
||||
|
@ -655,6 +707,7 @@ export {
|
|||
DOMCMapReaderFactory,
|
||||
DOMSVGFactory,
|
||||
getFilenameFromUrl,
|
||||
getPdfFilenameFromUrl,
|
||||
isFetchSupported,
|
||||
isPdfFile,
|
||||
isValidFetchUrl,
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
import {
|
||||
addLinkAttributes,
|
||||
getFilenameFromUrl,
|
||||
getPdfFilenameFromUrl,
|
||||
isFetchSupported,
|
||||
isPdfFile,
|
||||
isValidFetchUrl,
|
||||
|
@ -129,6 +130,7 @@ export {
|
|||
// From "./display/display_utils.js":
|
||||
addLinkAttributes,
|
||||
getFilenameFromUrl,
|
||||
getPdfFilenameFromUrl,
|
||||
isPdfFile,
|
||||
LinkTarget,
|
||||
loadScript,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue