Skip to content

Commit

Permalink
[api-minor] Add "contentLength" to the information returned by the `g…
Browse files Browse the repository at this point in the history
…etMetadata` method

Given that we already include the "Content-Disposition"-header filename, when it exists, it shouldn't hurt to also include the information from the "Content-Length"-header.
For PDF documents opened via a URL, which should be a very common way for the PDF.js library to be used, this will[1] thus provide a way of getting the PDF filesize without having to wait for the `getDownloadInfo`-promise to resolve[2].

With these API improvements, we can also simplify the filesize handling in the `PDFDocumentProperties` class.

---
[1] Assuming that the server is correctly configured, of course.

[2] Since that's not *guaranteed* to happen in general, with e.g. `disableAutoFetch = true` set.
  • Loading branch information
Snuffleupagus committed Nov 20, 2020
1 parent c88e805 commit 01d12b4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 51 deletions.
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

0 comments on commit 01d12b4

Please sign in to comment.