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 $fileName = $("#file-filename");
const $fileType = $("#file-filetype");
const $fileSize = $("#file-filesize");
const $downloadButton = $("#file-download");
const $openButton = $("#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();
$fileName.text(attributeMap.originalFileName || "?");
$fileSize.text((attributeMap.fileSize || "?") + " bytes");
$fileType.text(currentNote.mime);
}
$downloadButton.click(() => utils.download(getFileUrl()));
$openButton.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
}