Skip to content

Commit d3f7959

Browse files
Merge pull request #12642 from Snuffleupagus/api-getMetadata-contentLength
[api-minor] Add "contentLength" to the information returned by the `getMetadata` method
2 parents c88e805 + 01d12b4 commit d3f7959

File tree

4 files changed

+49
-51
lines changed

4 files changed

+49
-51
lines changed

src/display/api.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2654,9 +2654,8 @@ class WorkerTransport {
26542654
return {
26552655
info: results[0],
26562656
metadata: results[1] ? new Metadata(results[1]) : null,
2657-
contentDispositionFilename: this._fullReader
2658-
? this._fullReader.filename
2659-
: null,
2657+
contentDispositionFilename: this._fullReader?.filename ?? null,
2658+
contentLength: this._fullReader?.contentLength ?? null,
26602659
};
26612660
});
26622661
}

test/unit/api_spec.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,12 @@ describe("api", function () {
11321132
it("gets metadata", function (done) {
11331133
const promise = pdfDocument.getMetadata();
11341134
promise
1135-
.then(function ({ info, metadata, contentDispositionFilename }) {
1135+
.then(function ({
1136+
info,
1137+
metadata,
1138+
contentDispositionFilename,
1139+
contentLength,
1140+
}) {
11361141
expect(info.Title).toEqual("Basic API Test");
11371142
// Custom, non-standard, information dictionary entries.
11381143
expect(info.Custom).toEqual(undefined);
@@ -1147,6 +1152,7 @@ describe("api", function () {
11471152
expect(metadata.get("dc:title")).toEqual("Basic API Test");
11481153

11491154
expect(contentDispositionFilename).toEqual(null);
1155+
expect(contentLength).toEqual(basicApiFileLength);
11501156
done();
11511157
})
11521158
.catch(done.fail);
@@ -1160,7 +1166,12 @@ describe("api", function () {
11601166
.then(function (pdfDoc) {
11611167
return pdfDoc.getMetadata();
11621168
})
1163-
.then(function ({ info, metadata, contentDispositionFilename }) {
1169+
.then(function ({
1170+
info,
1171+
metadata,
1172+
contentDispositionFilename,
1173+
contentLength,
1174+
}) {
11641175
expect(info.Creator).toEqual("TeX");
11651176
expect(info.Producer).toEqual("pdfeTeX-1.21a");
11661177
expect(info.CreationDate).toEqual("D:20090401163925-07'00'");
@@ -1181,6 +1192,7 @@ describe("api", function () {
11811192

11821193
expect(metadata).toEqual(null);
11831194
expect(contentDispositionFilename).toEqual(null);
1195+
expect(contentLength).toEqual(1016315);
11841196

11851197
loadingTask.destroy().then(done);
11861198
})
@@ -1193,7 +1205,12 @@ describe("api", function () {
11931205
.then(function (pdfDoc) {
11941206
return pdfDoc.getMetadata();
11951207
})
1196-
.then(function ({ info, metadata, contentDispositionFilename }) {
1208+
.then(function ({
1209+
info,
1210+
metadata,
1211+
contentDispositionFilename,
1212+
contentLength,
1213+
}) {
11971214
// The following are PDF.js specific, non-standard, properties.
11981215
expect(info.PDFFormatVersion).toEqual(null);
11991216
expect(info.IsLinearized).toEqual(false);
@@ -1203,6 +1220,7 @@ describe("api", function () {
12031220

12041221
expect(metadata).toEqual(null);
12051222
expect(contentDispositionFilename).toEqual(null);
1223+
expect(contentLength).toEqual(624);
12061224

12071225
loadingTask.destroy().then(done);
12081226
})

web/app.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -863,15 +863,10 @@ const PDFViewerApplication = {
863863
}
864864
parameters[key] = value;
865865
}
866-
866+
// Finally, update the API parameters with the arguments (if they exist).
867867
if (args) {
868868
for (const key in args) {
869-
const value = args[key];
870-
871-
if (key === "length") {
872-
this.pdfDocumentProperties.setFileSize(value);
873-
}
874-
parameters[key] = value;
869+
parameters[key] = args[key];
875870
}
876871
}
877872

web/pdf_document_properties.js

+24-38
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,25 @@ class PDFDocumentProperties {
128128
// Get the document properties.
129129
this.pdfDocument
130130
.getMetadata()
131-
.then(({ info, metadata, contentDispositionFilename }) => {
132-
return Promise.all([
133-
info,
134-
metadata,
135-
contentDispositionFilename || getPDFFileNameFromURL(this.url),
136-
this._parseFileSize(this.maybeFileSize),
137-
this._parseDate(info.CreationDate),
138-
this._parseDate(info.ModDate),
139-
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
140-
return this._parsePageSize(
141-
getPageSizeInches(pdfPage),
142-
pagesRotation
143-
);
144-
}),
145-
this._parseLinearization(info.IsLinearized),
146-
]);
147-
})
131+
.then(
132+
({ info, metadata, contentDispositionFilename, contentLength }) => {
133+
return Promise.all([
134+
info,
135+
metadata,
136+
contentDispositionFilename || getPDFFileNameFromURL(this.url),
137+
this._parseFileSize(contentLength),
138+
this._parseDate(info.CreationDate),
139+
this._parseDate(info.ModDate),
140+
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
141+
return this._parsePageSize(
142+
getPageSizeInches(pdfPage),
143+
pagesRotation
144+
);
145+
}),
146+
this._parseLinearization(info.IsLinearized),
147+
]);
148+
}
149+
)
148150
.then(
149151
([
150152
info,
@@ -176,15 +178,13 @@ class PDFDocumentProperties {
176178
});
177179
this._updateUI();
178180

179-
// Get the correct fileSize, since it may not have been set (if
180-
// `this.setFileSize` wasn't called) or may be incorrectly set.
181-
return this.pdfDocument.getDownloadInfo();
181+
// Get the correct fileSize, since it may not have been available
182+
// or could potentially be wrong.
183+
return this.pdfDocument.getDownloadInfo().then(downloadInfo => {
184+
return this._parseFileSize(downloadInfo.length);
185+
});
182186
}
183187
)
184-
.then(({ length }) => {
185-
this.maybeFileSize = length;
186-
return this._parseFileSize(length);
187-
})
188188
.then(fileSize => {
189189
if (fileSize === this.fieldData.fileSize) {
190190
return; // The fileSize has already been correctly set.
@@ -228,27 +228,13 @@ class PDFDocumentProperties {
228228
this._dataAvailableCapability.resolve();
229229
}
230230

231-
/**
232-
* Set the file size of the PDF document. This method is used to
233-
* update the file size in the document properties overlay once it
234-
* is known so we do not have to wait until the entire file is loaded.
235-
*
236-
* @param {number} fileSize - The file size of the PDF document.
237-
*/
238-
setFileSize(fileSize) {
239-
if (Number.isInteger(fileSize) && fileSize > 0) {
240-
this.maybeFileSize = fileSize;
241-
}
242-
}
243-
244231
/**
245232
* @private
246233
*/
247234
_reset() {
248235
this.pdfDocument = null;
249236
this.url = null;
250237

251-
this.maybeFileSize = 0;
252238
delete this.fieldData;
253239
this._dataAvailableCapability = createPromiseCapability();
254240
this._currentPageNumber = 1;

0 commit comments

Comments
 (0)