1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

[api-minor] Support the Content-Disposition filename in the Firefox PDF Viewer (bug 1694556, PR 9379 follow-up)

As can be seen [in the mozilla-central code](https://searchfox.org/mozilla-central/rev/a6db3bd67367aa9ddd9505690cab09b47e65a762/toolkit/components/pdfjs/content/PdfStreamConverter.jsm#1222-1225), we're already getting the Content-Disposition filename. However, that data isn't passed through to the viewer nor to the `PDFDataTransportStream`-implementation, which explains why it's currently being ignored.

*Please note:* This will also require a small mozilla-central patch, see https://bugzilla.mozilla.org/show_bug.cgi?id=1694556, to forward the necessary data to the viewer.
This commit is contained in:
Jonas Jenwald 2021-02-24 13:02:58 +01:00
parent 061637d3f4
commit 6fd899dc44
10 changed files with 55 additions and 30 deletions

View file

@ -334,6 +334,7 @@ function getDocument(src) {
length: params.length,
initialData: params.initialData,
progressiveDone: params.progressiveDone,
contentDispositionFilename: params.contentDispositionFilename,
disableRange: params.disableRange,
disableStream: params.disableStream,
},
@ -401,6 +402,8 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
source.length = pdfDataRangeTransport.length;
source.initialData = pdfDataRangeTransport.initialData;
source.progressiveDone = pdfDataRangeTransport.progressiveDone;
source.contentDispositionFilename =
pdfDataRangeTransport.contentDispositionFilename;
}
return worker.messageHandler
.sendWithPromise("GetDocRequest", {
@ -554,11 +557,18 @@ class PDFDataRangeTransport {
* @param {number} length
* @param {Uint8Array} initialData
* @param {boolean} [progressiveDone]
* @param {string} [contentDispositionFilename]
*/
constructor(length, initialData, progressiveDone = false) {
constructor(
length,
initialData,
progressiveDone = false,
contentDispositionFilename = null
) {
this.length = length;
this.initialData = initialData;
this.progressiveDone = progressiveDone;
this.contentDispositionFilename = contentDispositionFilename;
this._rangeListeners = [];
this._progressListeners = [];

View file

@ -451,6 +451,10 @@ function addLinkAttributes(link, { url, target, rel, enabled = true } = {}) {
link.rel = typeof rel === "string" ? rel : DEFAULT_LINK_REL;
}
function isPdfFile(filename) {
return typeof filename === "string" && /\.pdf$/i.test(filename);
}
/**
* Gets the file name from a given URL.
* @param {string} url
@ -652,6 +656,7 @@ export {
DOMSVGFactory,
getFilenameFromUrl,
isFetchSupported,
isPdfFile,
isValidFetchUrl,
LinkTarget,
loadScript,

View file

@ -19,6 +19,7 @@ import {
UnexpectedResponseException,
} from "../shared/util.js";
import { getFilenameFromContentDispositionHeader } from "./content_disposition.js";
import { isPdfFile } from "./display_utils.js";
function validateRangeRequestCapabilities({
getResponseHeader,
@ -70,7 +71,7 @@ function extractFilenameFromHeader(getResponseHeader) {
filename = decodeURIComponent(filename);
} catch (ex) {}
}
if (/\.pdf$/i.test(filename)) {
if (isPdfFile(filename)) {
return filename;
}
}
@ -82,11 +83,7 @@ function createResponseStatusError(status, url) {
return new MissingPDFException('Missing PDF "' + url + '".');
}
return new UnexpectedResponseException(
"Unexpected server response (" +
status +
') while retrieving PDF "' +
url +
'".',
`Unexpected server response (${status}) while retrieving PDF "${url}".`,
status
);
}

View file

@ -14,6 +14,7 @@
*/
import { assert, createPromiseCapability } from "../shared/util.js";
import { isPdfFile } from "./display_utils.js";
/** @implements {IPDFStream} */
class PDFDataTransportStream {
@ -25,6 +26,8 @@ class PDFDataTransportStream {
this._queuedChunks = [];
this._progressiveDone = params.progressiveDone || false;
this._contentDispositionFilename =
params.contentDispositionFilename || null;
const initialData = params.initialData;
if (initialData?.length > 0) {
@ -125,7 +128,8 @@ class PDFDataTransportStream {
return new PDFDataTransportStreamReader(
this,
queuedChunks,
this._progressiveDone
this._progressiveDone,
this._contentDispositionFilename
);
}
@ -153,10 +157,17 @@ class PDFDataTransportStream {
/** @implements {IPDFStreamReader} */
class PDFDataTransportStreamReader {
constructor(stream, queuedChunks, progressiveDone = false) {
constructor(
stream,
queuedChunks,
progressiveDone = false,
contentDispositionFilename = null
) {
this._stream = stream;
this._done = progressiveDone || false;
this._filename = null;
this._filename = isPdfFile(contentDispositionFilename)
? contentDispositionFilename
: null;
this._queuedChunks = queuedChunks || [];
this._loaded = 0;
for (const chunk of this._queuedChunks) {

View file

@ -18,6 +18,7 @@ import {
addLinkAttributes,
getFilenameFromUrl,
isFetchSupported,
isPdfFile,
isValidFetchUrl,
LinkTarget,
loadScript,
@ -128,6 +129,7 @@ export {
// From "./display/display_utils.js":
addLinkAttributes,
getFilenameFromUrl,
isPdfFile,
LinkTarget,
loadScript,
PDFDateString,