mirror of
https://github.com/zadam/trilium.git
synced 2025-11-17 02:30:42 +01:00
refactoring + uploading attachments WIP
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
const becca = require("../../becca/becca");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
const utils = require("../../services/utils");
|
||||
const blobService = require("../../services/blob.js");
|
||||
|
||||
@@ -11,13 +10,7 @@ function getAttachmentBlob(req) {
|
||||
|
||||
function getAttachments(req) {
|
||||
const includeContent = req.query.includeContent === 'true';
|
||||
const {noteId} = req.params;
|
||||
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' doesn't exist.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(req.params.noteId);
|
||||
|
||||
return note.getAttachments()
|
||||
.map(attachment => processAttachment(attachment, includeContent));
|
||||
@@ -27,11 +20,7 @@ function getAttachment(req) {
|
||||
const includeContent = req.query.includeContent === 'true';
|
||||
const {attachmentId} = req.params;
|
||||
|
||||
const attachment = becca.getAttachment(attachmentId);
|
||||
|
||||
if (!attachment) {
|
||||
throw new NotFoundError(`Attachment '${attachmentId}' doesn't exist.`);
|
||||
}
|
||||
const attachment = becca.getAttachmentOrThrow(attachmentId);
|
||||
|
||||
return processAttachment(attachment, includeContent);
|
||||
}
|
||||
@@ -62,12 +51,7 @@ function saveAttachment(req) {
|
||||
const {noteId} = req.params;
|
||||
const {attachmentId, role, mime, title, content} = req.body;
|
||||
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' doesn't exist.`);
|
||||
}
|
||||
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
note.saveAttachment({attachmentId, role, mime, title, content});
|
||||
}
|
||||
|
||||
@@ -84,12 +68,7 @@ function deleteAttachment(req) {
|
||||
function convertAttachmentToNote(req) {
|
||||
const {attachmentId} = req.params;
|
||||
|
||||
const attachment = becca.getAttachment(attachmentId);
|
||||
|
||||
if (!attachment) {
|
||||
throw new NotFoundError(`Attachment '${attachmentId}' doesn't exist.`);
|
||||
}
|
||||
|
||||
const attachment = becca.getAttachmentOrThrow(attachmentId);
|
||||
return attachment.convertToNote();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ const attributeService = require('../../services/attributes');
|
||||
const BAttribute = require('../../becca/entities/battribute');
|
||||
const becca = require("../../becca/becca");
|
||||
const ValidationError = require("../../errors/validation_error");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
|
||||
function getEffectiveNoteAttributes(req) {
|
||||
const note = becca.getNote(req.params.noteId);
|
||||
@@ -20,11 +19,7 @@ function updateNoteAttribute(req) {
|
||||
|
||||
let attribute;
|
||||
if (body.attributeId) {
|
||||
attribute = becca.getAttribute(body.attributeId);
|
||||
|
||||
if (!attribute) {
|
||||
throw new NotFoundError(`Attribute '${body.attributeId}' does not exist.`);
|
||||
}
|
||||
attribute = becca.getAttributeOrThrow(body.attributeId);
|
||||
|
||||
if (attribute.noteId !== noteId) {
|
||||
throw new ValidationError(`Attribute '${body.attributeId}' is not owned by ${noteId}`);
|
||||
|
||||
@@ -10,7 +10,6 @@ const TaskContext = require('../../services/task_context');
|
||||
const branchService = require("../../services/branches");
|
||||
const log = require("../../services/log");
|
||||
const ValidationError = require("../../errors/validation_error");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
|
||||
/**
|
||||
* Code in this file deals with moving and cloning branches. The relationship between note and parent note is unique
|
||||
@@ -33,16 +32,8 @@ function moveBranchToParent(req) {
|
||||
function moveBranchBeforeNote(req) {
|
||||
const {branchId, beforeBranchId} = req.params;
|
||||
|
||||
const branchToMove = becca.getBranch(branchId);
|
||||
const beforeBranch = becca.getBranch(beforeBranchId);
|
||||
|
||||
if (!branchToMove) {
|
||||
throw new NotFoundError(`Can't find branch '${branchId}'`);
|
||||
}
|
||||
|
||||
if (!beforeBranch) {
|
||||
throw new NotFoundError(`Can't find branch '${beforeBranchId}'`);
|
||||
}
|
||||
const branchToMove = becca.getBranchOrThrow(branchId);
|
||||
const beforeBranch = becca.getBranchOrThrow(beforeBranchId);
|
||||
|
||||
const validationResult = treeService.validateParentChild(beforeBranch.parentNoteId, branchToMove.noteId, branchId);
|
||||
|
||||
@@ -192,11 +183,7 @@ function setExpandedForSubtree(req) {
|
||||
function deleteBranch(req) {
|
||||
const last = req.query.last === 'true';
|
||||
const eraseNotes = req.query.eraseNotes === 'true';
|
||||
const branch = becca.getBranch(req.params.branchId);
|
||||
|
||||
if (!branch) {
|
||||
throw new NotFoundError(`Branch '${req.params.branchId}' not found`);
|
||||
}
|
||||
const branch = becca.getBranchOrThrow(req.params.branchId);
|
||||
|
||||
const taskContext = TaskContext.getInstance(req.query.taskId, 'delete-notes');
|
||||
|
||||
|
||||
@@ -10,14 +10,10 @@ const { Readable } = require('stream');
|
||||
const chokidar = require('chokidar');
|
||||
const ws = require('../../services/ws');
|
||||
const becca = require("../../becca/becca");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
const ValidationError = require("../../errors/validation_error.js");
|
||||
|
||||
function updateFile(req) {
|
||||
const note = becca.getNote(req.params.noteId);
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${req.params.noteId}' doesn't exist.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(req.params.noteId);
|
||||
|
||||
const file = req.file;
|
||||
note.saveNoteRevision();
|
||||
@@ -37,11 +33,7 @@ function updateFile(req) {
|
||||
}
|
||||
|
||||
function updateAttachment(req) {
|
||||
const attachment = becca.getAttachment(req.params.attachmentId);
|
||||
if (!attachment) {
|
||||
throw new NotFoundError(`Attachment '${req.params.attachmentId}' doesn't exist.`);
|
||||
}
|
||||
|
||||
const attachment = becca.getAttachmentOrThrow(req.params.attachmentId);
|
||||
const file = req.file;
|
||||
attachment.getNote().saveNoteRevision();
|
||||
|
||||
@@ -107,20 +99,14 @@ const openAttachment = (req, res) => downloadAttachmentInt(req.params.attachment
|
||||
|
||||
function fileContentProvider(req) {
|
||||
// Read the file name from route params.
|
||||
const note = becca.getNote(req.params.noteId);
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${req.params.noteId}' doesn't exist.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(req.params.noteId);
|
||||
|
||||
return streamContent(note.getContent(), note.getFileName(), note.mime);
|
||||
}
|
||||
|
||||
function attachmentContentProvider(req) {
|
||||
// Read the file name from route params.
|
||||
const attachment = becca.getAttachment(req.params.attachmentId);
|
||||
if (!attachment) {
|
||||
throw new NotFoundError(`Attachment '${req.params.attachmentId}' doesn't exist.`);
|
||||
}
|
||||
const attachment = becca.getAttachmentOrThrow(req.params.attachmentId);
|
||||
|
||||
return streamContent(attachment.getContent(), attachment.getFileName(), attachment.mime);
|
||||
}
|
||||
@@ -152,11 +138,7 @@ function streamContent(content, fileName, mimeType) {
|
||||
}
|
||||
|
||||
function saveNoteToTmpDir(req) {
|
||||
const note = becca.getNote(req.params.noteId);
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${req.params.noteId}' doesn't exist.`);
|
||||
}
|
||||
|
||||
const note = becca.getNoteOrThrow(req.params.noteId);
|
||||
const fileName = note.getFileName();
|
||||
const content = note.getContent();
|
||||
|
||||
@@ -164,11 +146,7 @@ function saveNoteToTmpDir(req) {
|
||||
}
|
||||
|
||||
function saveAttachmentToTmpDir(req) {
|
||||
const attachment = becca.getAttachment(req.params.attachmentId);
|
||||
if (!attachment) {
|
||||
throw new NotFoundError(`Attachment '${req.params.attachmentId}' doesn't exist.`);
|
||||
}
|
||||
|
||||
const attachment = becca.getAttachmentOrThrow(req.params.attachmentId);
|
||||
const fileName = attachment.getFileName();
|
||||
const content = attachment.getContent();
|
||||
|
||||
@@ -204,11 +182,7 @@ function uploadModifiedFileToNote(req) {
|
||||
const noteId = req.params.noteId;
|
||||
const {filePath} = req.body;
|
||||
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' has not been found`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
log.info(`Updating note '${noteId}' with content from '${filePath}'`);
|
||||
|
||||
@@ -227,11 +201,7 @@ function uploadModifiedFileToAttachment(req) {
|
||||
const {attachmentId} = req.params;
|
||||
const {filePath} = req.body;
|
||||
|
||||
const attachment = becca.getAttachment(attachmentId);
|
||||
|
||||
if (!attachment) {
|
||||
throw new NotFoundError(`Attachment '${attachmentId}' has not been found`);
|
||||
}
|
||||
const attachment = becca.getAttachmentOrThrow(attachmentId);
|
||||
|
||||
log.info(`Updating attachment '${attachmentId}' with content from '${filePath}'`);
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ const becca = require('../../becca/becca');
|
||||
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
|
||||
const fs = require('fs');
|
||||
const ValidationError = require("../../errors/validation_error");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
|
||||
function returnImage(req, res) {
|
||||
const image = becca.getNote(req.params.noteId);
|
||||
@@ -64,11 +63,7 @@ function uploadImage(req) {
|
||||
const {noteId} = req.query;
|
||||
const {file} = req;
|
||||
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' doesn't exist.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
if (!["image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
|
||||
throw new ValidationError(`Unknown image type '${file.mimetype}'`);
|
||||
@@ -86,11 +81,7 @@ function updateImage(req) {
|
||||
const {noteId} = req.params;
|
||||
const {file} = req;
|
||||
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' doesn't exist.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
if (!["image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
|
||||
return {
|
||||
|
||||
@@ -11,9 +11,8 @@ const beccaLoader = require('../../becca/becca_loader');
|
||||
const log = require('../../services/log');
|
||||
const TaskContext = require('../../services/task_context');
|
||||
const ValidationError = require("../../errors/validation_error");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
|
||||
async function importToBranch(req) {
|
||||
async function importNotesToBranch(req) {
|
||||
const {parentNoteId} = req.params;
|
||||
const {taskId, last} = req.body;
|
||||
|
||||
@@ -32,11 +31,7 @@ async function importToBranch(req) {
|
||||
throw new ValidationError("No file has been uploaded");
|
||||
}
|
||||
|
||||
const parentNote = becca.getNote(parentNoteId);
|
||||
|
||||
if (!parentNote) {
|
||||
throw new NotFoundError(`Note '${parentNoteId}' doesn't exist.`);
|
||||
}
|
||||
const parentNote = becca.getNoteOrThrow(parentNoteId);
|
||||
|
||||
const extension = path.extname(file.originalname).toLowerCase();
|
||||
|
||||
@@ -79,14 +74,68 @@ async function importToBranch(req) {
|
||||
}), 1000);
|
||||
}
|
||||
|
||||
// import has deactivated note events so becca is not updated
|
||||
// instead we force it to reload (can be async)
|
||||
// import has deactivated note events so becca is not updated, instead we force it to reload
|
||||
beccaLoader.load();
|
||||
|
||||
return note.getPojo();
|
||||
}
|
||||
|
||||
async function importAttachmentsToNote(req) {
|
||||
const {parentNoteId} = req.params;
|
||||
const {taskId, last} = req.body;
|
||||
|
||||
const options = {
|
||||
shrinkImages: req.body.shrinkImages !== 'false',
|
||||
};
|
||||
|
||||
const file = req.file;
|
||||
|
||||
if (!file) {
|
||||
throw new ValidationError("No file has been uploaded");
|
||||
}
|
||||
|
||||
const parentNote = becca.getNoteOrThrow(parentNoteId);
|
||||
|
||||
// running all the event handlers on imported notes (and attributes) is slow
|
||||
// and may produce unintended consequences
|
||||
cls.disableEntityEvents();
|
||||
|
||||
// eliminate flickering during import
|
||||
cls.ignoreEntityChangeIds();
|
||||
|
||||
let note; // typically root of the import - client can show it after finishing the import
|
||||
|
||||
const taskContext = TaskContext.getInstance(taskId, 'import', options);
|
||||
|
||||
try {
|
||||
// FIXME
|
||||
|
||||
note = await singleImportService.importSingleFile(taskContext, file, parentNote);
|
||||
}
|
||||
catch (e) {
|
||||
const message = `Import failed with following error: '${e.message}'. More details might be in the logs.`;
|
||||
taskContext.reportError(message);
|
||||
|
||||
log.error(message + e.stack);
|
||||
|
||||
return [500, message];
|
||||
}
|
||||
|
||||
if (last === "true") {
|
||||
// small timeout to avoid race condition (the message is received before the transaction is committed)
|
||||
setTimeout(() => taskContext.taskSucceeded({
|
||||
parentNoteId: parentNoteId,
|
||||
importedNoteId: note.noteId
|
||||
}), 1000);
|
||||
}
|
||||
|
||||
// import has deactivated note events so becca is not updated, instead we force it to reload
|
||||
beccaLoader.load();
|
||||
|
||||
return note.getPojo();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
importToBranch
|
||||
importNotesToBranch,
|
||||
importAttachmentsToNote
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
const becca = require("../../becca/becca");
|
||||
const { JSDOM } = require("jsdom");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
|
||||
function buildDescendantCountMap(noteIdsToCount) {
|
||||
if (!Array.isArray(noteIdsToCount)) {
|
||||
throw new Error('noteIdsToCount: type error');
|
||||
@@ -345,25 +345,16 @@ function getFilteredBacklinks(note) {
|
||||
function getBacklinkCount(req) {
|
||||
const {noteId} = req.params;
|
||||
|
||||
const note = becca.getNote(noteId);
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' not found`);
|
||||
}
|
||||
else {
|
||||
return {
|
||||
count: getFilteredBacklinks(note).length
|
||||
};
|
||||
}
|
||||
return {
|
||||
count: getFilteredBacklinks(note).length
|
||||
};
|
||||
}
|
||||
|
||||
function getBacklinks(req) {
|
||||
const {noteId} = req.params;
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' was not found`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
let backlinksWithExcerptCount = 0;
|
||||
|
||||
|
||||
@@ -8,16 +8,10 @@ const log = require('../../services/log');
|
||||
const TaskContext = require('../../services/task_context');
|
||||
const becca = require("../../becca/becca");
|
||||
const ValidationError = require("../../errors/validation_error");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
const blobService = require("../../services/blob");
|
||||
|
||||
function getNote(req) {
|
||||
const note = becca.getNote(req.params.noteId);
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${req.params.noteId}' has not been found.`);
|
||||
}
|
||||
|
||||
return note;
|
||||
return becca.getNoteOrThrow(req.params.noteId);
|
||||
}
|
||||
|
||||
function getNoteBlob(req) {
|
||||
@@ -27,11 +21,7 @@ function getNoteBlob(req) {
|
||||
}
|
||||
|
||||
function getNoteMetadata(req) {
|
||||
const note = becca.getNote(req.params.noteId);
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${req.params.noteId}' has not been found.`);
|
||||
}
|
||||
|
||||
const note = becca.getNoteOrThrow(req.params.noteId);
|
||||
const contentMetadata = note.getContentMetadata();
|
||||
|
||||
return {
|
||||
@@ -132,11 +122,7 @@ function changeTitle(req) {
|
||||
const noteId = req.params.noteId;
|
||||
const title = req.body.title;
|
||||
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' has not been found`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
if (!note.isContentAvailable()) {
|
||||
throw new ValidationError(`Note '${noteId}' is not available for change`);
|
||||
@@ -232,11 +218,7 @@ function getDeleteNotesPreview(req) {
|
||||
|
||||
function forceSaveNoteRevision(req) {
|
||||
const {noteId} = req.params;
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' not found.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
if (!note.isContentAvailable()) {
|
||||
throw new ValidationError(`Note revision of a protected note cannot be created outside of a protected session.`);
|
||||
@@ -247,11 +229,7 @@ function forceSaveNoteRevision(req) {
|
||||
|
||||
function convertNoteToAttachment(req) {
|
||||
const {noteId} = req.params;
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' not found.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
return {
|
||||
attachment: note.convertToParentAttachment({ force: true })
|
||||
|
||||
@@ -7,14 +7,9 @@ const bulkActionService = require("../../services/bulk_actions");
|
||||
const cls = require("../../services/cls");
|
||||
const {formatAttrForSearch} = require("../../services/attribute_formatter");
|
||||
const ValidationError = require("../../errors/validation_error");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
|
||||
function searchFromNote(req) {
|
||||
const note = becca.getNote(req.params.noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${req.params.noteId}' has not been found.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(req.params.noteId);
|
||||
|
||||
if (note.isDeleted) {
|
||||
// this can be triggered from recent changes, and it's harmless to return empty list rather than fail
|
||||
@@ -29,11 +24,7 @@ function searchFromNote(req) {
|
||||
}
|
||||
|
||||
function searchAndExecute(req) {
|
||||
const note = becca.getNote(req.params.noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${req.params.noteId}' has not been found.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(req.params.noteId);
|
||||
|
||||
if (note.isDeleted) {
|
||||
// this can be triggered from recent changes, and it's harmless to return empty list rather than fail
|
||||
|
||||
@@ -2,16 +2,11 @@
|
||||
|
||||
const similarityService = require('../../becca/similarity');
|
||||
const becca = require("../../becca/becca");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
|
||||
async function getSimilarNotes(req) {
|
||||
const noteId = req.params.noteId;
|
||||
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' not found.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(noteId);
|
||||
|
||||
return await similarityService.findSimilarNotes(noteId);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
const sql = require('../../services/sql');
|
||||
const becca = require("../../becca/becca");
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
|
||||
function getSchema() {
|
||||
const tableNames = sql.getColumn(`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`);
|
||||
@@ -19,11 +18,7 @@ function getSchema() {
|
||||
}
|
||||
|
||||
function execute(req) {
|
||||
const note = becca.getNote(req.params.noteId);
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${req.params.noteId}' was not found.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(req.params.noteId);
|
||||
|
||||
const queries = note.getContent().split("\n---");
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
const sql = require('../../services/sql');
|
||||
const becca = require('../../becca/becca');
|
||||
const NotFoundError = require("../../errors/not_found_error");
|
||||
|
||||
function getNoteSize(req) {
|
||||
const {noteId} = req.params;
|
||||
@@ -23,12 +22,7 @@ function getNoteSize(req) {
|
||||
}
|
||||
|
||||
function getSubtreeSize(req) {
|
||||
const {noteId} = req.params;
|
||||
const note = becca.notes[noteId];
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError(`Note '${noteId}' was not found.`);
|
||||
}
|
||||
const note = becca.getNoteOrThrow(req.params.noteId);
|
||||
|
||||
const subTreeNoteIds = note.getSubtreeNoteIds();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user