2017-12-03 12:41:18 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-01 23:42:12 +08:00
|
|
|
const repository = require('../../services/repository');
|
2018-11-16 19:12:04 +08:00
|
|
|
const enexImportService = require('../../services/import/enex');
|
|
|
|
const opmlImportService = require('../../services/import/opml');
|
|
|
|
const tarImportService = require('../../services/import/tar');
|
2018-11-27 06:47:02 +08:00
|
|
|
const singleImportService = require('../../services/import/single');
|
2018-11-27 05:27:57 +08:00
|
|
|
const cls = require('../../services/cls');
|
2018-02-26 13:07:43 +08:00
|
|
|
const path = require('path');
|
2019-01-09 03:19:41 +08:00
|
|
|
const noteCacheService = require('../../services/note_cache');
|
2019-02-11 02:53:57 +08:00
|
|
|
const log = require('../../services/log');
|
2019-10-18 03:11:35 +08:00
|
|
|
const TaskContext = require('../../services/task_context.js');
|
2019-02-11 02:36:03 +08:00
|
|
|
|
2018-05-30 08:32:13 +08:00
|
|
|
async function importToBranch(req) {
|
2019-02-24 16:56:00 +08:00
|
|
|
const {parentNoteId} = req.params;
|
2019-10-18 03:11:35 +08:00
|
|
|
const {taskId, last} = req.body;
|
2019-02-12 06:45:58 +08:00
|
|
|
|
2019-02-24 19:24:28 +08:00
|
|
|
const options = {
|
|
|
|
safeImport: req.body.safeImport !== 'false',
|
2019-02-26 04:22:57 +08:00
|
|
|
shrinkImages: req.body.shrinkImages !== 'false',
|
2019-02-24 19:24:28 +08:00
|
|
|
textImportedAsText: req.body.textImportedAsText !== 'false',
|
2019-02-26 05:38:48 +08:00
|
|
|
codeImportedAsCode: req.body.codeImportedAsCode !== 'false',
|
|
|
|
explodeArchives: req.body.explodeArchives !== 'false'
|
2019-02-24 19:24:28 +08:00
|
|
|
};
|
2019-02-12 06:45:58 +08:00
|
|
|
|
2018-05-30 08:32:13 +08:00
|
|
|
const file = req.file;
|
|
|
|
|
2018-09-11 05:41:11 +08:00
|
|
|
if (!file) {
|
2018-09-04 03:06:24 +08:00
|
|
|
return [400, "No file has been uploaded"];
|
|
|
|
}
|
|
|
|
|
2018-05-30 08:32:13 +08:00
|
|
|
const parentNote = await repository.getNote(parentNoteId);
|
|
|
|
|
|
|
|
if (!parentNote) {
|
|
|
|
return [404, `Note ${parentNoteId} doesn't exist.`];
|
|
|
|
}
|
|
|
|
|
|
|
|
const extension = path.extname(file.originalname).toLowerCase();
|
|
|
|
|
2018-11-27 05:27:57 +08:00
|
|
|
// running all the event handlers on imported notes (and attributes) is slow
|
|
|
|
// and may produce unintended consequences
|
|
|
|
cls.disableEntityEvents();
|
|
|
|
|
2019-01-09 03:19:41 +08:00
|
|
|
let note; // typically root of the import - client can show it after finishing the import
|
|
|
|
|
2019-10-19 05:19:16 +08:00
|
|
|
const taskContext = TaskContext.getInstance(taskId, 'import', options);
|
2019-02-11 02:36:03 +08:00
|
|
|
|
2019-02-11 02:53:57 +08:00
|
|
|
try {
|
2019-02-26 05:38:48 +08:00
|
|
|
if (extension === '.tar' && options.explodeArchives) {
|
2019-10-18 03:11:35 +08:00
|
|
|
note = await tarImportService.importTar(taskContext, file.buffer, parentNote);
|
2019-02-26 05:38:48 +08:00
|
|
|
} else if (extension === '.opml' && options.explodeArchives) {
|
2019-10-18 03:11:35 +08:00
|
|
|
note = await opmlImportService.importOpml(taskContext, file.buffer, parentNote);
|
2019-02-26 05:38:48 +08:00
|
|
|
} else if (extension === '.enex' && options.explodeArchives) {
|
2019-10-18 03:11:35 +08:00
|
|
|
note = await enexImportService.importEnex(taskContext, file, parentNote);
|
2019-02-11 02:53:57 +08:00
|
|
|
} else {
|
2019-10-18 03:11:35 +08:00
|
|
|
note = await singleImportService.importSingleFile(taskContext, file, parentNote);
|
2019-02-11 02:53:57 +08:00
|
|
|
}
|
2018-09-03 19:40:40 +08:00
|
|
|
}
|
2019-02-11 02:53:57 +08:00
|
|
|
catch (e) {
|
|
|
|
const message = "Import failed with following error: '" + e.message + "'. More details might be in the logs.";
|
2019-10-18 03:11:35 +08:00
|
|
|
taskContext.reportError(message);
|
2019-02-11 02:53:57 +08:00
|
|
|
|
|
|
|
log.error(message + e.stack);
|
|
|
|
|
|
|
|
return [500, message];
|
2018-05-30 08:32:13 +08:00
|
|
|
}
|
2019-01-09 03:19:41 +08:00
|
|
|
|
2019-10-14 16:31:58 +08:00
|
|
|
if (last === "true") {
|
|
|
|
// small timeout to avoid race condition (message is received before the transaction is committed)
|
2019-10-18 03:11:35 +08:00
|
|
|
setTimeout(() => taskContext.taskSucceeded(parentNoteId, note.noteId), 1000);
|
2019-10-14 16:31:58 +08:00
|
|
|
}
|
|
|
|
|
2019-01-09 03:19:41 +08:00
|
|
|
// import has deactivated note events so note cache is not updated
|
|
|
|
// instead we force it to reload (can be async)
|
|
|
|
noteCacheService.load();
|
|
|
|
|
|
|
|
return note;
|
2018-05-30 08:32:13 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 03:34:07 +08:00
|
|
|
module.exports = {
|
2018-05-30 08:32:13 +08:00
|
|
|
importToBranch
|
2018-03-31 03:34:07 +08:00
|
|
|
};
|