trilium/src/public/javascripts/services/note_detail_file.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

import utils from "./utils.js";
import server from "./server.js";
import protectedSessionHolder from "./protected_session_holder.js";
import noteDetailService from "./note_detail.js";
2018-11-08 17:30:35 +08:00
const $component = $('#note-detail-file');
const $fileFileName = $("#file-filename");
const $fileFileType = $("#file-filetype");
const $fileFileSize = $("#file-filesize");
const $fileDownload = $("#file-download");
const $fileOpen = $("#file-open");
async function show() {
const currentNote = noteDetailService.getCurrentNote();
2018-08-10 02:55:16 +08:00
const attributes = await server.get('notes/' + currentNote.noteId + '/attributes');
const attributeMap = utils.toObject(attributes, l => [l.name, l.value]);
2018-11-08 17:30:35 +08:00
$component.show();
2018-08-20 03:42:03 +08:00
$fileFileName.text(attributeMap.originalFileName);
$fileFileSize.text(attributeMap.fileSize + " bytes");
$fileFileType.text(currentNote.mime);
}
$fileDownload.click(() => utils.download(getFileUrl()));
$fileOpen.click(() => {
if (utils.isElectron()) {
const open = require("open");
open(getFileUrl());
}
else {
window.location.href = getFileUrl();
}
});
function getFileUrl() {
// electron needs absolute URL so we extract current host, port, protocol
2018-04-02 08:50:58 +08:00
return utils.getHost() + "/api/notes/" + noteDetailService.getCurrentNoteId()
+ "/download?protectedSessionId=" + encodeURIComponent(protectedSessionHolder.getProtectedSessionId());
}
export default {
show,
getContent: () => null,
focus: () => null,
onNoteChange: () => null,
cleanup: () => null
}