mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 01:10: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
|
@ -570,60 +570,6 @@ function noContextMenuHandler(evt) {
|
|||
evt.preventDefault();
|
||||
}
|
||||
|
||||
function isDataSchema(url) {
|
||||
let i = 0;
|
||||
const ii = url.length;
|
||||
while (i < ii && url[i].trim() === "") {
|
||||
i++;
|
||||
}
|
||||
return url.substring(i, i + 5).toLowerCase() === "data:";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 (isDataSchema(url)) {
|
||||
console.warn(
|
||||
"getPDFFileNameFromURL: " +
|
||||
'ignoring "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;
|
||||
}
|
||||
|
||||
function normalizeWheelEventDirection(evt) {
|
||||
let delta = Math.hypot(evt.deltaX, evt.deltaY);
|
||||
const angle = Math.atan2(evt.deltaY, evt.deltaX);
|
||||
|
@ -1063,7 +1009,6 @@ export {
|
|||
getActiveOrFocusedElement,
|
||||
getOutputScale,
|
||||
getPageSizeInches,
|
||||
getPDFFileNameFromURL,
|
||||
getVisibleElements,
|
||||
isPortraitOrientation,
|
||||
isValidRotation,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue