From a23079c2dd0ea039ee9b2d80de8ab52ca204821d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 27 Aug 2020 16:00:12 +0200 Subject: [PATCH] Ensure that `PDFAttachmentViewer._bindLink` assigns the correct contentType when downloading PDF attachments This should provide better filetype detection when downloading PDF attachments in the viewer. Also, to avoid creating the "is PDF file" regular expression more than once it's extracted into a global constant instead. --- web/pdf_attachment_viewer.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index 0543cd6c5..6646830cd 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -17,6 +17,8 @@ import { createPromiseCapability, getFilenameFromUrl } from "pdfjs-lib"; import { BaseTreeViewer } from "./base_tree_viewer.js"; import { viewerCompatibilityParams } from "./viewer_compatibility.js"; +const PdfFileRegExp = /\.pdf$/i; + /** * @typedef {Object} PDFAttachmentViewerOptions * @property {HTMLDivElement} container - The viewer element. @@ -136,7 +138,8 @@ class PDFAttachmentViewer extends BaseTreeViewer { */ _bindLink(element, { content, filename }) { element.onclick = () => { - this.downloadManager.downloadData(content, filename, ""); + const contentType = PdfFileRegExp.test(filename) ? "application/pdf" : ""; + this.downloadManager.downloadData(content, filename, contentType); return false; }; } @@ -169,7 +172,7 @@ class PDFAttachmentViewer extends BaseTreeViewer { const element = document.createElement("a"); if ( - /\.pdf$/i.test(filename) && + PdfFileRegExp.test(filename) && !viewerCompatibilityParams.disableCreateObjectURL ) { this._bindPdfLink(element, { content: item.content, filename });