Skip to content

Build your personal knowledge base with Trilium Notes

attachment download now works also in electron, added option to open the attachment

azivneraa2bbc65758a
diff --git a/electron.js b/electron.js
index 3190b96eee..86d93bd493 100644
--- a/electron.js
+++ b/electron.js
@@ -15,6 +15,8 @@ require('electron-debug')();
// Prevent window being garbage collected
let mainWindow;
+require('electron-dl')({ saveAs: true });
+
function onClosed() {
// Dereference the window
// For multiple windows store them in an array
diff --git a/package-lock.json b/package-lock.json
index d1d224650d..6625326be7 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "trilium",
- "version": "0.6.1",
+ "version": "0.6.2",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@@ -3206,6 +3206,16 @@
"electron-localshortcut": "3.1.0"
}
},
+ "electron-dl": {
+ "version": "1.11.0",
+ "resolved": "https://registry.npmjs.org/electron-dl/-/electron-dl-1.11.0.tgz",
+ "integrity": "sha512-iL9qHzzWOuL9bus+UT+P72SwrDQcFTV6QHqcbhwgqjCC9/K5jhdRzG0dIMB3TzYlk6rmApanPqh9DvWykwIH1Q==",
+ "requires": {
+ "ext-name": "5.0.0",
+ "pupa": "1.0.0",
+ "unused-filename": "1.0.0"
+ }
+ },
"electron-download": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/electron-download/-/electron-download-3.3.0.tgz",
@@ -4374,6 +4384,23 @@
}
}
},
+ "ext-list": {
+ "version": "2.2.2",
+ "resolved": "https://registry.npmjs.org/ext-list/-/ext-list-2.2.2.tgz",
+ "integrity": "sha512-u+SQgsubraE6zItfVA0tBuCBhfU9ogSRnsvygI7wht9TS510oLkBRXBsqopeUG/GBOIQyKZO9wjTqIu/sf5zFA==",
+ "requires": {
+ "mime-db": "1.30.0"
+ }
+ },
+ "ext-name": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/ext-name/-/ext-name-5.0.0.tgz",
+ "integrity": "sha512-yblEwXAbGv1VQDmow7s38W77hzAgJAO50ztBLMcUyUBfxv1HC+LGwtiEN+Co6LtlqT/5uwVOxsD4TNIilWhwdQ==",
+ "requires": {
+ "ext-list": "2.2.2",
+ "sort-keys-length": "1.0.1"
+ }
+ },
"extend": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz",
@@ -5922,8 +5949,7 @@
"is-plain-obj": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz",
- "integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4=",
- "dev": true
+ "integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4="
},
"is-png": {
"version": "1.1.0",
@@ -7152,6 +7178,11 @@
}
}
},
+ "modify-filename": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/modify-filename/-/modify-filename-1.1.0.tgz",
+ "integrity": "sha1-mi3sg4Bvuy2XXyK+7IWcoms5OqE="
+ },
"moment": {
"version": "2.20.1",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.20.1.tgz",
@@ -7564,6 +7595,11 @@
"mimic-fn": "1.1.0"
}
},
+ "open": {
+ "version": "0.0.5",
+ "resolved": "https://registry.npmjs.org/open/-/open-0.0.5.tgz",
+ "integrity": "sha1-QsPhjslUZra/DcQvOilFw/DK2Pw="
+ },
"optimist": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz",
@@ -8391,6 +8427,11 @@
"resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz",
"integrity": "sha1-wNWmOycYgArY4esPpSachN1BhF4="
},
+ "pupa": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/pupa/-/pupa-1.0.0.tgz",
+ "integrity": "sha1-mpVopa9+ZXuEYqbp1TKHQ1YM7/Y="
+ },
"q": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/q/-/q-1.5.1.tgz",
@@ -9186,11 +9227,18 @@
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/sort-keys/-/sort-keys-1.1.2.tgz",
"integrity": "sha1-RBttTTRnmPG05J6JIK37oOVD+a0=",
- "dev": true,
"requires": {
"is-plain-obj": "1.1.0"
}
},
+ "sort-keys-length": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/sort-keys-length/-/sort-keys-length-1.0.1.tgz",
+ "integrity": "sha1-nLb09OnkgVWmqgZx7dM2/xR5oYg=",
+ "requires": {
+ "sort-keys": "1.1.2"
+ }
+ },
"source-map": {
"version": "0.5.7",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz",
@@ -10963,6 +11011,22 @@
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
"integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw="
},
+ "unused-filename": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/unused-filename/-/unused-filename-1.0.0.tgz",
+ "integrity": "sha1-00CID3GuIRXrqhMlvvBcxmhEacY=",
+ "requires": {
+ "modify-filename": "1.1.0",
+ "path-exists": "3.0.0"
+ },
+ "dependencies": {
+ "path-exists": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz",
+ "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU="
+ }
+ }
+ },
"unzip-response": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/unzip-response/-/unzip-response-2.0.1.tgz",
diff --git a/package.json b/package.json
index f97cb465e3..8a82ce8c14 100644
--- a/package.json
+++ b/package.json
@@ -29,6 +29,7 @@
"ejs": "~2.5.7",
"electron": "^1.8.2",
"electron-debug": "^1.5.0",
+ "electron-dl": "^1.11.0",
"electron-in-page-search": "^1.2.4",
"express": "~4.16.2",
"express-promise-wrap": "^0.2.2",
@@ -45,6 +46,7 @@
"jimp": "^0.2.28",
"moment": "^2.20.1",
"multer": "^1.3.0",
+ "open": "0.0.5",
"rand-token": "^0.4.0",
"request": "^2.83.0",
"request-promise": "^4.2.2",
diff --git a/src/public/javascripts/note_editor.js b/src/public/javascripts/note_editor.js
index c130ac5831..0f8f43b992 100644
--- a/src/public/javascripts/note_editor.js
+++ b/src/public/javascripts/note_editor.js
@@ -18,6 +18,7 @@ const noteEditor = (function() {
const $attachmentFileType = $("#attachment-filetype");
const $attachmentFileSize = $("#attachment-filesize");
const $attachmentDownload = $("#attachment-download");
+ const $attachmentOpen = $("#attachment-open");
let editor = null;
let codeEditor = null;
@@ -271,9 +272,37 @@ const noteEditor = (function() {
}
$attachmentDownload.click(() => {
- window.location.href = "/api/attachments/download/" + getCurrentNoteId();
+ if (isElectron()) {
+ const remote = require('electron').remote;
+
+ remote.getCurrentWebContents().downloadURL(getAttachmentUrl());
+ }
+ else {
+ window.location.href = getAttachmentUrl();
+ }
+ });
+
+ $attachmentOpen.click(() => {
+ if (isElectron()) {
+ const open = require("open");
+
+ open(getAttachmentUrl());
+ }
+ else {
+ window.location.href = getAttachmentUrl();
+ }
});
+ function getAttachmentUrl() {
+ // electron needs absolute URL so we extract current host, port, protocol
+ const url = new URL(window.location.href);
+ const host = url.protocol + "//" + url.hostname + ":" + url.port;
+
+ const downloadUrl = "/api/attachments/download/" + getCurrentNoteId();
+
+ return host + downloadUrl;
+ }
+
$(document).ready(() => {
$noteTitle.on('input', () => {
noteChanged();
diff --git a/src/views/index.ejs b/src/views/index.ejs
index ac2d9648e9..d6ad8f59f3 100644
--- a/src/views/index.ejs
+++ b/src/views/index.ejs
@@ -160,6 +160,8 @@
<tr>
<td>
<button id="attachment-download" class="btn btn-primary" type="button">Download</button>
+ &nbsp;
+ <button id="attachment-open" class="btn btn-primary" type="button">Open</button>
</td>
</tr>
</table>