Merge pull request #12645 from Snuffleupagus/async-PDFDocumentProperties-open

Convert the `PDFDocumentProperties.open` method to be async
This commit is contained in:
Tim van der Meij 2020-11-21 14:42:48 +01:00 committed by GitHub
commit d88d47d621
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,7 +97,7 @@ class PDFDocumentProperties {
/** /**
* Open the document properties overlay. * Open the document properties overlay.
*/ */
open() { async open() {
const freezeFieldData = data => { const freezeFieldData = data => {
Object.defineProperty(this, "fieldData", { Object.defineProperty(this, "fieldData", {
value: Object.freeze(data), value: Object.freeze(data),
@ -107,10 +107,10 @@ class PDFDocumentProperties {
}); });
}; };
Promise.all([ await Promise.all([
this.overlayManager.open(this.overlayName), this.overlayManager.open(this.overlayName),
this._dataAvailableCapability.promise, this._dataAvailableCapability.promise,
]).then(() => { ]);
const currentPageNumber = this._currentPageNumber; const currentPageNumber = this._currentPageNumber;
const pagesRotation = this._pagesRotation; const pagesRotation = this._pagesRotation;
@ -126,38 +126,31 @@ class PDFDocumentProperties {
} }
// Get the document properties. // Get the document properties.
this.pdfDocument const {
.getMetadata()
.then(
({ info, metadata, contentDispositionFilename, contentLength }) => {
return Promise.all([
info, info,
metadata, /* metadata, */
contentDispositionFilename,
contentLength,
} = await this.pdfDocument.getMetadata();
const [
fileName,
fileSize,
creationDate,
modificationDate,
pageSize,
isLinearized,
] = await Promise.all([
contentDispositionFilename || getPDFFileNameFromURL(this.url), contentDispositionFilename || getPDFFileNameFromURL(this.url),
this._parseFileSize(contentLength), this._parseFileSize(contentLength),
this._parseDate(info.CreationDate), this._parseDate(info.CreationDate),
this._parseDate(info.ModDate), this._parseDate(info.ModDate),
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => { this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
return this._parsePageSize( return this._parsePageSize(getPageSizeInches(pdfPage), pagesRotation);
getPageSizeInches(pdfPage),
pagesRotation
);
}), }),
this._parseLinearization(info.IsLinearized), this._parseLinearization(info.IsLinearized),
]); ]);
}
)
.then(
([
info,
metadata,
fileName,
fileSize,
creationDate,
modDate,
pageSize,
isLinearized,
]) => {
freezeFieldData({ freezeFieldData({
fileName, fileName,
fileSize, fileSize,
@ -166,7 +159,7 @@ class PDFDocumentProperties {
subject: info.Subject, subject: info.Subject,
keywords: info.Keywords, keywords: info.Keywords,
creationDate, creationDate,
modificationDate: modDate, modificationDate,
creator: info.Creator, creator: info.Creator,
producer: info.Producer, producer: info.Producer,
version: info.PDFFormatVersion, version: info.PDFFormatVersion,
@ -180,22 +173,15 @@ class PDFDocumentProperties {
// Get the correct fileSize, since it may not have been available // Get the correct fileSize, since it may not have been available
// or could potentially be wrong. // or could potentially be wrong.
return this.pdfDocument.getDownloadInfo().then(downloadInfo => { const { length } = await this.pdfDocument.getDownloadInfo();
return this._parseFileSize(downloadInfo.length); if (contentLength === length) {
});
}
)
.then(fileSize => {
if (fileSize === this.fieldData.fileSize) {
return; // The fileSize has already been correctly set. return; // The fileSize has already been correctly set.
} }
const data = Object.assign(Object.create(null), this.fieldData); const data = Object.assign(Object.create(null), this.fieldData);
data.fileSize = fileSize; data.fileSize = await this._parseFileSize(length);
freezeFieldData(data); freezeFieldData(data);
this._updateUI(); this._updateUI();
});
});
} }
/** /**