Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(export): drop redundant blob copy for binary attachments
Elian Doran62e414696357
diff --git a/packages/trilium-core/src/services/export/zip.spec.ts b/packages/trilium-core/src/services/export/zip.spec.tsindex ea863bae9f..272d9a180d 100644--- a/packages/trilium-core/src/services/export/zip.spec.ts+++ b/packages/trilium-core/src/services/export/zip.spec.ts@@ -281,6 +281,27 @@ describe.skipIf(isBrowserRuntime)("zip export (real DB)", () => {expect(entries[attFileName]).toBeDefined();});+ it("round-trips binary attachment content byte-for-byte", async () => {+ const { note } = createNote("root", { title: "BinaryAttachHost", content: "<p>host</p>" });+ // Bytes that are not valid UTF-8 (0x00, 0xFF, lone 0x80 continuation byte)+ // so any accidental string coercion in the export path would corrupt them.+ const binaryContent = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x00, 0xff, 0x80, 0x01, 0xfe]);+ getContext().init(() =>+ note.saveAttachment({ role: "image", mime: "image/png", title: "pixel.png", content: binaryContent })+ );+ const branch = note.getParentBranches()[0];++ const { entries } = await exportSubtree(branch, "html");+ const rootMeta = parseMeta(entries).files[0];++ const attMeta = (rootMeta.attachments ?? [])[0];+ expect(attMeta).toBeDefined();+ const attFileName = attMeta.dataFileName ?? "";+ expect(entries[attFileName]).toBeDefined();+ // The exported bytes must equal the stored bytes exactly.+ expect(Buffer.compare(entries[attFileName], binaryContent)).toBe(0);+ });+it("keeps the extension on very long multi-byte titles within the 255-byte limit", async () => {// A title of 3-byte CJK characters long enough that, once the upstream// 255-byte sanitize cap fills the base, appending the extension woulddiff --git a/packages/trilium-core/src/services/export/zip.ts b/packages/trilium-core/src/services/export/zip.tsindex 8ea3ef7600..2a41340a50 100644--- a/packages/trilium-core/src/services/export/zip.ts+++ b/packages/trilium-core/src/services/export/zip.ts@@ -359,9 +359,12 @@ async function exportToZip(taskContext: TaskContext<"export">, branch: BBranch,}const attachment = note.getAttachmentById(attachmentMeta.attachmentId);+ // getContent() already returns a string or a Uint8Array; the binary+ // case can be appended as-is (the zip provider handles the Buffer+ // conversion), so avoid an extra full copy of the blob here.const content = attachment.getContent();- archive.append(typeof content === "string" ? content : new Uint8Array(content), {+ archive.append(content, {name: filePathPrefix + attachmentMeta.dataFileName,date: dateUtils.parseDateTime(note.utcDateModified)});