Notes/src/routes/api/files.js

152 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-02-14 23:31:20 -05:00
"use strict";
const protectedSessionService = require('../../services/protected_session');
const utils = require('../../services/utils');
const log = require('../../services/log');
2019-11-09 11:58:52 +01:00
const noteRevisionService = require('../../services/note_revisions');
const tmp = require('tmp');
const fs = require('fs');
const { Readable } = require('stream');
2021-04-24 11:39:59 +02:00
const chokidar = require('chokidar');
const ws = require('../../services/ws');
2021-05-17 22:09:49 +02:00
const becca = require("../../becca/becca.js");
2018-02-14 23:31:20 -05:00
2020-06-20 12:31:38 +02:00
function updateFile(req) {
2019-11-09 11:58:52 +01:00
const {noteId} = req.params;
2018-02-14 23:31:20 -05:00
const file = req.file;
2021-05-02 11:23:58 +02:00
const note = becca.getNote(noteId);
2018-02-14 23:31:20 -05:00
2019-11-09 11:58:52 +01:00
if (!note) {
return [404, `Note ${noteId} doesn't exist.`];
2018-02-14 23:31:20 -05:00
}
2020-06-20 12:31:38 +02:00
noteRevisionService.createNoteRevision(note);
2019-11-09 11:58:52 +01:00
note.mime = file.mimetype.toLowerCase();
note.save();
2019-11-09 11:58:52 +01:00
2020-06-20 12:31:38 +02:00
note.setContent(file.buffer);
2019-11-09 11:58:52 +01:00
2020-06-20 12:31:38 +02:00
note.setLabel('originalFileName', file.originalname);
2019-11-09 11:58:52 +01:00
2020-06-20 12:31:38 +02:00
noteRevisionService.protectNoteRevisions(note);
return {
2019-11-09 11:58:52 +01:00
uploaded: true
};
}
function getFilename(note) {
// (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
return utils.formatDownloadTitle(note.title, note.type, note.mime);
}
2020-06-20 12:31:38 +02:00
function downloadNoteFile(noteId, res, contentDisposition = true) {
2021-05-02 11:23:58 +02:00
const note = becca.getNote(noteId);
if (!note) {
return res.status(404).send(`Note ${noteId} doesn't exist.`);
}
if (note.isProtected && !protectedSessionService.isProtectedSessionAvailable()) {
2019-01-27 15:47:40 +01:00
return res.status(401).send("Protected session not available");
}
if (contentDisposition) {
const filename = getFilename(note);
res.setHeader('Content-Disposition', utils.getContentDisposition(filename));
}
res.setHeader('Content-Type', note.mime);
2020-06-20 12:31:38 +02:00
res.send(note.getContent());
}
2020-06-20 12:31:38 +02:00
function downloadFile(req, res) {
2019-01-27 15:47:40 +01:00
const noteId = req.params.noteId;
2020-06-20 12:31:38 +02:00
return downloadNoteFile(noteId, res);
}
2020-06-20 12:31:38 +02:00
function openFile(req, res) {
const noteId = req.params.noteId;
2019-01-27 15:47:40 +01:00
2020-06-20 12:31:38 +02:00
return downloadNoteFile(noteId, res, false);
2019-01-27 15:47:40 +01:00
}
function fileContentProvider(req) {
// Read file name from route params.
2021-05-02 11:23:58 +02:00
const note = becca.getNote(req.params.noteId);
const fileName = getFilename(note);
let content = note.getContent();
if (typeof content === "string") {
content = Buffer.from(content, 'utf8');
}
const totalSize = content.byteLength;
const mimeType = note.mime;
const getStream = range => {
if (!range) {
// Request if for complete content.
return Readable.from(content);
}
// Partial content request.
const { start, end } = range;
return Readable.from(content.slice(start, end + 1));
}
return {
fileName,
totalSize,
mimeType,
getStream
};
}
function saveToTmpDir(req) {
const noteId = req.params.noteId;
2021-05-02 11:23:58 +02:00
const note = becca.getNote(noteId);
if (!note) {
return [404,`Note ${noteId} doesn't exist.`];
}
const tmpObj = tmp.fileSync({postfix: getFilename(note)});
fs.writeSync(tmpObj.fd, note.getContent());
fs.closeSync(tmpObj.fd);
log.info(`Saved temporary file for note ${noteId} into ${tmpObj.name}`);
2021-04-24 11:39:59 +02:00
if (utils.isElectron()) {
chokidar.watch(tmpObj.name).on('change', (path, stats) => {
ws.sendMessageToAllClients({
type: 'openedFileUpdated',
noteId: noteId,
lastModifiedMs: stats.atimeMs,
filePath: tmpObj.name
});
});
}
return {
tmpFilePath: tmpObj.name
};
}
module.exports = {
2019-11-09 11:58:52 +01:00
updateFile,
openFile,
fileContentProvider,
2019-01-27 15:47:40 +01:00
downloadFile,
downloadNoteFile,
saveToTmpDir
};