diff --git a/apps/server/src/routes/api/revisions.ts b/apps/server/src/routes/api/revisions.ts index ee4041ece..b0df41922 100644 --- a/apps/server/src/routes/api/revisions.ts +++ b/apps/server/src/routes/api/revisions.ts @@ -1,7 +1,7 @@ import { EditedNotesResponse, RevisionItem, RevisionPojo } from "@triliumnext/commons"; -import { becca_service, blob as blobService, NotePojo } from "@triliumnext/core"; +import { becca_service, binary_utils, blob as blobService, NotePojo } from "@triliumnext/core"; import type { Request, Response } from "express"; import path from "path"; @@ -55,7 +55,7 @@ function getRevision(req: Request) { revision.content = revision.getContent(); if (revision.content && revision.type === "image") { - revision.content = revision.content.toString("base64"); + revision.content = binary_utils.encodeBase64(revision.content); } } diff --git a/apps/server/src/services/sync.ts b/apps/server/src/services/sync.ts index 665e140e3..6b52fc60d 100644 --- a/apps/server/src/services/sync.ts +++ b/apps/server/src/services/sync.ts @@ -1,7 +1,7 @@ import type { EntityChange, EntityChangeRecord, EntityRow } from "@triliumnext/commons"; -import { becca_loader, entity_constructor } from "@triliumnext/core"; +import { becca_loader, binary_utils, entity_constructor } from "@triliumnext/core"; import becca from "../becca/becca.js"; import appInfo from "./app_info.js"; @@ -360,7 +360,7 @@ function getEntityChangeRow(entityChange: EntityChange) { } if (entityRow.content) { - entityRow.content = entityRow.content.toString("base64"); + entityRow.content = binary_utils.encodeBase64(entityRow.content); } } diff --git a/apps/server/src/share/shaca/entities/sattachment.ts b/apps/server/src/share/shaca/entities/sattachment.ts index 1f4f1ae90..090eaf734 100644 --- a/apps/server/src/share/shaca/entities/sattachment.ts +++ b/apps/server/src/share/shaca/entities/sattachment.ts @@ -1,6 +1,7 @@ import { BlobRow } from "@triliumnext/commons"; +import { binary_utils } from "@triliumnext/core"; import utils from "../../../services/utils.js"; import sql from "../../sql.js"; @@ -43,18 +44,18 @@ class SAttachment extends AbstractShacaEntity { if (!row) { if (silentNotFoundError) { return undefined; - } + } throw new Error(`Cannot find blob for attachment '${this.attachmentId}', blob '${this.blobId}'`); - + } const content = row.content; if (this.hasStringContent()) { - return content === null ? "" : content.toString("utf-8"); - } + return content === null ? "" : binary_utils.decodeUtf8(content); + } return content; - + } /** @returns true if the attachment has string content (not binary) */ diff --git a/apps/server/src/share/shaca/entities/snote.ts b/apps/server/src/share/shaca/entities/snote.ts index 6f0b9c8c6..ab51a434f 100644 --- a/apps/server/src/share/shaca/entities/snote.ts +++ b/apps/server/src/share/shaca/entities/snote.ts @@ -1,5 +1,5 @@ import { BlobRow } from "@triliumnext/commons"; -import { NOTE_TYPE_ICONS } from "@triliumnext/core"; +import { binary_utils, NOTE_TYPE_ICONS } from "@triliumnext/core"; import escape from "escape-html"; import utils from "../../../services/utils.js"; @@ -107,7 +107,7 @@ class SNote extends AbstractShacaEntity { const content = row.content; if (this.hasStringContent()) { - return content === null ? "" : content.toString("utf-8"); + return content === null ? "" : binary_utils.decodeUtf8(content); } return content; } diff --git a/packages/trilium-core/src/services/utils/binary.ts b/packages/trilium-core/src/services/utils/binary.ts index 94272039f..54b734ca7 100644 --- a/packages/trilium-core/src/services/utils/binary.ts +++ b/packages/trilium-core/src/services/utils/binary.ts @@ -8,7 +8,8 @@ export function concat2(a: Uint8Array, b: Uint8Array): Uint8Array { return out; } -export function encodeBase64(bytes: Uint8Array): string { +export function encodeBase64(stringOrBuffer: string | Uint8Array): string { + const bytes = wrapStringOrBuffer(stringOrBuffer); let binary = ""; const len = bytes.length; @@ -31,12 +32,16 @@ export function decodeBase64(base64: string): Uint8Array { return bytes; } -export function decodeUtf8(bytes: Uint8Array) { - return utf8Decoder.decode(bytes); +export function decodeUtf8(stringOrBuffer: string | Uint8Array) { + if (typeof stringOrBuffer === "string") { + return stringOrBuffer; + } else { + return utf8Decoder.decode(stringOrBuffer); + } } -export function encodeUtf8(string: string) { - return utf8Encoder.encode(string); +export function encodeUtf8(string: string | Uint8Array) { + return utf8Encoder.encode(wrapStringOrBuffer(string)); } export function unwrapStringOrBuffer(stringOrBuffer: string | Uint8Array) {