2017-12-02 23:41:18 -05:00
|
|
|
"use strict";
|
|
|
|
|
2018-11-16 12:12:04 +01:00
|
|
|
const enexImportService = require('../../services/import/enex');
|
|
|
|
const opmlImportService = require('../../services/import/opml');
|
2020-03-20 21:57:16 +01:00
|
|
|
const zipImportService = require('../../services/import/zip');
|
2018-11-26 23:47:02 +01:00
|
|
|
const singleImportService = require('../../services/import/single');
|
2018-11-26 22:27:57 +01:00
|
|
|
const cls = require('../../services/cls');
|
2018-02-26 00:07:43 -05:00
|
|
|
const path = require('path');
|
2021-07-20 13:29:11 +02:00
|
|
|
const becca = require('../../becca/becca');
|
2021-06-29 22:15:57 +02:00
|
|
|
const beccaLoader = require('../../becca/becca_loader');
|
2019-02-10 19:53:57 +01:00
|
|
|
const log = require('../../services/log');
|
2021-06-29 22:15:57 +02:00
|
|
|
const TaskContext = require('../../services/task_context');
|
2019-02-10 19:36:03 +01:00
|
|
|
|
2020-06-27 00:40:35 +02:00
|
|
|
async function importToBranch(req) {
|
2019-02-24 09:56:00 +01:00
|
|
|
const {parentNoteId} = req.params;
|
2019-10-17 21:11:35 +02:00
|
|
|
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',
|
2019-02-25 22:38:48 +01:00
|
|
|
codeImportedAsCode: req.body.codeImportedAsCode !== 'false',
|
2020-05-30 16:15:00 -05:00
|
|
|
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) {
|
2018-09-03 21:06:24 +02:00
|
|
|
return [400, "No file has been uploaded"];
|
|
|
|
}
|
|
|
|
|
2021-05-02 11:23:58 +02:00
|
|
|
const parentNote = becca.getNote(parentNoteId);
|
2018-05-29 20:32:13 -04:00
|
|
|
|
|
|
|
if (!parentNote) {
|
|
|
|
return [404, `Note ${parentNoteId} doesn't exist.`];
|
|
|
|
}
|
|
|
|
|
|
|
|
const extension = path.extname(file.originalname).toLowerCase();
|
|
|
|
|
2018-11-26 22:27:57 +01:00
|
|
|
// running all the event handlers on imported notes (and attributes) is slow
|
|
|
|
// and may produce unintended consequences
|
|
|
|
cls.disableEntityEvents();
|
|
|
|
|
2021-02-16 22:51:00 +01:00
|
|
|
// eliminate flickering during import
|
|
|
|
cls.ignoreEntityChanges();
|
|
|
|
|
2019-01-08 20:19:41 +01:00
|
|
|
let note; // typically root of the import - client can show it after finishing the import
|
|
|
|
|
2019-10-18 23:19:16 +02:00
|
|
|
const taskContext = TaskContext.getInstance(taskId, 'import', 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);
|
2019-02-25 22:38:48 +01:00
|
|
|
} else if (extension === '.opml' && options.explodeArchives) {
|
2020-06-27 00:40:35 +02:00
|
|
|
note = await opmlImportService.importOpml(taskContext, file.buffer, parentNote);
|
2019-02-25 22:38:48 +01:00
|
|
|
} 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.";
|
2019-10-17 21:11:35 +02:00
|
|
|
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
|
|
|
}
|
2019-01-08 20:19:41 +01:00
|
|
|
|
2019-10-14 10:31:58 +02:00
|
|
|
if (last === "true") {
|
|
|
|
// small timeout to avoid race condition (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);
|
2019-10-14 10:31:58 +02:00
|
|
|
}
|
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
// import has deactivated note events so becca is not updated
|
2019-01-08 20:19:41 +01:00
|
|
|
// instead we force it to reload (can be async)
|
2020-06-27 00:40:35 +02:00
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
beccaLoader.load();
|
2019-01-08 20:19:41 +01:00
|
|
|
|
2021-07-20 13:29:11 +02:00
|
|
|
return note.getPojo();
|
2018-05-29 20:32:13 -04:00
|
|
|
}
|
|
|
|
|
2018-03-30 15:34:07 -04:00
|
|
|
module.exports = {
|
2018-05-29 20:32:13 -04:00
|
|
|
importToBranch
|
2020-05-16 23:12:29 +02:00
|
|
|
};
|