174 lines
5.4 KiB
JavaScript
Raw Normal View History

import utils from "./utils.js";
import server from "./server.js";
2023-05-03 10:23:20 +02:00
function checkType(type) {
if (type !== 'notes' && type !== 'attachments') {
throw new Error(`Unrecognized type '${type}', should be 'notes' or 'attachments'`);
}
}
function getFileUrl(type, noteId) {
checkType(type);
return getUrlForDownload(`api/${type}/${noteId}/download`);
}
2023-05-03 10:23:20 +02:00
function getOpenFileUrl(type, noteId) {
checkType(type);
return getUrlForDownload(`api/${type}/${noteId}/open`);
}
function download(url) {
if (utils.isElectron()) {
2021-11-16 22:43:08 +01:00
const remote = utils.dynamicRequire('@electron/remote');
remote.getCurrentWebContents().downloadURL(url);
} else {
window.location.href = url;
}
}
function downloadFileNote(noteId) {
2023-05-03 10:23:20 +02:00
const url = `${getFileUrl('notes', noteId)}?${Date.now()}`; // don't use cache
download(url);
}
2023-05-03 10:23:20 +02:00
function downloadAttachment(attachmentId) {
const url = `${getFileUrl('attachments', attachmentId)}?${Date.now()}`; // don't use cache
2023-05-03 10:23:20 +02:00
download(url);
}
2023-05-17 23:57:32 +02:00
async function openNoteCustom(noteId) {
if (!utils.isElectron() || utils.isMac()) {
return;
}
const resp = await server.post(`notes/${noteId}/save-to-tmp-dir`);
let filePath = resp.tmpFilePath;
const {exec} = utils.dynamicRequire('child_process');
const platform = process.platform;
if (platform === 'linux') {
// we don't know which terminal is available, try in succession
const terminals = ['x-terminal-emulator', 'gnome-terminal', 'konsole', 'xterm', 'xfce4-terminal', 'mate-terminal', 'rxvt', 'terminator', 'terminology'];
2023-05-16 11:57:28 +00:00
const openFileWithTerminal = (terminal) => {
2023-05-17 23:57:32 +02:00
const command = `${terminal} -e 'mimeopen -d "${filePath}"'`;
console.log(`Open Note custom: ${command} `);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Open Note custom: Failed to open file with ${terminal}: ${error}`);
searchTerminal(terminals.indexOf(terminal) + 1);
} else {
console.log(`Open Note custom: File opened with ${terminal}: ${stdout}`);
}
});
2023-05-16 11:57:28 +00:00
};
2023-05-17 23:57:32 +02:00
2023-05-16 11:57:28 +00:00
const searchTerminal = (index) => {
2023-05-17 23:57:32 +02:00
const terminal = terminals[index];
if (!terminal) {
console.error('Open Note custom: No terminal found!');
open(getFileUrl(noteId), {url: true});
return;
2023-05-16 11:57:28 +00:00
}
2023-05-17 23:57:32 +02:00
exec(`which ${terminal}`, (error, stdout, stderr) => {
if (stdout.trim()) {
openFileWithTerminal(terminal);
} else {
searchTerminal(index + 1);
}
});
2023-05-16 11:57:28 +00:00
};
searchTerminal(0);
2023-05-17 23:57:32 +02:00
} else if (platform === 'win32') {
2023-05-16 11:57:28 +00:00
if (filePath.indexOf("/") !== -1) {
2023-05-17 23:57:32 +02:00
// Note that the path separator must be \ instead of /
filePath = filePath.replace(/\//g, "\\");
2023-05-16 11:57:28 +00:00
}
const command = `rundll32.exe shell32.dll,OpenAs_RunDLL ` + filePath;
exec(command, (err, stdout, stderr) => {
2023-05-17 23:57:32 +02:00
if (err) {
console.error("Open Note custom: ", err);
open(getFileUrl(noteId), {url: true});
return;
}
2023-05-16 11:57:28 +00:00
});
2023-05-17 23:57:32 +02:00
} else {
2023-05-16 11:57:28 +00:00
console.log('Currently "Open Note custom" only supports linux and windows systems');
2023-05-17 23:57:32 +02:00
open(getFileUrl(noteId), {url: true});
2023-05-16 11:57:28 +00:00
}
2023-05-17 23:57:32 +02:00
}
2023-05-16 11:57:28 +00:00
function downloadRevision(noteId, revisionId) {
const url = getUrlForDownload(`api/revisions/${revisionId}/download`);
download(url);
}
/**
* @param url - should be without initial slash!!!
*/
function getUrlForDownload(url) {
if (utils.isElectron()) {
// electron needs absolute URL, so we extract current host, port, protocol
return `${getHost()}/${url}`;
}
else {
2023-06-29 23:32:19 +02:00
// web server can be deployed on subdomain, so we need to use a relative path
return url;
}
}
2023-05-03 10:23:20 +02:00
function canOpenInBrowser(mime) {
return mime === "application/pdf"
|| mime.startsWith("image")
|| mime.startsWith("audio")
|| mime.startsWith("video");
}
async function openExternally(type, entityId, mime) {
checkType(type);
if (utils.isElectron()) {
const resp = await server.post(`${type}/${entityId}/save-to-tmp-dir`);
const electron = utils.dynamicRequire('electron');
const res = await electron.shell.openPath(resp.tmpFilePath);
if (res) {
// fallback in case there's no default application for this file
window.open(getFileUrl(type, entityId));
2023-05-03 10:23:20 +02:00
}
}
else {
// allow browser to handle opening common file
if (canOpenInBrowser(mime)) {
window.open(getOpenFileUrl(type, entityId));
} else {
window.location.href = getFileUrl(type, entityId);
}
}
}
const openNoteExternally = async (noteId, mime) => await openExternally('notes', noteId, mime);
const openAttachmentExternally = async (attachmentId, mime) => await openExternally('attachments', attachmentId, mime);
function getHost() {
const url = new URL(window.location.href);
return `${url.protocol}//${url.hostname}:${url.port}`;
}
export default {
2020-11-12 22:13:59 +01:00
download,
downloadFileNote,
downloadRevision,
2023-05-03 10:23:20 +02:00
downloadAttachment,
getUrlForDownload,
openNoteExternally,
openAttachmentExternally,
2023-05-16 11:57:28 +00:00
openNoteCustom,
}