Move the initialization of "metadata" out of PDFViewerApplication.load
Over time, with more and more API-functionality added, the `PDFViewerApplication.load` method has become quite large and complex. In an attempt to improve the current situation somewhat, this patch moves the fetching and initialization of "metadata" out into its own (private) helper method instead.
This commit is contained in:
parent
32f1d0de76
commit
d07be1a89b
273
web/app.js
273
web/app.js
@ -1237,144 +1237,155 @@ const PDFViewerApplication = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this._initializePageLabels(pdfDocument);
|
this._initializePageLabels(pdfDocument);
|
||||||
|
this._initializeMetadata(pdfDocument);
|
||||||
|
},
|
||||||
|
|
||||||
pdfDocument
|
/**
|
||||||
.getMetadata()
|
* @private
|
||||||
.then(({ info, metadata, contentDispositionFilename }) => {
|
*/
|
||||||
this.documentInfo = info;
|
async _initializeMetadata(pdfDocument) {
|
||||||
this.metadata = metadata;
|
const {
|
||||||
this.contentDispositionFilename = contentDispositionFilename;
|
info,
|
||||||
|
metadata,
|
||||||
|
contentDispositionFilename,
|
||||||
|
} = await pdfDocument.getMetadata();
|
||||||
|
|
||||||
// Provides some basic debug information
|
if (pdfDocument !== this.pdfDocument) {
|
||||||
console.log(
|
return; // The document was closed while the metadata resolved.
|
||||||
"PDF " +
|
}
|
||||||
pdfDocument.fingerprint +
|
this.documentInfo = info;
|
||||||
" [" +
|
this.metadata = metadata;
|
||||||
info.PDFFormatVersion +
|
this.contentDispositionFilename = contentDispositionFilename;
|
||||||
" " +
|
|
||||||
(info.Producer || "-").trim() +
|
|
||||||
" / " +
|
|
||||||
(info.Creator || "-").trim() +
|
|
||||||
"]" +
|
|
||||||
" (PDF.js: " +
|
|
||||||
(version || "-") +
|
|
||||||
(AppOptions.get("enableWebGL") ? " [WebGL]" : "") +
|
|
||||||
")"
|
|
||||||
);
|
|
||||||
|
|
||||||
let pdfTitle;
|
// Provides some basic debug information
|
||||||
|
console.log(
|
||||||
|
"PDF " +
|
||||||
|
pdfDocument.fingerprint +
|
||||||
|
" [" +
|
||||||
|
info.PDFFormatVersion +
|
||||||
|
" " +
|
||||||
|
(info.Producer || "-").trim() +
|
||||||
|
" / " +
|
||||||
|
(info.Creator || "-").trim() +
|
||||||
|
"]" +
|
||||||
|
" (PDF.js: " +
|
||||||
|
(version || "-") +
|
||||||
|
(AppOptions.get("enableWebGL") ? " [WebGL]" : "") +
|
||||||
|
")"
|
||||||
|
);
|
||||||
|
|
||||||
const infoTitle = info && info["Title"];
|
let pdfTitle;
|
||||||
if (infoTitle) {
|
|
||||||
pdfTitle = infoTitle;
|
const infoTitle = info && info["Title"];
|
||||||
}
|
if (infoTitle) {
|
||||||
const metadataTitle = metadata && metadata.get("dc:title");
|
pdfTitle = infoTitle;
|
||||||
if (metadataTitle) {
|
}
|
||||||
// Ghostscript can produce invalid 'dc:title' Metadata entries:
|
const metadataTitle = metadata && metadata.get("dc:title");
|
||||||
// - The title may be "Untitled" (fixes bug 1031612).
|
if (metadataTitle) {
|
||||||
// - The title may contain incorrectly encoded characters, which thus
|
// Ghostscript can produce invalid 'dc:title' Metadata entries:
|
||||||
// looks broken, hence we ignore the Metadata entry when it
|
// - The title may be "Untitled" (fixes bug 1031612).
|
||||||
// contains characters from the Specials Unicode block
|
// - The title may contain incorrectly encoded characters, which thus
|
||||||
// (fixes bug 1605526).
|
// looks broken, hence we ignore the Metadata entry when it
|
||||||
if (
|
// contains characters from the Specials Unicode block
|
||||||
metadataTitle !== "Untitled" &&
|
// (fixes bug 1605526).
|
||||||
!/[\uFFF0-\uFFFF]/g.test(metadataTitle)
|
if (
|
||||||
) {
|
metadataTitle !== "Untitled" &&
|
||||||
pdfTitle = metadataTitle;
|
!/[\uFFF0-\uFFFF]/g.test(metadataTitle)
|
||||||
|
) {
|
||||||
|
pdfTitle = metadataTitle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pdfTitle) {
|
||||||
|
this.setTitle(
|
||||||
|
`${pdfTitle} - ${contentDispositionFilename || document.title}`
|
||||||
|
);
|
||||||
|
} else if (contentDispositionFilename) {
|
||||||
|
this.setTitle(contentDispositionFilename);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (info.IsAcroFormPresent) {
|
||||||
|
console.warn("Warning: AcroForm/XFA is not supported");
|
||||||
|
this.fallback(UNSUPPORTED_FEATURES.forms);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
typeof PDFJSDev === "undefined" ||
|
||||||
|
PDFJSDev.test("MOZCENTRAL || GENERIC")
|
||||||
|
) {
|
||||||
|
// Telemetry labels must be C++ variable friendly.
|
||||||
|
let versionId = "other";
|
||||||
|
// Keep these in sync with mozilla central's Histograms.json.
|
||||||
|
const KNOWN_VERSIONS = [
|
||||||
|
"1.0",
|
||||||
|
"1.1",
|
||||||
|
"1.2",
|
||||||
|
"1.3",
|
||||||
|
"1.4",
|
||||||
|
"1.5",
|
||||||
|
"1.6",
|
||||||
|
"1.7",
|
||||||
|
"1.8",
|
||||||
|
"1.9",
|
||||||
|
"2.0",
|
||||||
|
"2.1",
|
||||||
|
"2.2",
|
||||||
|
"2.3",
|
||||||
|
];
|
||||||
|
if (KNOWN_VERSIONS.includes(info.PDFFormatVersion)) {
|
||||||
|
versionId = `v${info.PDFFormatVersion.replace(".", "_")}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
let generatorId = "other";
|
||||||
|
// Keep these in sync with mozilla central's Histograms.json.
|
||||||
|
const KNOWN_GENERATORS = [
|
||||||
|
"acrobat distiller",
|
||||||
|
"acrobat pdfwriter",
|
||||||
|
"adobe livecycle",
|
||||||
|
"adobe pdf library",
|
||||||
|
"adobe photoshop",
|
||||||
|
"ghostscript",
|
||||||
|
"tcpdf",
|
||||||
|
"cairo",
|
||||||
|
"dvipdfm",
|
||||||
|
"dvips",
|
||||||
|
"pdftex",
|
||||||
|
"pdfkit",
|
||||||
|
"itext",
|
||||||
|
"prince",
|
||||||
|
"quarkxpress",
|
||||||
|
"mac os x",
|
||||||
|
"microsoft",
|
||||||
|
"openoffice",
|
||||||
|
"oracle",
|
||||||
|
"luradocument",
|
||||||
|
"pdf-xchange",
|
||||||
|
"antenna house",
|
||||||
|
"aspose.cells",
|
||||||
|
"fpdf",
|
||||||
|
];
|
||||||
|
if (info.Producer) {
|
||||||
|
const producer = info.Producer.toLowerCase();
|
||||||
|
KNOWN_GENERATORS.some(function(generator) {
|
||||||
|
if (!producer.includes(generator)) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
generatorId = generator.replace(/[ .\-]/g, "_");
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (pdfTitle) {
|
let formType = null;
|
||||||
this.setTitle(
|
if (info.IsAcroFormPresent) {
|
||||||
`${pdfTitle} - ${contentDispositionFilename || document.title}`
|
formType = info.IsXFAPresent ? "xfa" : "acroform";
|
||||||
);
|
}
|
||||||
} else if (contentDispositionFilename) {
|
this.externalServices.reportTelemetry({
|
||||||
this.setTitle(contentDispositionFilename);
|
type: "documentInfo",
|
||||||
}
|
version: versionId,
|
||||||
|
generator: generatorId,
|
||||||
if (info.IsAcroFormPresent) {
|
formType,
|
||||||
console.warn("Warning: AcroForm/XFA is not supported");
|
|
||||||
this.fallback(UNSUPPORTED_FEATURES.forms);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
typeof PDFJSDev === "undefined" ||
|
|
||||||
PDFJSDev.test("MOZCENTRAL || GENERIC")
|
|
||||||
) {
|
|
||||||
// Telemetry labels must be C++ variable friendly.
|
|
||||||
let versionId = "other";
|
|
||||||
// Keep these in sync with mozilla central's Histograms.json.
|
|
||||||
const KNOWN_VERSIONS = [
|
|
||||||
"1.0",
|
|
||||||
"1.1",
|
|
||||||
"1.2",
|
|
||||||
"1.3",
|
|
||||||
"1.4",
|
|
||||||
"1.5",
|
|
||||||
"1.6",
|
|
||||||
"1.7",
|
|
||||||
"1.8",
|
|
||||||
"1.9",
|
|
||||||
"2.0",
|
|
||||||
"2.1",
|
|
||||||
"2.2",
|
|
||||||
"2.3",
|
|
||||||
];
|
|
||||||
if (KNOWN_VERSIONS.includes(info.PDFFormatVersion)) {
|
|
||||||
versionId = `v${info.PDFFormatVersion.replace(".", "_")}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
let generatorId = "other";
|
|
||||||
// Keep these in sync with mozilla central's Histograms.json.
|
|
||||||
const KNOWN_GENERATORS = [
|
|
||||||
"acrobat distiller",
|
|
||||||
"acrobat pdfwriter",
|
|
||||||
"adobe livecycle",
|
|
||||||
"adobe pdf library",
|
|
||||||
"adobe photoshop",
|
|
||||||
"ghostscript",
|
|
||||||
"tcpdf",
|
|
||||||
"cairo",
|
|
||||||
"dvipdfm",
|
|
||||||
"dvips",
|
|
||||||
"pdftex",
|
|
||||||
"pdfkit",
|
|
||||||
"itext",
|
|
||||||
"prince",
|
|
||||||
"quarkxpress",
|
|
||||||
"mac os x",
|
|
||||||
"microsoft",
|
|
||||||
"openoffice",
|
|
||||||
"oracle",
|
|
||||||
"luradocument",
|
|
||||||
"pdf-xchange",
|
|
||||||
"antenna house",
|
|
||||||
"aspose.cells",
|
|
||||||
"fpdf",
|
|
||||||
];
|
|
||||||
if (info.Producer) {
|
|
||||||
const producer = info.Producer.toLowerCase();
|
|
||||||
KNOWN_GENERATORS.some(function(generator) {
|
|
||||||
if (!producer.includes(generator)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
generatorId = generator.replace(/[ .\-]/g, "_");
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let formType = null;
|
|
||||||
if (info.IsAcroFormPresent) {
|
|
||||||
formType = info.IsXFAPresent ? "xfa" : "acroform";
|
|
||||||
}
|
|
||||||
this.externalServices.reportTelemetry({
|
|
||||||
type: "documentInfo",
|
|
||||||
version: versionId,
|
|
||||||
generator: generatorId,
|
|
||||||
formType,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user