1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Merge pull request #13105 from Snuffleupagus/BasePdfManager-parseDocBaseUrl

Improve memory usage around the `BasePdfManager.docBaseUrl` parameter (PR 7689 follow-up)
This commit is contained in:
Tim van der Meij 2021-03-19 23:03:20 +01:00 committed by GitHub
commit 8269ddbd16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 251 additions and 244 deletions

View file

@ -22,7 +22,6 @@ import {
DEFAULT_SCALE_VALUE,
EventBus,
getActiveOrFocusedElement,
getPDFFileNameFromURL,
isValidRotation,
isValidScrollMode,
isValidSpreadMode,
@ -44,6 +43,7 @@ import {
createPromiseCapability,
getDocument,
getFilenameFromUrl,
getPdfFilenameFromUrl,
GlobalWorkerOptions,
InvalidPDFException,
isPdfFile,
@ -749,7 +749,7 @@ const PDFViewerApplication = {
setTitleUsingUrl(url = "") {
this.url = url;
this.baseUrl = url.split("#")[0];
let title = getPDFFileNameFromURL(url, "");
let title = getPdfFilenameFromUrl(url, "");
if (!title) {
try {
title = decodeURIComponent(getFilenameFromUrl(url)) || url;
@ -773,7 +773,7 @@ const PDFViewerApplication = {
get _docFilename() {
// Use `this.url` instead of `this.baseUrl` to perform filename detection
// based on the reference fragment as ultimate fallback if needed.
return this._contentDispositionFilename || getPDFFileNameFromURL(this.url);
return this._contentDispositionFilename || getPdfFilenameFromUrl(this.url);
},
/**

View file

@ -13,8 +13,7 @@
* limitations under the License.
*/
import { getPDFFileNameFromURL } from "./ui_utils.js";
import { loadScript } from "pdfjs-lib";
import { getPdfFilenameFromUrl, loadScript } from "pdfjs-lib";
async function docPropertiesLookup(pdfDocument) {
const url = "",
@ -37,7 +36,7 @@ async function docPropertiesLookup(pdfDocument) {
...info,
baseURL: baseUrl,
filesize: contentLength,
filename: contentDispositionFilename || getPDFFileNameFromURL(url),
filename: contentDispositionFilename || getPdfFilenameFromUrl(url),
metadata: metadata?.getRaw(),
authors: metadata?.get("dc:creator"),
numPages: pdfDocument.numPages,

View file

@ -13,12 +13,12 @@
* limitations under the License.
*/
import { createPromiseCapability, PDFDateString } from "pdfjs-lib";
import {
getPageSizeInches,
getPDFFileNameFromURL,
isPortraitOrientation,
} from "./ui_utils.js";
createPromiseCapability,
getPdfFilenameFromUrl,
PDFDateString,
} from "pdfjs-lib";
import { getPageSizeInches, isPortraitOrientation } from "./ui_utils.js";
const DEFAULT_FIELD_CONTENT = "-";
@ -140,7 +140,7 @@ class PDFDocumentProperties {
pageSize,
isLinearized,
] = await Promise.all([
contentDispositionFilename || getPDFFileNameFromURL(this.url),
contentDispositionFilename || getPdfFilenameFromUrl(this.url),
this._parseFileSize(contentLength),
this._parseDate(info.CreationDate),
this._parseDate(info.ModDate),

View file

@ -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,