server-ts: Fix build errors

This commit is contained in:
Elian Doran 2024-02-25 15:06:43 +02:00
parent 7f0102181d
commit 8c5f680dca
No known key found for this signature in database
4 changed files with 34 additions and 19 deletions

View File

@ -103,13 +103,15 @@ function getType(options: GetTypeOpts, mime: string) {
function normalizeMimeType(mime: string) { function normalizeMimeType(mime: string) {
mime = mime ? mime.toLowerCase() : ''; mime = mime ? mime.toLowerCase() : '';
const mappedMime = CODE_MIME_TYPES[mime];
if (!(mime in CODE_MIME_TYPES) || CODE_MIME_TYPES[mime] === true) { if (mappedMime === true) {
return mime; return mime;
} else if (typeof mappedMime === "string") {
return mappedMime;
} }
else {
return CODE_MIME_TYPES[mime]; return undefined;
}
} }
export = { export = {

View File

@ -12,7 +12,13 @@ import utils = require('../../services/utils');
import importUtils = require('./utils'); import importUtils = require('./utils');
import htmlSanitizer = require('../html_sanitizer'); import htmlSanitizer = require('../html_sanitizer');
function importSingleFile(taskContext: TaskContext, file, parentNote: BNote) { interface File {
originalname: string;
mimetype: string;
buffer: string | Buffer;
}
function importSingleFile(taskContext: TaskContext, file: 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) {
@ -36,15 +42,21 @@ function importSingleFile(taskContext: TaskContext, file, parentNote: BNote) {
return importFile(taskContext, file, parentNote); return importFile(taskContext, file, parentNote);
} }
function importImage(file, parentNote: BNote, taskContext) { function importImage(file: File, parentNote: BNote, taskContext: TaskContext) {
const {note} = imageService.saveImage(parentNote.noteId, file.buffer, file.originalname, taskContext.data.shrinkImages); if (typeof file.buffer === "string") {
throw new Error("Invalid file content for image.");
}
const {note} = imageService.saveImage(parentNote.noteId, file.buffer, file.originalname, !!taskContext.data?.shrinkImages);
taskContext.increaseProgressCount(); taskContext.increaseProgressCount();
return note; return note;
} }
function importFile(taskContext: TaskContext, file, parentNote: BNote) { function importFile(taskContext: TaskContext, file: File, parentNote: BNote) {
if (typeof file.buffer !== "string") {
throw new Error("Invalid file content for text.");
}
const originalName = file.originalname; const originalName = file.originalname;
const {note} = noteService.createNewNote({ const {note} = noteService.createNewNote({
@ -63,8 +75,8 @@ function importFile(taskContext: TaskContext, file, parentNote: BNote) {
return note; return note;
} }
function importCodeNote(taskContext: TaskContext, file, parentNote: BNote) { function importCodeNote(taskContext: TaskContext, file: 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;
const mime = mimeService.normalizeMimeType(detectedMime); const mime = mimeService.normalizeMimeType(detectedMime);
@ -83,8 +95,8 @@ function importCodeNote(taskContext: TaskContext, file, parentNote: BNote) {
return note; return note;
} }
function importPlainText(taskContext: TaskContext, file, parentNote) { function importPlainText(taskContext: TaskContext, file: File, parentNote: BNote) {
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);
@ -120,8 +132,8 @@ function convertTextToHtml(text: string) {
return text; return text;
} }
function importMarkdown(taskContext: TaskContext, file, parentNote: BNote) { function importMarkdown(taskContext: TaskContext, file: 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);
@ -144,8 +156,8 @@ function importMarkdown(taskContext: TaskContext, file, parentNote: BNote) {
return note; return note;
} }
function importHtml(taskContext: TaskContext, file, parentNote: BNote) { function importHtml(taskContext: TaskContext, file: 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) {
@ -168,10 +180,10 @@ function importHtml(taskContext: TaskContext, file, parentNote: BNote) {
return note; return note;
} }
function importAttachment(taskContext: TaskContext, file, parentNote: BNote) { function importAttachment(taskContext: TaskContext, file: File, parentNote: BNote) {
const mime = mimeService.getMime(file.originalname) || file.mimetype; const mime = mimeService.getMime(file.originalname) || file.mimetype;
if (mime.startsWith("image/")) { if (mime.startsWith("image/") && typeof file.buffer !== "string") {
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();

View File

@ -10,6 +10,7 @@ interface TaskData {
textImportedAsText?: boolean; textImportedAsText?: boolean;
codeImportedAsCode?: boolean; codeImportedAsCode?: boolean;
shrinkImages?: boolean; shrinkImages?: boolean;
replaceUnderscoresWithSpaces?: boolean;
} }
class TaskContext { class TaskContext {

View File

@ -226,7 +226,7 @@ function removeTextFileExtension(filePath: string) {
} }
} }
function getNoteTitle(filePath: string, replaceUnderscoresWithSpaces: boolean, noteMeta: { title: string }) { function getNoteTitle(filePath: string, replaceUnderscoresWithSpaces: boolean, noteMeta?: { title: string }) {
if (noteMeta) { if (noteMeta) {
return noteMeta.title; return noteMeta.title;
} else { } else {