Notes/src/routes/api/import.js

126 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-12-02 23:41:18 -05:00
"use strict";
const enexImportService = require('../../services/import/enex.js');
const opmlImportService = require('../../services/import/opml.js');
const zipImportService = require('../../services/import/zip.js');
const singleImportService = require('../../services/import/single.js');
const cls = require('../../services/cls.js');
const path = require('path');
const becca = require('../../becca/becca.js');
const beccaLoader = require('../../becca/becca_loader.js');
const log = require('../../services/log.js');
const TaskContext = require('../../services/task_context.js');
const ValidationError = require('../../errors/validation_error.js');
2019-02-10 19:36:03 +01:00
async function importNotesToBranch(req) {
const {parentNoteId} = req.params;
const {taskId, last} = req.body;
2019-02-11 23:45:58 +01:00
2019-02-24 12:24:28 +01:00
const options = {
safeImport: req.body.safeImport !== 'false',
2019-02-25 21:22:57 +01:00
shrinkImages: req.body.shrinkImages !== 'false',
2019-02-24 12:24:28 +01:00
textImportedAsText: req.body.textImportedAsText !== 'false',
codeImportedAsCode: req.body.codeImportedAsCode !== 'false',
explodeArchives: req.body.explodeArchives !== 'false',
replaceUnderscoresWithSpaces: req.body.replaceUnderscoresWithSpaces !== 'false'
2019-02-24 12:24:28 +01:00
};
2019-02-11 23:45:58 +01:00
2018-05-29 20:32:13 -04:00
const file = req.file;
2018-09-10 23:41:11 +02:00
if (!file) {
throw new ValidationError("No file has been uploaded");
2018-09-03 21:06:24 +02:00
}
const parentNote = becca.getNoteOrThrow(parentNoteId);
2018-05-29 20:32:13 -04:00
const extension = path.extname(file.originalname).toLowerCase();
// 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
2023-05-09 00:05:27 +02:00
const taskContext = TaskContext.getInstance(taskId, 'importNotes', options);
2019-02-10 19:36:03 +01:00
2019-02-10 19:53:57 +01:00
try {
2020-07-09 21:46:33 +02:00
if (extension === '.zip' && options.explodeArchives) {
2020-06-27 00:40:35 +02:00
note = await zipImportService.importZip(taskContext, file.buffer, parentNote);
} else if (extension === '.opml' && options.explodeArchives) {
2020-06-27 00:40:35 +02:00
note = await opmlImportService.importOpml(taskContext, file.buffer, parentNote);
} else if (extension === '.enex' && options.explodeArchives) {
2020-06-27 00:40:35 +02:00
note = await enexImportService.importEnex(taskContext, file, parentNote);
2019-02-10 19:53:57 +01:00
} else {
2020-06-27 00:40:35 +02:00
note = await singleImportService.importSingleFile(taskContext, file, parentNote);
2019-02-10 19:53:57 +01:00
}
2018-09-03 13:40:40 +02:00
}
2019-02-10 19:53:57 +01:00
catch (e) {
const message = `Import failed with following error: '${e.message}'. More details might be in the logs.`;
taskContext.reportError(message);
2019-02-10 19:53:57 +01:00
log.error(message + e.stack);
return [500, message];
2018-05-29 20:32:13 -04:00
}
if (last === "true") {
2023-05-05 23:41:11 +02:00
// small timeout to avoid race condition (the message is received before the transaction is committed)
2019-10-19 00:11:07 +02:00
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();
}
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);
2023-05-09 00:05:27 +02:00
const taskContext = TaskContext.getInstance(taskId, 'importAttachment', options);
2023-06-30 11:18:34 +02:00
// unlike in note import, we let the events run, because a huge number of attachments is not likely
try {
2023-05-09 00:05:27 +02:00
await singleImportService.importAttachment(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({
2023-05-09 00:05:27 +02:00
parentNoteId: parentNoteId
}), 1000);
}
2018-05-29 20:32:13 -04:00
}
2018-03-30 15:34:07 -04:00
module.exports = {
importNotesToBranch,
importAttachmentsToNote
2020-05-16 23:12:29 +02:00
};