mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	refactoring + uploading attachments WIP
This commit is contained in:
		@@ -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
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user