From e7aa84435bcffaf8ac9ce4e8798fcdab202c5cce Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 8 Mar 2020 18:06:24 +0100 Subject: [PATCH] render PDF preview in electron for pdf notes --- .../javascripts/widgets/type_widgets/file.js | 31 +++++++++++-------- src/public/stylesheets/detail.css | 3 ++ src/routes/api/files.js | 17 +++++++--- src/routes/routes.js | 1 + 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/public/javascripts/widgets/type_widgets/file.js b/src/public/javascripts/widgets/type_widgets/file.js index 45da692fb..1facde24a 100644 --- a/src/public/javascripts/widgets/type_widgets/file.js +++ b/src/public/javascripts/widgets/type_widgets/file.js @@ -9,30 +9,30 @@ const TPL = ` Note ID: - - Original file name: File type: - - File size:

+    
+    
 
-    
-     
-    
-     
-    
-
-    
+    
+ +   + +   + + + +
`; export default class FileTypeWidget extends TypeWidget { @@ -45,6 +45,7 @@ export default class FileTypeWidget extends TypeWidget { this.$fileType = this.$widget.find(".file-filetype"); this.$fileSize = this.$widget.find(".file-filesize"); this.$previewContent = this.$widget.find(".file-preview-content"); + this.$pdfPreview = this.$widget.find(".pdf-preview"); this.$downloadButton = this.$widget.find(".file-download"); this.$openButton = this.$widget.find(".file-open"); this.$uploadNewRevisionButton = this.$widget.find(".file-upload-new-revision"); @@ -110,12 +111,16 @@ export default class FileTypeWidget extends TypeWidget { const noteComplement = await this.tabContext.getNoteComplement(); + this.$previewContent.empty().hide(); + this.$pdfPreview.attr('src', '').empty().hide(); + if (noteComplement.content) { this.$previewContent.show(); this.$previewContent.text(noteComplement.content); } - else { - this.$previewContent.empty().hide(); + else if (note.mime === 'application/pdf' && utils.isElectron()) { + this.$pdfPreview.show(); + this.$pdfPreview.attr("src", utils.getUrlForDownload("api/notes/" + this.noteId + "/open")); } // open doesn't work for protected notes since it works through browser which isn't in protected session diff --git a/src/public/stylesheets/detail.css b/src/public/stylesheets/detail.css index faa58219c..0e45a201b 100644 --- a/src/public/stylesheets/detail.css +++ b/src/public/stylesheets/detail.css @@ -76,6 +76,9 @@ .note-detail-file { padding: 10px; + display: flex; + flex-direction: column; + height: 100%; } .file-table th, .file-table td { diff --git a/src/routes/api/files.js b/src/routes/api/files.js index 5df06f179..54ba741fb 100644 --- a/src/routes/api/files.js +++ b/src/routes/api/files.js @@ -31,7 +31,7 @@ async function updateFile(req) { }; } -async function downloadNoteFile(noteId, res) { +async function downloadNoteFile(noteId, res, contentDisposition = true) { const note = await repository.getNote(noteId); if (!note) { @@ -42,9 +42,12 @@ async function downloadNoteFile(noteId, res) { return res.status(401).send("Protected session not available"); } - // (one) reason we're not using the originFileName (available as label) is that it's not - // available for older note revisions and thus would be inconsistent - res.setHeader('Content-Disposition', utils.getContentDisposition(note.title || "untitled")); + if (contentDisposition) { + // (one) reason we're not using the originFileName (available as label) is that it's not + // available for older note revisions and thus would be inconsistent + res.setHeader('Content-Disposition', utils.getContentDisposition(note.title || "untitled")); + } + res.setHeader('Content-Type', note.mime); res.send(await note.getContent()); @@ -54,11 +57,17 @@ async function downloadFile(req, res) { const noteId = req.params.noteId; return await downloadNoteFile(noteId, res); +} +async function openFile(req, res) { + const noteId = req.params.noteId; + + return await downloadNoteFile(noteId, res, false); } module.exports = { updateFile, + openFile, downloadFile, downloadNoteFile }; \ No newline at end of file diff --git a/src/routes/routes.js b/src/routes/routes.js index 53a01e3a1..69ffb581d 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -156,6 +156,7 @@ function register(app) { route(PUT, '/api/notes/:noteId/file', [auth.checkApiAuthOrElectron, uploadMiddleware, csrfMiddleware], filesRoute.updateFile, apiResultHandler); + route(GET, '/api/notes/:noteId/open', [auth.checkApiAuthOrElectron], filesRoute.openFile); route(GET, '/api/notes/:noteId/download', [auth.checkApiAuthOrElectron], filesRoute.downloadFile); // this "hacky" path is used for easier referencing of CSS resources route(GET, '/api/notes/download/:noteId', [auth.checkApiAuthOrElectron], filesRoute.downloadFile);