Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port services/import/single

Elian Doran7f0102181d4f
diff --git a/src/services/image.ts b/src/services/image.ts
index 046a28bec2..0a95b87d6c 100644
--- a/src/services/image.ts
+++ b/src/services/image.ts
@@ -133,7 +133,7 @@ function saveImage(parentNoteId: string, uploadBuffer: Buffer, originalName: str
};
}
-function saveImageToAttachment(noteId: string, uploadBuffer: Buffer, originalName: string, shrinkImageSwitch: boolean, trimFilename = false) {
+function saveImageToAttachment(noteId: string, uploadBuffer: Buffer, originalName: string, shrinkImageSwitch?: boolean, trimFilename = false) {
log.info(`Saving image '${originalName}' as attachment into note '${noteId}'`);
if (trimFilename && originalName.length > 40) {
@@ -162,7 +162,7 @@ function saveImageToAttachment(noteId: string, uploadBuffer: Buffer, originalNam
}, 5000);
// resizing images asynchronously since JIMP does not support sync operation
- processImage(uploadBuffer, originalName, shrinkImageSwitch).then(({buffer, imageFormat}) => {
+ processImage(uploadBuffer, originalName, !!shrinkImageSwitch).then(({buffer, imageFormat}) => {
sql.transactional(() => {
// re-read, might be changed in the meantime
if (!attachment.attachmentId) { throw new Error("Missing attachment ID."); }
@@ -234,7 +234,7 @@ async function resize(buffer: Buffer, quality: number) {
return resultBuffer;
}
-module.exports = {
+export = {
saveImage,
saveImageToAttachment,
updateImage
diff --git a/src/services/import/mime.ts b/src/services/import/mime.ts
index 92a6e4ea7a..9cb71c3d5a 100644
--- a/src/services/import/mime.ts
+++ b/src/services/import/mime.ts
@@ -80,8 +80,8 @@ function getMime(fileName: string) {
}
interface GetTypeOpts {
- textImportedAsText: boolean;
- codeImportedAsCode: boolean;
+ textImportedAsText?: boolean;
+ codeImportedAsCode?: boolean;
}
function getType(options: GetTypeOpts, mime: string) {
diff --git a/src/services/import/single.js b/src/services/import/single.ts
similarity index 76%
rename from src/services/import/single.js
rename to src/services/import/single.ts
index 31928e7fe3..18c45b85ee 100644
--- a/src/services/import/single.js
+++ b/src/services/import/single.ts
@@ -1,18 +1,21 @@
"use strict";
-const noteService = require('../../services/notes');
-const imageService = require('../../services/image');
-const protectedSessionService = require('../protected_session');
-const markdownService = require('./markdown');
-const mimeService = require('./mime');
-const utils = require('../../services/utils');
-const importUtils = require('./utils');
-const htmlSanitizer = require('../html_sanitizer');
-
-function importSingleFile(taskContext, file, parentNote) {
+import BNote = require("../../becca/entities/bnote");
+import TaskContext = require("../task_context");
+
+import noteService = require('../../services/notes');
+import imageService = require('../../services/image');
+import protectedSessionService = require('../protected_session');
+import markdownService = require('./markdown');
+import mimeService = require('./mime');
+import utils = require('../../services/utils');
+import importUtils = require('./utils');
+import htmlSanitizer = require('../html_sanitizer');
+
+function importSingleFile(taskContext: TaskContext, file, parentNote: BNote) {
const mime = mimeService.getMime(file.originalname) || file.mimetype;
- if (taskContext.data.textImportedAsText) {
+ if (taskContext?.data?.textImportedAsText) {
if (mime === 'text/html') {
return importHtml(taskContext, file, parentNote);
} else if (['text/markdown', 'text/x-markdown'].includes(mime)) {
@@ -22,7 +25,7 @@ function importSingleFile(taskContext, file, parentNote) {
}
}
- if (taskContext.data.codeImportedAsCode && mimeService.getType(taskContext.data, mime) === 'code') {
+ if (taskContext?.data?.codeImportedAsCode && mimeService.getType(taskContext.data, mime) === 'code') {
return importCodeNote(taskContext, file, parentNote);
}
@@ -33,7 +36,7 @@ function importSingleFile(taskContext, file, parentNote) {
return importFile(taskContext, file, parentNote);
}
-function importImage(file, parentNote, taskContext) {
+function importImage(file, parentNote: BNote, taskContext) {
const {note} = imageService.saveImage(parentNote.noteId, file.buffer, file.originalname, taskContext.data.shrinkImages);
taskContext.increaseProgressCount();
@@ -41,7 +44,7 @@ function importImage(file, parentNote, taskContext) {
return note;
}
-function importFile(taskContext, file, parentNote) {
+function importFile(taskContext: TaskContext, file, parentNote: BNote) {
const originalName = file.originalname;
const {note} = noteService.createNewNote({
@@ -60,7 +63,7 @@ function importFile(taskContext, file, parentNote) {
return note;
}
-function importCodeNote(taskContext, file, parentNote) {
+function importCodeNote(taskContext: TaskContext, file, parentNote: BNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
const content = file.buffer.toString("utf-8");
const detectedMime = mimeService.getMime(file.originalname) || file.mimetype;
@@ -80,7 +83,7 @@ function importCodeNote(taskContext, file, parentNote) {
return note;
}
-function importPlainText(taskContext, file, parentNote) {
+function importPlainText(taskContext: TaskContext, file, parentNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
const plainTextContent = file.buffer.toString("utf-8");
const htmlContent = convertTextToHtml(plainTextContent);
@@ -99,7 +102,7 @@ function importPlainText(taskContext, file, parentNote) {
return note;
}
-function convertTextToHtml(text) {
+function convertTextToHtml(text: string) {
// 1: Plain Text Search
text = text.replace(/&/g, "&").
replace(/</g, "&lt;").
@@ -117,13 +120,13 @@ function convertTextToHtml(text) {
return text;
}
-function importMarkdown(taskContext, file, parentNote) {
+function importMarkdown(taskContext: TaskContext, file, parentNote: BNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
const markdownContent = file.buffer.toString("utf-8");
let htmlContent = markdownService.renderToHtml(markdownContent, title);
- if (taskContext.data.safeImport) {
+ if (taskContext.data?.safeImport) {
htmlContent = htmlSanitizer.sanitize(htmlContent);
}
@@ -141,11 +144,11 @@ function importMarkdown(taskContext, file, parentNote) {
return note;
}
-function importHtml(taskContext, file, parentNote) {
+function importHtml(taskContext: TaskContext, file, parentNote: BNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
let content = file.buffer.toString("utf-8");
- if (taskContext.data.safeImport) {
+ if (taskContext?.data?.safeImport) {
content = htmlSanitizer.sanitize(content);
}
@@ -165,17 +168,11 @@ function importHtml(taskContext, file, parentNote) {
return note;
}
-/**
- * @param {TaskContext} taskContext
- * @param file
- * @param {BNote} parentNote
- * @returns {BNote}
- */
-function importAttachment(taskContext, file, parentNote) {
+function importAttachment(taskContext: TaskContext, file, parentNote: BNote) {
const mime = mimeService.getMime(file.originalname) || file.mimetype;
if (mime.startsWith("image/")) {
- imageService.saveImageToAttachment(parentNote.noteId, file.buffer, file.originalname, taskContext.data.shrinkImages);
+ imageService.saveImageToAttachment(parentNote.noteId, file.buffer, file.originalname, taskContext.data?.shrinkImages);
taskContext.increaseProgressCount();
} else {
@@ -190,7 +187,7 @@ function importAttachment(taskContext, file, parentNote) {
}
}
-module.exports = {
+export = {
importSingleFile,
importAttachment
};
diff --git a/src/services/task_context.ts b/src/services/task_context.ts
index 7d2cdc4487..23408f0dd0 100644
--- a/src/services/task_context.ts
+++ b/src/services/task_context.ts
@@ -5,13 +5,20 @@ import ws = require('./ws');
// taskId => TaskContext
const taskContexts: Record<string, TaskContext> = {};
+interface TaskData {
+ safeImport?: boolean;
+ textImportedAsText?: boolean;
+ codeImportedAsCode?: boolean;
+ shrinkImages?: boolean;
+}
+
class TaskContext {
private taskId: string;
private taskType: string | null;
- private data: {} | null;
private progressCount: number;
private lastSentCountTs: number;
+ data: TaskData | null;
noteDeletionHandlerTriggered: boolean;
constructor(taskId: string, taskType: string | null = null, data: {} | null = {}) {
diff --git a/src/services/ws.ts b/src/services/ws.ts
index 461b0e650e..f731bffafe 100644
--- a/src/services/ws.ts
+++ b/src/services/ws.ts
@@ -30,7 +30,8 @@ interface Message {
type: string;
data?: {
lastSyncedPush?: number,
- entityChanges?: any[]
+ entityChanges?: any[],
+ safeImport?: boolean
},
lastSyncedPush?: number,