From f88ac5dfaeff24d1fa61f439d45934008a141d59 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 6 Jan 2026 11:46:15 +0200 Subject: [PATCH] chore(server): fix imports to becca entities --- apps/server/src/becca/entities/battachment.ts | 2 + apps/server/src/becca/entities/battribute.ts | 2 + apps/server/src/becca/entities/bbranch.ts | 2 + .../server/src/becca/entities/betapi_token.ts | 2 + apps/server/src/becca/entities/bnote.ts | 2 + apps/server/src/becca/entities/boption.ts | 2 + .../server/src/becca/entities/brecent_note.ts | 2 + apps/server/src/becca/entities/brevision.ts | 2 + apps/server/src/services/import/zip.ts | 87 ++++++++++--------- packages/trilium-core/src/index.ts | 9 ++ 10 files changed, 69 insertions(+), 43 deletions(-) create mode 100644 apps/server/src/becca/entities/battachment.ts create mode 100644 apps/server/src/becca/entities/battribute.ts create mode 100644 apps/server/src/becca/entities/bbranch.ts create mode 100644 apps/server/src/becca/entities/betapi_token.ts create mode 100644 apps/server/src/becca/entities/bnote.ts create mode 100644 apps/server/src/becca/entities/boption.ts create mode 100644 apps/server/src/becca/entities/brecent_note.ts create mode 100644 apps/server/src/becca/entities/brevision.ts diff --git a/apps/server/src/becca/entities/battachment.ts b/apps/server/src/becca/entities/battachment.ts new file mode 100644 index 000000000..45a2e0cac --- /dev/null +++ b/apps/server/src/becca/entities/battachment.ts @@ -0,0 +1,2 @@ +import { BAttachment } from "@triliumnext/core"; +export default BAttachment; diff --git a/apps/server/src/becca/entities/battribute.ts b/apps/server/src/becca/entities/battribute.ts new file mode 100644 index 000000000..f7c6f70c3 --- /dev/null +++ b/apps/server/src/becca/entities/battribute.ts @@ -0,0 +1,2 @@ +import { BAttribute } from "@triliumnext/core"; +export default BAttribute; diff --git a/apps/server/src/becca/entities/bbranch.ts b/apps/server/src/becca/entities/bbranch.ts new file mode 100644 index 000000000..0c687237b --- /dev/null +++ b/apps/server/src/becca/entities/bbranch.ts @@ -0,0 +1,2 @@ +import { BBranch } from "@triliumnext/core"; +export default BBranch; diff --git a/apps/server/src/becca/entities/betapi_token.ts b/apps/server/src/becca/entities/betapi_token.ts new file mode 100644 index 000000000..114aa7400 --- /dev/null +++ b/apps/server/src/becca/entities/betapi_token.ts @@ -0,0 +1,2 @@ +import { BEtapiToken } from "@triliumnext/core"; +export default BEtapiToken; diff --git a/apps/server/src/becca/entities/bnote.ts b/apps/server/src/becca/entities/bnote.ts new file mode 100644 index 000000000..523e4e004 --- /dev/null +++ b/apps/server/src/becca/entities/bnote.ts @@ -0,0 +1,2 @@ +import { BNote } from "@triliumnext/core"; +export default BNote; diff --git a/apps/server/src/becca/entities/boption.ts b/apps/server/src/becca/entities/boption.ts new file mode 100644 index 000000000..e17b896ff --- /dev/null +++ b/apps/server/src/becca/entities/boption.ts @@ -0,0 +1,2 @@ +import { BOption } from "@triliumnext/core"; +export default BOption; diff --git a/apps/server/src/becca/entities/brecent_note.ts b/apps/server/src/becca/entities/brecent_note.ts new file mode 100644 index 000000000..e8cee478d --- /dev/null +++ b/apps/server/src/becca/entities/brecent_note.ts @@ -0,0 +1,2 @@ +import { BRecentNote } from "@triliumnext/core"; +export default BRecentNote; diff --git a/apps/server/src/becca/entities/brevision.ts b/apps/server/src/becca/entities/brevision.ts new file mode 100644 index 000000000..c11de52a4 --- /dev/null +++ b/apps/server/src/becca/entities/brevision.ts @@ -0,0 +1,2 @@ +import { BRevision } from "@triliumnext/core"; +export default BRevision; diff --git a/apps/server/src/services/import/zip.ts b/apps/server/src/services/import/zip.ts index c1ac90b91..51bb38cd0 100644 --- a/apps/server/src/services/import/zip.ts +++ b/apps/server/src/services/import/zip.ts @@ -1,26 +1,27 @@ -"use strict"; -import BAttribute from "../../becca/entities/battribute.js"; -import { removeTextFileExtension, newEntityId, getNoteTitle, processStringOrBuffer, unescapeHtml } from "../../services/utils.js"; -import log from "../../services/log.js"; -import noteService from "../../services/notes.js"; -import attributeService from "../../services/attributes.js"; -import BBranch from "../../becca/entities/bbranch.js"; + +import { ALLOWED_NOTE_TYPES, type NoteType } from "@triliumnext/commons"; import path from "path"; -import protectedSessionService from "../protected_session.js"; -import mimeService from "./mime.js"; -import treeService from "../tree.js"; +import type { Stream } from "stream"; import yauzl from "yauzl"; -import htmlSanitizer from "../html_sanitizer.js"; + import becca from "../../becca/becca.js"; import BAttachment from "../../becca/entities/battachment.js"; -import markdownService from "./markdown.js"; -import type TaskContext from "../task_context.js"; +import BAttribute from "../../becca/entities/battribute.js"; +import BBranch from "../../becca/entities/bbranch.js"; import type BNote from "../../becca/entities/bnote.js"; -import type NoteMeta from "../meta/note_meta.js"; +import attributeService from "../../services/attributes.js"; +import log from "../../services/log.js"; +import noteService from "../../services/notes.js"; +import { getNoteTitle, newEntityId, processStringOrBuffer, removeTextFileExtension, unescapeHtml } from "../../services/utils.js"; +import htmlSanitizer from "../html_sanitizer.js"; import type AttributeMeta from "../meta/attribute_meta.js"; -import type { Stream } from "stream"; -import { ALLOWED_NOTE_TYPES, type NoteType } from "@triliumnext/commons"; +import type NoteMeta from "../meta/note_meta.js"; +import protectedSessionService from "../protected_session.js"; +import type TaskContext from "../task_context.js"; +import treeService from "../tree.js"; +import markdownService from "./markdown.js"; +import mimeService from "./mime.js"; interface MetaFile { files: NoteMeta[]; @@ -108,7 +109,7 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu dataFileName: "" }; - let parent: NoteMeta | undefined = undefined; + let parent: NoteMeta | undefined; for (let segment of pathSegments) { if (!cursor?.children?.length) { @@ -241,10 +242,10 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu } const { note } = noteService.createNewNote({ - parentNoteId: parentNoteId, + parentNoteId, title: noteTitle || "", content: "", - noteId: noteId, + noteId, type: resolveNoteType(noteMeta?.type), mime: noteMeta ? noteMeta.mime : "text/html", prefix: noteMeta?.prefix || "", @@ -294,12 +295,12 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu attachmentId: getNewAttachmentId(attachmentMeta.attachmentId), noteId: getNewNoteId(noteMeta.noteId) }; - } else { - // don't check for noteMeta since it's not mandatory for notes - return { - noteId: getNoteId(noteMeta, absUrl) - }; - } + } + // don't check for noteMeta since it's not mandatory for notes + return { + noteId: getNoteId(noteMeta, absUrl) + }; + } function processTextNoteContent(content: string, noteTitle: string, filePath: string, noteMeta?: NoteMeta) { @@ -312,9 +313,9 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu content = content.replace(/

([^<]*)<\/h1>/gi, (match, text) => { if (noteTitle.trim() === text.trim()) { return ""; // remove whole H1 tag - } else { - return `

${text}

`; - } + } + return `

${text}

`; + }); if (taskContext.data?.safeImport) { @@ -347,9 +348,9 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu return `src="api/attachments/${target.attachmentId}/image/${path.basename(url)}"`; } else if (target.noteId) { return `src="api/images/${target.noteId}/${path.basename(url)}"`; - } else { - return match; - } + } + return match; + }); content = content.replace(/href="([^"]*)"/g, (match, url) => { @@ -373,9 +374,9 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu return `href="#root/${target.noteId}?viewMode=attachments&attachmentId=${target.attachmentId}"`; } else if (target.noteId) { return `href="#root/${target.noteId}"`; - } else { - return match; - } + } + return match; + }); if (noteMeta) { @@ -525,9 +526,9 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu } ({ note } = noteService.createNewNote({ - parentNoteId: parentNoteId, + parentNoteId, title: noteTitle || "", - content: content, + content, noteId, type, mime, @@ -536,7 +537,7 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu // root notePosition should be ignored since it relates to the original document // now import root should be placed after existing notes into new parent notePosition: noteMeta && firstNote ? noteMeta.notePosition : undefined, - isProtected: isProtected + isProtected })); createdNoteIds.add(note.noteId); @@ -648,7 +649,7 @@ function streamToBuffer(stream: Stream): Promise { export function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise { return new Promise((res, rej) => { - zipfile.openReadStream(entry, function (err, readStream) { + zipfile.openReadStream(entry, (err, readStream) => { if (err) rej(err); if (!readStream) throw new Error("Unable to read content."); @@ -659,7 +660,7 @@ export function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise export function readZipFile(buffer: Buffer, processEntryCallback: (zipfile: yauzl.ZipFile, entry: yauzl.Entry) => Promise) { return new Promise((res, rej) => { - yauzl.fromBuffer(buffer, { lazyEntries: true, validateEntrySizes: false }, function (err, zipfile) { + yauzl.fromBuffer(buffer, { lazyEntries: true, validateEntrySizes: false }, (err, zipfile) => { if (err) rej(err); if (!zipfile) throw new Error("Unable to read zip file."); @@ -691,9 +692,9 @@ function resolveNoteType(type: string | undefined): NoteType { if (type && (ALLOWED_NOTE_TYPES as readonly string[]).includes(type)) { return type as NoteType; - } else { - return "text"; - } + } + return "text"; + } export function removeTriliumTags(content: string) { @@ -702,7 +703,7 @@ export function removeTriliumTags(content: string) { "([^<]*)<\/title>" ]; for (const tag of tagsToRemove) { - let re = new RegExp(tag, "gi"); + const re = new RegExp(tag, "gi"); content = content.replace(re, ""); } diff --git a/packages/trilium-core/src/index.ts b/packages/trilium-core/src/index.ts index 2922f6c81..c73eb6c80 100644 --- a/packages/trilium-core/src/index.ts +++ b/packages/trilium-core/src/index.ts @@ -16,6 +16,15 @@ export * from "./errors"; export type { CryptoProvider } from "./services/encryption/crypto"; export { default as becca } from "./becca/becca"; +export { default as BAttachment } from "./becca/entities/battachment"; +export { default as BAttribute } from "./becca/entities/battribute"; +export { default as BBlob } from "./becca/entities/bblob"; +export { default as BBranch } from "./becca/entities/bbranch"; +export { default as BEtapiToken } from "./becca/entities/betapi_token"; +export { default as BNote } from "./becca/entities/bnote"; +export { default as BOption } from "./becca/entities/boption"; +export { default as BRecentNote } from "./becca/entities/brecent_note"; +export { default as BRevision } from "./becca/entities/brevision"; export function initializeCore({ dbConfig, executionContext, crypto }: { dbConfig: SqlServiceParams,