From 1b0690ddfcaf558522e0cc7033c2b17956b59555 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 13 Jul 2024 16:43:33 +0300 Subject: [PATCH 1/3] server: Fix note type import A regression caused by the port to TypeScript caused all note types to be treated as a "text" instead of other types such as canvas. The MIME type, however, was unaffected. --- src/becca/entities/rows.ts | 3 ++- src/services/import/zip.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/becca/entities/rows.ts b/src/becca/entities/rows.ts index 4428b6dde..4258c3805 100644 --- a/src/becca/entities/rows.ts +++ b/src/becca/entities/rows.ts @@ -91,7 +91,8 @@ export interface BranchRow { * end user. Those types should be used only for checking against, they are * not for direct use. */ -export type NoteType = ("file" | "image" | "search" | "noteMap" | "launcher" | "doc" | "contentWidget" | "text" | "relationMap" | "render" | "canvas" | "mermaid" | "book" | "webView" | "code"); +export const ALLOWED_NOTE_TYPES = [ "file", "image", "search", "noteMap", "launcher", "doc", "contentWidget", "text", "relationMap", "render", "canvas", "mermaid", "book", "webView", "code" ] as const; +export type NoteType = typeof ALLOWED_NOTE_TYPES[number]; export interface NoteRow { noteId: string; diff --git a/src/services/import/zip.ts b/src/services/import/zip.ts index 93ccc2da9..203b36442 100644 --- a/src/services/import/zip.ts +++ b/src/services/import/zip.ts @@ -20,7 +20,7 @@ import BNote = require('../../becca/entities/bnote'); import NoteMeta = require('../meta/note_meta'); import AttributeMeta = require('../meta/attribute_meta'); import { Stream } from 'stream'; -import { NoteType } from '../../becca/entities/rows'; +import { ALLOWED_NOTE_TYPES, NoteType } from '../../becca/entities/rows'; interface MetaFile { files: NoteMeta[] @@ -231,7 +231,7 @@ async function importZip(taskContext: TaskContext, fileBuffer: Buffer, importRoo if (!parentNoteId) { throw new Error("Missing parent note ID."); } - + const {note} = noteService.createNewNote({ parentNoteId: parentNoteId, title: noteTitle || "", @@ -462,13 +462,14 @@ async function importZip(taskContext: TaskContext, fileBuffer: Buffer, importRoo } let type = resolveNoteType(noteMeta?.type); + console.log("Resolved note type is ", noteMeta?.type, resolveNoteType(noteMeta?.type)); if (type !== 'file' && type !== 'image') { content = content.toString("utf-8"); } const noteTitle = utils.getNoteTitle(filePath, taskContext.data?.replaceUnderscoresWithSpaces || false, noteMeta); - + content = processNoteContent(noteMeta, type, mime, content, noteTitle || "", filePath); let note = becca.getNote(noteId); @@ -653,7 +654,11 @@ function resolveNoteType(type: string | undefined): NoteType { return 'webView'; } - return "text"; + if (type && (ALLOWED_NOTE_TYPES as readonly string[]).includes(type)) { + return type as NoteType; + } else { + return "text"; + } } export = { From d99cc11d8bfc9efc8e17e00dcec6b351a46beaf8 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 13 Jul 2024 16:52:31 +0300 Subject: [PATCH 2/3] server: Fix import of notes with type=file --- src/becca/entities/rows.ts | 2 +- src/services/import/zip.ts | 4 ---- src/services/note-interface.ts | 2 +- src/services/search/expressions/note_content_fulltext.ts | 4 ++-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/becca/entities/rows.ts b/src/becca/entities/rows.ts index 4258c3805..119d392ec 100644 --- a/src/becca/entities/rows.ts +++ b/src/becca/entities/rows.ts @@ -107,5 +107,5 @@ export interface NoteRow { dateModified: string; utcDateCreated: string; utcDateModified: string; - content?: string; + content?: string | Buffer; } diff --git a/src/services/import/zip.ts b/src/services/import/zip.ts index 203b36442..f8391350b 100644 --- a/src/services/import/zip.ts +++ b/src/services/import/zip.ts @@ -500,10 +500,6 @@ async function importZip(taskContext: TaskContext, fileBuffer: Buffer, importRoo } } else { - if (typeof content !== "string") { - throw new Error("Incorrect content type."); - } - ({note} = noteService.createNewNote({ parentNoteId: parentNoteId, title: noteTitle || "", diff --git a/src/services/note-interface.ts b/src/services/note-interface.ts index 0b99c31ef..2cfa0b86a 100644 --- a/src/services/note-interface.ts +++ b/src/services/note-interface.ts @@ -7,7 +7,7 @@ export interface NoteParams { parentNoteId: string; templateNoteId?: string; title: string; - content: string; + content: string | Buffer; /** text, code, file, image, search, book, relationMap, canvas, webView */ type: NoteType; /** default value is derived from default mimes for type */ diff --git a/src/services/search/expressions/note_content_fulltext.ts b/src/services/search/expressions/note_content_fulltext.ts index ada9705a2..02626bf16 100644 --- a/src/services/search/expressions/note_content_fulltext.ts +++ b/src/services/search/expressions/note_content_fulltext.ts @@ -74,7 +74,7 @@ class NoteContentFulltextExp extends Expression { } if (isProtected) { - if (!protectedSessionService.isProtectedSessionAvailable() || !content) { + if (!protectedSessionService.isProtectedSessionAvailable() || !content || typeof content !== "string") { return; } @@ -86,7 +86,7 @@ class NoteContentFulltextExp extends Expression { } } - if (!content) { + if (!content || typeof content !== "string") { return; } From 606490a611c95bec1ee737887cad7a2344e79d0a Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 13 Jul 2024 16:55:20 +0300 Subject: [PATCH 3/3] server: Remove log and fix whitespace --- src/services/import/zip.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/services/import/zip.ts b/src/services/import/zip.ts index f8391350b..0c4008ddf 100644 --- a/src/services/import/zip.ts +++ b/src/services/import/zip.ts @@ -231,7 +231,7 @@ async function importZip(taskContext: TaskContext, fileBuffer: Buffer, importRoo if (!parentNoteId) { throw new Error("Missing parent note ID."); } - + const {note} = noteService.createNewNote({ parentNoteId: parentNoteId, title: noteTitle || "", @@ -462,14 +462,13 @@ async function importZip(taskContext: TaskContext, fileBuffer: Buffer, importRoo } let type = resolveNoteType(noteMeta?.type); - console.log("Resolved note type is ", noteMeta?.type, resolveNoteType(noteMeta?.type)); if (type !== 'file' && type !== 'image') { content = content.toString("utf-8"); } const noteTitle = utils.getNoteTitle(filePath, taskContext.data?.replaceUnderscoresWithSpaces || false, noteMeta); - + content = processNoteContent(noteMeta, type, mime, content, noteTitle || "", filePath); let note = becca.getNote(noteId);