2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2021-06-29 22:15:57 +02:00
|
|
|
const beccaService = require('../../becca/becca_service');
|
2023-06-04 23:01:40 +02:00
|
|
|
const revisionService = require('../../services/revisions.js');
|
2019-11-09 08:53:13 +01:00
|
|
|
const utils = require('../../services/utils');
|
2021-03-22 23:07:43 +01:00
|
|
|
const sql = require('../../services/sql');
|
2022-11-03 20:28:56 +02:00
|
|
|
const cls = require('../../services/cls');
|
2019-11-09 08:53:13 +01:00
|
|
|
const path = require('path');
|
2021-06-29 22:15:57 +02:00
|
|
|
const becca = require("../../becca/becca");
|
2023-05-05 16:37:39 +02:00
|
|
|
const blobService = require("../../services/blob.js");
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
function getRevisionBlob(req) {
|
2023-05-05 22:21:51 +02:00
|
|
|
const preview = req.query.preview === 'true';
|
2023-05-05 16:37:39 +02:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
return blobService.getBlobPojo('revisions', req.params.revisionId, { preview });
|
2023-05-05 16:37:39 +02:00
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
function getRevisions(req) {
|
|
|
|
return becca.getRevisionsFromQuery(`
|
|
|
|
SELECT revisions.*,
|
2023-03-16 11:02:07 +01:00
|
|
|
LENGTH(blobs.content) AS contentLength
|
2023-06-04 23:01:40 +02:00
|
|
|
FROM revisions
|
|
|
|
JOIN blobs ON revisions.blobId = blobs.blobId
|
2020-12-16 14:36:24 +01:00
|
|
|
WHERE noteId = ?
|
2019-11-09 15:21:14 +01:00
|
|
|
ORDER BY utcDateCreated DESC`, [req.params.noteId]);
|
2019-09-08 09:17:16 +02:00
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
function getRevision(req) {
|
|
|
|
const revision = becca.getRevision(req.params.revisionId);
|
2019-09-08 09:17:16 +02:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
if (revision.type === 'file') {
|
|
|
|
if (revision.hasStringContent()) {
|
|
|
|
revision.content = revision.getContent().substr(0, 10000);
|
2019-11-09 13:01:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-06-04 23:01:40 +02:00
|
|
|
revision.content = revision.getContent();
|
2019-11-01 20:00:56 +01:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
if (revision.content && revision.type === 'image') {
|
|
|
|
revision.content = revision.content.toString('base64');
|
2019-11-09 08:53:13 +01:00
|
|
|
}
|
2019-11-08 23:09:57 +01:00
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
return revision;
|
2018-03-30 13:56:46 -04:00
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2019-11-09 08:53:13 +01:00
|
|
|
/**
|
2023-06-04 23:01:40 +02:00
|
|
|
* @param {BRevision} revision
|
2023-01-05 23:38:41 +01:00
|
|
|
* @returns {string}
|
2019-11-09 08:53:13 +01:00
|
|
|
*/
|
2023-06-04 23:01:40 +02:00
|
|
|
function getRevisionFilename(revision) {
|
|
|
|
let filename = utils.formatDownloadTitle(revision.title, revision.type, revision.mime);
|
2019-11-09 08:53:13 +01:00
|
|
|
|
|
|
|
const extension = path.extname(filename);
|
2023-06-04 23:01:40 +02:00
|
|
|
const date = revision.dateCreated
|
2019-11-09 08:53:13 +01:00
|
|
|
.substr(0, 19)
|
|
|
|
.replace(' ', '_')
|
|
|
|
.replace(/[^0-9_]/g, '');
|
|
|
|
|
|
|
|
if (extension) {
|
2022-12-21 15:19:05 +01:00
|
|
|
filename = `${filename.substr(0, filename.length - extension.length)}-${date}${extension}`;
|
2019-11-09 08:53:13 +01:00
|
|
|
}
|
|
|
|
else {
|
2022-12-21 15:19:05 +01:00
|
|
|
filename += `-${date}`;
|
2019-11-09 08:53:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
function downloadRevision(req, res) {
|
|
|
|
const revision = becca.getRevision(req.params.revisionId);
|
2019-11-09 08:53:13 +01:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
if (!revision.isContentAvailable()) {
|
2022-07-01 00:01:29 +02:00
|
|
|
return res.setHeader("Content-Type", "text/plain")
|
|
|
|
.status(401)
|
|
|
|
.send("Protected session not available");
|
2019-11-09 08:53:13 +01:00
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
const filename = getRevisionFilename(revision);
|
2019-11-09 08:53:13 +01:00
|
|
|
|
|
|
|
res.setHeader('Content-Disposition', utils.getContentDisposition(filename));
|
2023-06-04 23:01:40 +02:00
|
|
|
res.setHeader('Content-Type', revision.mime);
|
2019-11-09 08:53:13 +01:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
res.send(revision.getContent());
|
2019-11-09 08:53:13 +01:00
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
function eraseAllRevisions(req) {
|
|
|
|
const revisionIdsToErase = sql.getColumn('SELECT revisionId FROM revisions WHERE noteId = ?',
|
2019-11-09 15:21:14 +01:00
|
|
|
[req.params.noteId]);
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
revisionService.eraseRevisions(revisionIdsToErase);
|
2019-11-09 15:21:14 +01:00
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
function eraseRevision(req) {
|
|
|
|
revisionService.eraseRevisions([req.params.revisionId]);
|
2019-11-09 15:21:14 +01:00
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
function restoreRevision(req) {
|
|
|
|
const revision = becca.getRevision(req.params.revisionId);
|
2020-05-07 23:34:13 +02:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
if (revision) {
|
|
|
|
const note = revision.getNote();
|
2020-05-07 23:34:13 +02:00
|
|
|
|
2023-04-19 22:47:33 +02:00
|
|
|
sql.transactional(() => {
|
2023-06-04 23:01:40 +02:00
|
|
|
note.saveRevision();
|
2020-05-07 23:34:13 +02:00
|
|
|
|
2023-04-19 22:47:33 +02:00
|
|
|
for (const oldNoteAttachment of note.getAttachments()) {
|
|
|
|
oldNoteAttachment.markAsDeleted();
|
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
let revisionContent = revision.getContent();
|
2023-04-19 22:47:33 +02:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
for (const revisionAttachment of revision.getAttachments()) {
|
2023-04-19 22:47:33 +02:00
|
|
|
const noteAttachment = revisionAttachment.copy();
|
|
|
|
noteAttachment.parentId = note.noteId;
|
|
|
|
noteAttachment.setContent(revisionAttachment.getContent(), { forceSave: true });
|
|
|
|
|
|
|
|
// content is rewritten to point to the restored revision attachments
|
|
|
|
revisionContent = revisionContent.replaceAll(`attachments/${revisionAttachment.attachmentId}`, `attachments/${noteAttachment.attachmentId}`);
|
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
note.title = revision.title;
|
2023-04-19 22:47:33 +02:00
|
|
|
note.setContent(revisionContent, { forceSave: true });
|
|
|
|
});
|
2020-05-07 23:34:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getEditedNotesOnDate(req) {
|
2021-05-02 19:59:16 +02:00
|
|
|
const noteIds = sql.getColumn(`
|
2019-12-01 14:30:59 +01:00
|
|
|
SELECT notes.*
|
|
|
|
FROM notes
|
|
|
|
WHERE noteId IN (
|
|
|
|
SELECT noteId FROM notes
|
2020-06-20 23:24:34 +02:00
|
|
|
WHERE notes.dateCreated LIKE :date
|
|
|
|
OR notes.dateModified LIKE :date
|
2019-12-01 14:30:59 +01:00
|
|
|
UNION ALL
|
2023-06-04 23:01:40 +02:00
|
|
|
SELECT noteId FROM revisions
|
|
|
|
WHERE revisions.dateLastEdited LIKE :date
|
2019-12-01 14:30:59 +01:00
|
|
|
)
|
2019-12-02 22:27:06 +01:00
|
|
|
ORDER BY isDeleted
|
2022-12-21 15:19:05 +01:00
|
|
|
LIMIT 50`, {date: `${req.params.date}%`});
|
2019-09-07 10:11:59 +02:00
|
|
|
|
2022-11-03 20:28:56 +02:00
|
|
|
let notes = becca.getNotes(noteIds, true);
|
|
|
|
|
|
|
|
// Narrow down the results if a note is hoisted, similar to "Jump to note".
|
|
|
|
const hoistedNoteId = cls.getHoistedNoteId();
|
2022-11-03 21:40:42 +01:00
|
|
|
if (hoistedNoteId !== 'root') {
|
2022-11-03 20:28:56 +02:00
|
|
|
notes = notes.filter(note => note.hasAncestor(hoistedNoteId));
|
|
|
|
}
|
|
|
|
|
2023-05-26 14:54:13 +08:00
|
|
|
return notes.map(note => {
|
2023-06-05 09:23:42 +02:00
|
|
|
const notePath = getNotePathData(note);
|
2019-09-07 10:11:59 +02:00
|
|
|
|
2023-05-26 14:54:13 +08:00
|
|
|
const notePojo = note.getPojo();
|
|
|
|
notePojo.notePath = notePath ? notePath.notePath : null;
|
2023-05-26 14:36:21 +08:00
|
|
|
|
2023-05-26 14:54:13 +08:00
|
|
|
return notePojo;
|
|
|
|
});
|
2019-09-07 10:11:59 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-16 11:28:24 +02:00
|
|
|
function getNotePathData(note) {
|
|
|
|
const retPath = note.getBestNotePath();
|
|
|
|
|
|
|
|
if (retPath) {
|
|
|
|
const noteTitle = beccaService.getNoteTitleForPath(retPath);
|
|
|
|
|
|
|
|
let branchId;
|
|
|
|
|
|
|
|
if (note.isRoot()) {
|
|
|
|
branchId = 'none_root';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const parentNote = note.parents[0];
|
|
|
|
branchId = becca.getBranchFromChildAndParent(note.noteId, parentNote.noteId).branchId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
noteId: note.noteId,
|
|
|
|
branchId: branchId,
|
|
|
|
title: noteTitle,
|
|
|
|
notePath: retPath,
|
|
|
|
path: retPath.join('/')
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:56:46 -04:00
|
|
|
module.exports = {
|
2023-06-04 23:01:40 +02:00
|
|
|
getRevisionBlob,
|
|
|
|
getRevisions,
|
|
|
|
getRevision,
|
|
|
|
downloadRevision,
|
2019-11-09 15:21:14 +01:00
|
|
|
getEditedNotesOnDate,
|
2023-06-04 23:01:40 +02:00
|
|
|
eraseAllRevisions,
|
|
|
|
eraseRevision,
|
|
|
|
restoreRevision
|
2020-05-12 10:28:31 +02:00
|
|
|
};
|