mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
server-ts: Convert routes/api/import
This commit is contained in:
parent
291b791b67
commit
3902719008
@ -1,18 +1,20 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const enexImportService = require('../../services/import/enex');
|
import enexImportService = require('../../services/import/enex');
|
||||||
const opmlImportService = require('../../services/import/opml');
|
import opmlImportService = require('../../services/import/opml');
|
||||||
const zipImportService = require('../../services/import/zip');
|
import zipImportService = require('../../services/import/zip');
|
||||||
const singleImportService = require('../../services/import/single');
|
import singleImportService = require('../../services/import/single');
|
||||||
const cls = require('../../services/cls');
|
import cls = require('../../services/cls');
|
||||||
const path = require('path');
|
import path = require('path');
|
||||||
const becca = require('../../becca/becca');
|
import becca = require('../../becca/becca');
|
||||||
const beccaLoader = require('../../becca/becca_loader');
|
import beccaLoader = require('../../becca/becca_loader');
|
||||||
const log = require('../../services/log');
|
import log = require('../../services/log');
|
||||||
const TaskContext = require('../../services/task_context');
|
import TaskContext = require('../../services/task_context');
|
||||||
const ValidationError = require('../../errors/validation_error');
|
import ValidationError = require('../../errors/validation_error');
|
||||||
|
import { Request } from 'express';
|
||||||
|
import BNote = require('../../becca/entities/bnote');
|
||||||
|
|
||||||
async function importNotesToBranch(req) {
|
async function importNotesToBranch(req: Request) {
|
||||||
const { parentNoteId } = req.params;
|
const { parentNoteId } = req.params;
|
||||||
const { taskId, last } = req.body;
|
const { taskId, last } = req.body;
|
||||||
|
|
||||||
@ -25,7 +27,7 @@ async function importNotesToBranch(req) {
|
|||||||
replaceUnderscoresWithSpaces: req.body.replaceUnderscoresWithSpaces !== 'false'
|
replaceUnderscoresWithSpaces: req.body.replaceUnderscoresWithSpaces !== 'false'
|
||||||
};
|
};
|
||||||
|
|
||||||
const file = req.file;
|
const file = (req as any).file;
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
throw new ValidationError("No file has been uploaded");
|
throw new ValidationError("No file has been uploaded");
|
||||||
@ -42,7 +44,7 @@ async function importNotesToBranch(req) {
|
|||||||
// eliminate flickering during import
|
// eliminate flickering during import
|
||||||
cls.ignoreEntityChangeIds();
|
cls.ignoreEntityChangeIds();
|
||||||
|
|
||||||
let note; // typically root of the import - client can show it after finishing the import
|
let note: BNote | null; // typically root of the import - client can show it after finishing the import
|
||||||
|
|
||||||
const taskContext = TaskContext.getInstance(taskId, 'importNotes', options);
|
const taskContext = TaskContext.getInstance(taskId, 'importNotes', options);
|
||||||
|
|
||||||
@ -50,14 +52,24 @@ async function importNotesToBranch(req) {
|
|||||||
if (extension === '.zip' && options.explodeArchives) {
|
if (extension === '.zip' && options.explodeArchives) {
|
||||||
note = await zipImportService.importZip(taskContext, file.buffer, parentNote);
|
note = await zipImportService.importZip(taskContext, file.buffer, parentNote);
|
||||||
} else if (extension === '.opml' && options.explodeArchives) {
|
} else if (extension === '.opml' && options.explodeArchives) {
|
||||||
note = await opmlImportService.importOpml(taskContext, file.buffer, parentNote);
|
const importResult = await opmlImportService.importOpml(taskContext, file.buffer, parentNote);
|
||||||
|
if (!Array.isArray(importResult)) {
|
||||||
|
note = importResult;
|
||||||
|
} else {
|
||||||
|
return importResult;
|
||||||
|
}
|
||||||
} else if (extension === '.enex' && options.explodeArchives) {
|
} else if (extension === '.enex' && options.explodeArchives) {
|
||||||
note = await enexImportService.importEnex(taskContext, file, parentNote);
|
const importResult = await enexImportService.importEnex(taskContext, file, parentNote);
|
||||||
|
if (!Array.isArray(importResult)) {
|
||||||
|
note = importResult;
|
||||||
|
} else {
|
||||||
|
return importResult;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
note = await singleImportService.importSingleFile(taskContext, file, parentNote);
|
note = await singleImportService.importSingleFile(taskContext, file, parentNote);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e: any) {
|
||||||
const message = `Import failed with following error: '${e.message}'. More details might be in the logs.`;
|
const message = `Import failed with following error: '${e.message}'. More details might be in the logs.`;
|
||||||
taskContext.reportError(message);
|
taskContext.reportError(message);
|
||||||
|
|
||||||
@ -66,11 +78,15 @@ async function importNotesToBranch(req) {
|
|||||||
return [500, message];
|
return [500, message];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!note) {
|
||||||
|
return [500, "No note was generated as a result of the import."];
|
||||||
|
}
|
||||||
|
|
||||||
if (last === "true") {
|
if (last === "true") {
|
||||||
// small timeout to avoid race condition (the message is received before the transaction is committed)
|
// small timeout to avoid race condition (the message is received before the transaction is committed)
|
||||||
setTimeout(() => taskContext.taskSucceeded({
|
setTimeout(() => taskContext.taskSucceeded({
|
||||||
parentNoteId: parentNoteId,
|
parentNoteId: parentNoteId,
|
||||||
importedNoteId: note.noteId
|
importedNoteId: note?.noteId
|
||||||
}), 1000);
|
}), 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +96,7 @@ async function importNotesToBranch(req) {
|
|||||||
return note.getPojo();
|
return note.getPojo();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function importAttachmentsToNote(req) {
|
async function importAttachmentsToNote(req: Request) {
|
||||||
const { parentNoteId } = req.params;
|
const { parentNoteId } = req.params;
|
||||||
const { taskId, last } = req.body;
|
const { taskId, last } = req.body;
|
||||||
|
|
||||||
@ -88,7 +104,7 @@ async function importAttachmentsToNote(req) {
|
|||||||
shrinkImages: req.body.shrinkImages !== 'false',
|
shrinkImages: req.body.shrinkImages !== 'false',
|
||||||
};
|
};
|
||||||
|
|
||||||
const file = req.file;
|
const file = (req as any).file;
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
throw new ValidationError("No file has been uploaded");
|
throw new ValidationError("No file has been uploaded");
|
||||||
@ -102,7 +118,7 @@ async function importAttachmentsToNote(req) {
|
|||||||
try {
|
try {
|
||||||
await singleImportService.importAttachment(taskContext, file, parentNote);
|
await singleImportService.importAttachment(taskContext, file, parentNote);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e: any) {
|
||||||
const message = `Import failed with following error: '${e.message}'. More details might be in the logs.`;
|
const message = `Import failed with following error: '${e.message}'. More details might be in the logs.`;
|
||||||
taskContext.reportError(message);
|
taskContext.reportError(message);
|
||||||
|
|
||||||
@ -119,7 +135,7 @@ async function importAttachmentsToNote(req) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export = {
|
||||||
importNotesToBranch,
|
importNotesToBranch,
|
||||||
importAttachmentsToNote
|
importAttachmentsToNote
|
||||||
};
|
};
|
@ -37,7 +37,7 @@ const loginApiRoute = require('./api/login.js');
|
|||||||
const recentNotesRoute = require('./api/recent_notes.js');
|
const recentNotesRoute = require('./api/recent_notes.js');
|
||||||
const appInfoRoute = require('./api/app_info');
|
const appInfoRoute = require('./api/app_info');
|
||||||
const exportRoute = require('./api/export');
|
const exportRoute = require('./api/export');
|
||||||
const importRoute = require('./api/import.js');
|
const importRoute = require('./api/import');
|
||||||
const setupApiRoute = require('./api/setup.js');
|
const setupApiRoute = require('./api/setup.js');
|
||||||
const sqlRoute = require('./api/sql');
|
const sqlRoute = require('./api/sql');
|
||||||
const databaseRoute = require('./api/database');
|
const databaseRoute = require('./api/database');
|
||||||
|
@ -55,7 +55,7 @@ interface Note {
|
|||||||
let note: Partial<Note> = {};
|
let note: Partial<Note> = {};
|
||||||
let resource: Resource;
|
let resource: Resource;
|
||||||
|
|
||||||
function importEnex(taskContext: TaskContext, file: File, parentNote: BNote) {
|
function importEnex(taskContext: TaskContext, file: File, parentNote: BNote): Promise<BNote> {
|
||||||
const saxStream = sax.createStream(true);
|
const saxStream = sax.createStream(true);
|
||||||
|
|
||||||
const rootNoteTitle = file.originalname.toLowerCase().endsWith(".enex")
|
const rootNoteTitle = file.originalname.toLowerCase().endsWith(".enex")
|
||||||
|
@ -66,7 +66,7 @@ class TaskContext {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
taskSucceeded(result?: string) {
|
taskSucceeded(result?: string | Record<string, string | undefined>) {
|
||||||
ws.sendMessageToAllClients({
|
ws.sendMessageToAllClients({
|
||||||
type: 'taskSucceeded',
|
type: 'taskSucceeded',
|
||||||
taskId: this.taskId,
|
taskId: this.taskId,
|
||||||
|
@ -42,7 +42,7 @@ interface Message {
|
|||||||
taskType?: string | null;
|
taskType?: string | null;
|
||||||
message?: string;
|
message?: string;
|
||||||
reason?: string;
|
reason?: string;
|
||||||
result?: string;
|
result?: string | Record<string, string | undefined>;
|
||||||
|
|
||||||
script?: string;
|
script?: string;
|
||||||
params?: any[];
|
params?: any[];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user