2017-12-02 23:41:18 -05:00
|
|
|
"use strict";
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import enexImportService from "../../services/import/enex.js";
|
|
|
|
import opmlImportService from "../../services/import/opml.js";
|
|
|
|
import zipImportService from "../../services/import/zip.js";
|
|
|
|
import singleImportService from "../../services/import/single.js";
|
|
|
|
import cls from "../../services/cls.js";
|
2024-04-06 21:30:27 +03:00
|
|
|
import path = require('path');
|
2024-07-18 21:35:17 +03:00
|
|
|
import becca from "../../becca/becca.js";
|
|
|
|
import beccaLoader from "../../becca/becca_loader.js";
|
|
|
|
import log from "../../services/log.js";
|
|
|
|
import TaskContext from "../../services/task_context.js";
|
|
|
|
import ValidationError from "../../errors/validation_error.js";
|
2024-04-06 21:30:27 +03:00
|
|
|
import { Request } from 'express';
|
2024-07-18 21:35:17 +03:00
|
|
|
import BNote from "../../becca/entities/bnote.js";
|
2024-04-07 14:29:08 +03:00
|
|
|
import { AppRequest } from '../route-interface';
|
2024-04-06 21:30:27 +03:00
|
|
|
|
2024-04-07 14:29:08 +03:00
|
|
|
async function importNotesToBranch(req: AppRequest) {
|
2024-03-17 21:29:57 +02:00
|
|
|
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',
|
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
|
|
|
|
2024-04-07 14:29:08 +03:00
|
|
|
const file = req.file;
|
2018-05-29 20:32:13 -04:00
|
|
|
|
2018-09-10 23:41:11 +02:00
|
|
|
if (!file) {
|
2022-12-09 16:04:13 +01:00
|
|
|
throw new ValidationError("No file has been uploaded");
|
2018-09-03 21:06:24 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 00:02:08 +02:00
|
|
|
const parentNote = becca.getNoteOrThrow(parentNoteId);
|
2018-05-29 20:32:13 -04:00
|
|
|
|
|
|
|
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
|
2021-09-16 15:02:20 +02:00
|
|
|
cls.ignoreEntityChangeIds();
|
2021-02-16 22:51:00 +01:00
|
|
|
|
2024-04-06 21:30:27 +03:00
|
|
|
let note: BNote | null; // typically root of the import - client can show it after finishing the import
|
2019-01-08 20:19:41 +01:00
|
|
|
|
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 {
|
2024-04-07 14:29:08 +03:00
|
|
|
if (extension === '.zip' && options.explodeArchives && typeof file.buffer !== "string") {
|
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) {
|
2024-04-06 21:30:27 +03:00
|
|
|
const importResult = await opmlImportService.importOpml(taskContext, file.buffer, parentNote);
|
|
|
|
if (!Array.isArray(importResult)) {
|
|
|
|
note = importResult;
|
|
|
|
} else {
|
|
|
|
return importResult;
|
|
|
|
}
|
2019-02-25 22:38:48 +01:00
|
|
|
} else if (extension === '.enex' && options.explodeArchives) {
|
2024-04-06 21:30:27 +03:00
|
|
|
const importResult = await enexImportService.importEnex(taskContext, file, parentNote);
|
|
|
|
if (!Array.isArray(importResult)) {
|
|
|
|
note = importResult;
|
|
|
|
} else {
|
|
|
|
return importResult;
|
|
|
|
}
|
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
|
|
|
}
|
2024-04-06 21:30:27 +03:00
|
|
|
catch (e: any) {
|
2022-12-21 15:19:05 +01:00
|
|
|
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
|
|
|
|
2024-04-06 21:30:27 +03:00
|
|
|
if (!note) {
|
|
|
|
return [500, "No note was generated as a result of the import."];
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:31:58 +02: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,
|
2024-04-06 21:30:27 +03:00
|
|
|
importedNoteId: note?.noteId
|
2019-10-19 00:11:07 +02:00
|
|
|
}), 1000);
|
2019-10-14 10:31:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 00:02:08 +02:00
|
|
|
// import has deactivated note events so becca is not updated, instead we force it to reload
|
|
|
|
beccaLoader.load();
|
|
|
|
|
|
|
|
return note.getPojo();
|
|
|
|
}
|
|
|
|
|
2024-04-07 14:29:08 +03:00
|
|
|
async function importAttachmentsToNote(req: AppRequest) {
|
2024-03-17 21:29:57 +02:00
|
|
|
const { parentNoteId } = req.params;
|
|
|
|
const { taskId, last } = req.body;
|
2023-05-08 00:02:08 +02:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
shrinkImages: req.body.shrinkImages !== 'false',
|
|
|
|
};
|
|
|
|
|
2024-04-07 14:29:08 +03:00
|
|
|
const file = req.file;
|
2023-05-08 00:02:08 +02:00
|
|
|
|
|
|
|
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-05-08 00:02:08 +02:00
|
|
|
|
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
|
2023-05-08 00:02:08 +02:00
|
|
|
|
|
|
|
try {
|
2023-05-09 00:05:27 +02:00
|
|
|
await singleImportService.importAttachment(taskContext, file, parentNote);
|
2023-05-08 00:02:08 +02:00
|
|
|
}
|
2024-04-06 21:30:27 +03:00
|
|
|
catch (e: any) {
|
2023-05-08 00:02:08 +02:00
|
|
|
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
|
2023-05-08 00:02:08 +02:00
|
|
|
}), 1000);
|
|
|
|
}
|
2018-05-29 20:32:13 -04:00
|
|
|
}
|
|
|
|
|
2024-04-06 21:30:27 +03:00
|
|
|
export = {
|
2023-05-08 00:02:08 +02:00
|
|
|
importNotesToBranch,
|
|
|
|
importAttachmentsToNote
|
2020-05-16 23:12:29 +02:00
|
|
|
};
|