server-ts: Port services/import/single

This commit is contained in:
Elian Doran 2024-02-25 14:52:20 +02:00
parent 59d618f06b
commit 7f0102181d
No known key found for this signature in database
5 changed files with 41 additions and 36 deletions

View File

@ -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}'`); log.info(`Saving image '${originalName}' as attachment into note '${noteId}'`);
if (trimFilename && originalName.length > 40) { if (trimFilename && originalName.length > 40) {
@ -162,7 +162,7 @@ function saveImageToAttachment(noteId: string, uploadBuffer: Buffer, originalNam
}, 5000); }, 5000);
// resizing images asynchronously since JIMP does not support sync operation // 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(() => { sql.transactional(() => {
// re-read, might be changed in the meantime // re-read, might be changed in the meantime
if (!attachment.attachmentId) { throw new Error("Missing attachment ID."); } if (!attachment.attachmentId) { throw new Error("Missing attachment ID."); }
@ -234,7 +234,7 @@ async function resize(buffer: Buffer, quality: number) {
return resultBuffer; return resultBuffer;
} }
module.exports = { export = {
saveImage, saveImage,
saveImageToAttachment, saveImageToAttachment,
updateImage updateImage

View File

@ -80,8 +80,8 @@ function getMime(fileName: string) {
} }
interface GetTypeOpts { interface GetTypeOpts {
textImportedAsText: boolean; textImportedAsText?: boolean;
codeImportedAsCode: boolean; codeImportedAsCode?: boolean;
} }
function getType(options: GetTypeOpts, mime: string) { function getType(options: GetTypeOpts, mime: string) {

View File

@ -1,18 +1,21 @@
"use strict"; "use strict";
const noteService = require('../../services/notes'); import BNote = require("../../becca/entities/bnote");
const imageService = require('../../services/image'); import TaskContext = require("../task_context");
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 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; const mime = mimeService.getMime(file.originalname) || file.mimetype;
if (taskContext.data.textImportedAsText) { if (taskContext?.data?.textImportedAsText) {
if (mime === 'text/html') { if (mime === 'text/html') {
return importHtml(taskContext, file, parentNote); return importHtml(taskContext, file, parentNote);
} else if (['text/markdown', 'text/x-markdown'].includes(mime)) { } 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); return importCodeNote(taskContext, file, parentNote);
} }
@ -33,7 +36,7 @@ function importSingleFile(taskContext, file, parentNote) {
return importFile(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); const {note} = imageService.saveImage(parentNote.noteId, file.buffer, file.originalname, taskContext.data.shrinkImages);
taskContext.increaseProgressCount(); taskContext.increaseProgressCount();
@ -41,7 +44,7 @@ function importImage(file, parentNote, taskContext) {
return note; return note;
} }
function importFile(taskContext, file, parentNote) { function importFile(taskContext: TaskContext, file, parentNote: BNote) {
const originalName = file.originalname; const originalName = file.originalname;
const {note} = noteService.createNewNote({ const {note} = noteService.createNewNote({
@ -60,7 +63,7 @@ function importFile(taskContext, file, parentNote) {
return note; return note;
} }
function importCodeNote(taskContext, file, parentNote) { function importCodeNote(taskContext: TaskContext, file, parentNote: BNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces); const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
const content = file.buffer.toString("utf-8"); const content = file.buffer.toString("utf-8");
const detectedMime = mimeService.getMime(file.originalname) || file.mimetype; const detectedMime = mimeService.getMime(file.originalname) || file.mimetype;
@ -80,7 +83,7 @@ function importCodeNote(taskContext, file, parentNote) {
return note; return note;
} }
function importPlainText(taskContext, file, parentNote) { function importPlainText(taskContext: TaskContext, file, parentNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces); const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
const plainTextContent = file.buffer.toString("utf-8"); const plainTextContent = file.buffer.toString("utf-8");
const htmlContent = convertTextToHtml(plainTextContent); const htmlContent = convertTextToHtml(plainTextContent);
@ -99,7 +102,7 @@ function importPlainText(taskContext, file, parentNote) {
return note; return note;
} }
function convertTextToHtml(text) { function convertTextToHtml(text: string) {
// 1: Plain Text Search // 1: Plain Text Search
text = text.replace(/&/g, "&"). text = text.replace(/&/g, "&").
replace(/</g, "&lt;"). replace(/</g, "&lt;").
@ -117,13 +120,13 @@ function convertTextToHtml(text) {
return 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 title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
const markdownContent = file.buffer.toString("utf-8"); const markdownContent = file.buffer.toString("utf-8");
let htmlContent = markdownService.renderToHtml(markdownContent, title); let htmlContent = markdownService.renderToHtml(markdownContent, title);
if (taskContext.data.safeImport) { if (taskContext.data?.safeImport) {
htmlContent = htmlSanitizer.sanitize(htmlContent); htmlContent = htmlSanitizer.sanitize(htmlContent);
} }
@ -141,11 +144,11 @@ function importMarkdown(taskContext, file, parentNote) {
return note; return note;
} }
function importHtml(taskContext, file, parentNote) { function importHtml(taskContext: TaskContext, file, parentNote: BNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces); const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
let content = file.buffer.toString("utf-8"); let content = file.buffer.toString("utf-8");
if (taskContext.data.safeImport) { if (taskContext?.data?.safeImport) {
content = htmlSanitizer.sanitize(content); content = htmlSanitizer.sanitize(content);
} }
@ -165,17 +168,11 @@ function importHtml(taskContext, file, parentNote) {
return note; return note;
} }
/** function importAttachment(taskContext: TaskContext, file, parentNote: BNote) {
* @param {TaskContext} taskContext
* @param file
* @param {BNote} parentNote
* @returns {BNote}
*/
function importAttachment(taskContext, file, parentNote) {
const mime = mimeService.getMime(file.originalname) || file.mimetype; const mime = mimeService.getMime(file.originalname) || file.mimetype;
if (mime.startsWith("image/")) { 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(); taskContext.increaseProgressCount();
} else { } else {
@ -190,7 +187,7 @@ function importAttachment(taskContext, file, parentNote) {
} }
} }
module.exports = { export = {
importSingleFile, importSingleFile,
importAttachment importAttachment
}; };

View File

@ -5,13 +5,20 @@ import ws = require('./ws');
// taskId => TaskContext // taskId => TaskContext
const taskContexts: Record<string, TaskContext> = {}; const taskContexts: Record<string, TaskContext> = {};
interface TaskData {
safeImport?: boolean;
textImportedAsText?: boolean;
codeImportedAsCode?: boolean;
shrinkImages?: boolean;
}
class TaskContext { class TaskContext {
private taskId: string; private taskId: string;
private taskType: string | null; private taskType: string | null;
private data: {} | null;
private progressCount: number; private progressCount: number;
private lastSentCountTs: number; private lastSentCountTs: number;
data: TaskData | null;
noteDeletionHandlerTriggered: boolean; noteDeletionHandlerTriggered: boolean;
constructor(taskId: string, taskType: string | null = null, data: {} | null = {}) { constructor(taskId: string, taskType: string | null = null, data: {} | null = {}) {

View File

@ -30,7 +30,8 @@ interface Message {
type: string; type: string;
data?: { data?: {
lastSyncedPush?: number, lastSyncedPush?: number,
entityChanges?: any[] entityChanges?: any[],
safeImport?: boolean
}, },
lastSyncedPush?: number, lastSyncedPush?: number,