From 7bd8b97dba61ab80c79836d47ab0cb46d2a6e57e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 15 Apr 2017 20:49:16 +0200 Subject: [PATCH] Change `PDFAttachmentViewer` to only open PDF attachments in the viewer, instead of downloading them, when `PDFJS.disableCreateObjectURL = false` This prevents issues with the filename detection being skipped, when trying to download the opened PDF attachment, since `getPDFFileNameFromURL` ignores `data:` URLs for performance reasons. --- web/pdf_attachment_viewer.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index c8a3b034b..3720b4751 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -79,13 +79,15 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() { /** * @private */ - _bindPdfLink: - function PDFAttachmentViewer_bindPdfLink(button, content, filename) { + _bindPdfLink(button, content, filename) { + if (PDFJS.disableCreateObjectURL) { + throw new Error('bindPdfLink: ' + + 'Unsupported "PDFJS.disableCreateObjectURL" value.'); + } var blobUrl; button.onclick = function() { if (!blobUrl) { - blobUrl = createObjectURL( - content, 'application/pdf', PDFJS.disableCreateObjectURL); + blobUrl = createObjectURL(content, 'application/pdf'); } var viewerUrl; if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) { @@ -97,10 +99,8 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() { // eslint-disable-next-line no-undef viewerUrl = chrome.runtime.getURL('/content/web/viewer.html') + '?file=' + encodeURIComponent(blobUrl + '#' + filename); - } else { + } else if (PDFJSDev.test('FIREFOX || MOZCENTRAL')) { // Let Firefox's content handler catch the URL and display the PDF. - // In Firefox PDFJS.disableCreateObjectURL is always false, so - // blobUrl is always a blob:-URL and never a data:-URL. viewerUrl = blobUrl + '?' + encodeURIComponent(filename); } window.open(viewerUrl); @@ -151,7 +151,7 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() { div.className = 'attachmentsItem'; var button = document.createElement('button'); button.textContent = filename; - if (/\.pdf$/i.test(filename)) { + if (/\.pdf$/i.test(filename) && !PDFJS.disableCreateObjectURL) { this._bindPdfLink(button, item.content, filename); } else { this._bindLink(button, item.content, filename);