Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] Add "contentLength" to the information returned by the getMetadata method #12642

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2654,9 +2654,8 @@ class WorkerTransport {
return {
info: results[0],
metadata: results[1] ? new Metadata(results[1]) : null,
contentDispositionFilename: this._fullReader
? this._fullReader.filename
: null,
contentDispositionFilename: this._fullReader?.filename ?? null,
contentLength: this._fullReader?.contentLength ?? null,
};
});
}
Expand Down
24 changes: 21 additions & 3 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,12 @@ describe("api", function () {
it("gets metadata", function (done) {
const promise = pdfDocument.getMetadata();
promise
.then(function ({ info, metadata, contentDispositionFilename }) {
.then(function ({
info,
metadata,
contentDispositionFilename,
contentLength,
}) {
expect(info.Title).toEqual("Basic API Test");
// Custom, non-standard, information dictionary entries.
expect(info.Custom).toEqual(undefined);
Expand All @@ -1147,6 +1152,7 @@ describe("api", function () {
expect(metadata.get("dc:title")).toEqual("Basic API Test");

expect(contentDispositionFilename).toEqual(null);
expect(contentLength).toEqual(basicApiFileLength);
done();
})
.catch(done.fail);
Expand All @@ -1160,7 +1166,12 @@ describe("api", function () {
.then(function (pdfDoc) {
return pdfDoc.getMetadata();
})
.then(function ({ info, metadata, contentDispositionFilename }) {
.then(function ({
info,
metadata,
contentDispositionFilename,
contentLength,
}) {
expect(info.Creator).toEqual("TeX");
expect(info.Producer).toEqual("pdfeTeX-1.21a");
expect(info.CreationDate).toEqual("D:20090401163925-07'00'");
Expand All @@ -1181,6 +1192,7 @@ describe("api", function () {

expect(metadata).toEqual(null);
expect(contentDispositionFilename).toEqual(null);
expect(contentLength).toEqual(1016315);

loadingTask.destroy().then(done);
})
Expand All @@ -1193,7 +1205,12 @@ describe("api", function () {
.then(function (pdfDoc) {
return pdfDoc.getMetadata();
})
.then(function ({ info, metadata, contentDispositionFilename }) {
.then(function ({
info,
metadata,
contentDispositionFilename,
contentLength,
}) {
// The following are PDF.js specific, non-standard, properties.
expect(info.PDFFormatVersion).toEqual(null);
expect(info.IsLinearized).toEqual(false);
Expand All @@ -1203,6 +1220,7 @@ describe("api", function () {

expect(metadata).toEqual(null);
expect(contentDispositionFilename).toEqual(null);
expect(contentLength).toEqual(624);

loadingTask.destroy().then(done);
})
Expand Down
9 changes: 2 additions & 7 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,15 +863,10 @@ const PDFViewerApplication = {
}
parameters[key] = value;
}

// Finally, update the API parameters with the arguments (if they exist).
if (args) {
for (const key in args) {
const value = args[key];

if (key === "length") {
this.pdfDocumentProperties.setFileSize(value);
}
parameters[key] = value;
parameters[key] = args[key];
}
}

Expand Down
62 changes: 24 additions & 38 deletions web/pdf_document_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,25 @@ class PDFDocumentProperties {
// Get the document properties.
this.pdfDocument
.getMetadata()
.then(({ info, metadata, contentDispositionFilename }) => {
return Promise.all([
info,
metadata,
contentDispositionFilename || getPDFFileNameFromURL(this.url),
this._parseFileSize(this.maybeFileSize),
this._parseDate(info.CreationDate),
this._parseDate(info.ModDate),
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
return this._parsePageSize(
getPageSizeInches(pdfPage),
pagesRotation
);
}),
this._parseLinearization(info.IsLinearized),
]);
})
.then(
({ info, metadata, contentDispositionFilename, contentLength }) => {
return Promise.all([
info,
metadata,
contentDispositionFilename || getPDFFileNameFromURL(this.url),
this._parseFileSize(contentLength),
this._parseDate(info.CreationDate),
this._parseDate(info.ModDate),
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
return this._parsePageSize(
getPageSizeInches(pdfPage),
pagesRotation
);
}),
this._parseLinearization(info.IsLinearized),
]);
}
)
.then(
([
info,
Expand Down Expand Up @@ -176,15 +178,13 @@ class PDFDocumentProperties {
});
this._updateUI();

// Get the correct fileSize, since it may not have been set (if
// `this.setFileSize` wasn't called) or may be incorrectly set.
return this.pdfDocument.getDownloadInfo();
// Get the correct fileSize, since it may not have been available
// or could potentially be wrong.
return this.pdfDocument.getDownloadInfo().then(downloadInfo => {
return this._parseFileSize(downloadInfo.length);
});
}
)
.then(({ length }) => {
this.maybeFileSize = length;
return this._parseFileSize(length);
})
.then(fileSize => {
if (fileSize === this.fieldData.fileSize) {
return; // The fileSize has already been correctly set.
Expand Down Expand Up @@ -228,27 +228,13 @@ class PDFDocumentProperties {
this._dataAvailableCapability.resolve();
}

/**
* Set the file size of the PDF document. This method is used to
* update the file size in the document properties overlay once it
* is known so we do not have to wait until the entire file is loaded.
*
* @param {number} fileSize - The file size of the PDF document.
*/
setFileSize(fileSize) {
if (Number.isInteger(fileSize) && fileSize > 0) {
this.maybeFileSize = fileSize;
}
}

/**
* @private
*/
_reset() {
this.pdfDocument = null;
this.url = null;

this.maybeFileSize = 0;
delete this.fieldData;
this._dataAvailableCapability = createPromiseCapability();
this._currentPageNumber = 1;
Expand Down