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:
parent
061637d3f4
commit
6fd899dc44
10 changed files with 55 additions and 30 deletions
|
@ -44,6 +44,7 @@ import {
|
|||
getFilenameFromUrl,
|
||||
GlobalWorkerOptions,
|
||||
InvalidPDFException,
|
||||
isPdfFile,
|
||||
LinkTarget,
|
||||
loadScript,
|
||||
MissingPDFException,
|
||||
|
@ -727,7 +728,10 @@ const PDFViewerApplication = {
|
|||
onOpenWithTransport: (url, length, transport) => {
|
||||
this.open(url, { length, range: transport });
|
||||
},
|
||||
onOpenWithData: data => {
|
||||
onOpenWithData: (data, contentDispositionFilename) => {
|
||||
if (isPdfFile(contentDispositionFilename)) {
|
||||
this._contentDispositionFilename = contentDispositionFilename;
|
||||
}
|
||||
this.open(data);
|
||||
},
|
||||
onOpenWithURL: (url, length, originalUrl) => {
|
||||
|
@ -1744,7 +1748,7 @@ const PDFViewerApplication = {
|
|||
}
|
||||
this.documentInfo = info;
|
||||
this.metadata = metadata;
|
||||
this._contentDispositionFilename = contentDispositionFilename;
|
||||
this._contentDispositionFilename ??= contentDispositionFilename;
|
||||
this._contentLength ??= contentLength; // See `getDownloadInfo`-call above.
|
||||
|
||||
// Provides some basic debug information
|
||||
|
|
|
@ -13,8 +13,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { createObjectURL, createValidAbsoluteUrl } from "pdfjs-lib";
|
||||
import { PdfFileRegExp } from "./ui_utils.js";
|
||||
import { createObjectURL, createValidAbsoluteUrl, isPdfFile } from "pdfjs-lib";
|
||||
import { viewerCompatibilityParams } from "./viewer_compatibility.js";
|
||||
|
||||
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("CHROME || GENERIC")) {
|
||||
|
@ -68,10 +67,10 @@ class DownloadManager {
|
|||
* @returns {boolean} Indicating if the data was opened.
|
||||
*/
|
||||
openOrDownloadData(element, data, filename) {
|
||||
const isPdfFile = PdfFileRegExp.test(filename);
|
||||
const contentType = isPdfFile ? "application/pdf" : "";
|
||||
const isPdfData = isPdfFile(filename);
|
||||
const contentType = isPdfData ? "application/pdf" : "";
|
||||
|
||||
if (isPdfFile && !viewerCompatibilityParams.disableCreateObjectURL) {
|
||||
if (isPdfData && !viewerCompatibilityParams.disableCreateObjectURL) {
|
||||
let blobUrl = this._openBlobUrls.get(element);
|
||||
if (!blobUrl) {
|
||||
blobUrl = URL.createObjectURL(new Blob([data], { type: contentType }));
|
||||
|
|
|
@ -14,10 +14,10 @@
|
|||
*/
|
||||
|
||||
import "../extensions/firefox/tools/l10n.js";
|
||||
import { DEFAULT_SCALE_VALUE, PdfFileRegExp } from "./ui_utils.js";
|
||||
import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
|
||||
import { PDFDataRangeTransport, shadow } from "pdfjs-lib";
|
||||
import { isPdfFile, PDFDataRangeTransport, shadow } from "pdfjs-lib";
|
||||
import { BasePreferences } from "./preferences.js";
|
||||
import { DEFAULT_SCALE_VALUE } from "./ui_utils.js";
|
||||
|
||||
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
|
||||
throw new Error(
|
||||
|
@ -129,10 +129,10 @@ class DownloadManager {
|
|||
* @returns {boolean} Indicating if the data was opened.
|
||||
*/
|
||||
openOrDownloadData(element, data, filename) {
|
||||
const isPdfFile = PdfFileRegExp.test(filename);
|
||||
const contentType = isPdfFile ? "application/pdf" : "";
|
||||
const isPdfData = isPdfFile(filename);
|
||||
const contentType = isPdfData ? "application/pdf" : "";
|
||||
|
||||
if (isPdfFile) {
|
||||
if (isPdfData) {
|
||||
let blobUrl = this._openBlobUrls.get(element);
|
||||
if (!blobUrl) {
|
||||
blobUrl = URL.createObjectURL(new Blob([data], { type: contentType }));
|
||||
|
@ -332,7 +332,8 @@ class FirefoxExternalServices extends DefaultExternalServices {
|
|||
pdfDataRangeTransport = new FirefoxComDataRangeTransport(
|
||||
args.length,
|
||||
args.data,
|
||||
args.done
|
||||
args.done,
|
||||
args.filename
|
||||
);
|
||||
|
||||
callbacks.onOpenWithTransport(
|
||||
|
@ -367,7 +368,7 @@ class FirefoxExternalServices extends DefaultExternalServices {
|
|||
callbacks.onError(args.errorCode);
|
||||
break;
|
||||
}
|
||||
callbacks.onOpenWithData(args.data);
|
||||
callbacks.onOpenWithData(args.data, args.filename);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -119,8 +119,8 @@ class PDFAttachmentViewer extends BaseTreeViewer {
|
|||
let attachmentsCount = 0;
|
||||
for (const name of names) {
|
||||
const item = attachments[name];
|
||||
const content = item.content;
|
||||
const filename = getFilenameFromUrl(item.filename);
|
||||
const content = item.content,
|
||||
filename = getFilenameFromUrl(item.filename);
|
||||
|
||||
const div = document.createElement("div");
|
||||
div.className = "treeItem";
|
||||
|
|
|
@ -69,9 +69,6 @@ const SpreadMode = {
|
|||
// Used by `PDFViewerApplication`, and by the API unit-tests.
|
||||
const AutoPrintRegExp = /\bprint\s*\(/;
|
||||
|
||||
// Used by the (various) `DownloadManager`-implementations.
|
||||
const PdfFileRegExp = /\.pdf$/i;
|
||||
|
||||
// Replaces {{arguments}} with their values.
|
||||
function formatL10nValue(text, args) {
|
||||
if (!args) {
|
||||
|
@ -1062,7 +1059,6 @@ export {
|
|||
normalizeWheelEventDirection,
|
||||
NullL10n,
|
||||
parseQueryString,
|
||||
PdfFileRegExp,
|
||||
PresentationModeState,
|
||||
ProgressBar,
|
||||
RendererType,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue