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
client: fix some issues
Adorian Dorane0c1bbdd8f4d
diff --git a/apps/client/src/widgets/type_widgets/canvas/image_to_drawing.spec.ts b/apps/client/src/widgets/type_widgets/canvas/image_to_drawing.spec.tsindex c84718dca2..12c18c3f8e 100644--- a/apps/client/src/widgets/type_widgets/canvas/image_to_drawing.spec.ts+++ b/apps/client/src/widgets/type_widgets/canvas/image_to_drawing.spec.ts@@ -5,7 +5,7 @@ import server from "../../../services/server";import toast from "../../../services/toast";import { convertImageToDrawing } from "./image_to_drawing";-vi.mock("../../../services/server", () => ({ default: { upload: vi.fn() } }));+vi.mock("../../../services/server", () => ({ default: { upload: vi.fn(), remove: vi.fn() } }));vi.mock("../../../services/toast", () => ({ default: { showError: vi.fn() } }));vi.mock("../../../services/i18n", () => ({ t: (key: string) => key }));vi.mock("@excalidraw/excalidraw", () => ({@@ -14,6 +14,7 @@ vi.mock("@excalidraw/excalidraw", () => ({}));const upload = vi.mocked(server.upload);+const remove = vi.mocked(server.remove);describe("convertImageToDrawing", () => {let imageType: string;@@ -44,6 +45,7 @@ describe("convertImageToDrawing", () => {vi.unstubAllGlobals();vi.restoreAllMocks();upload.mockReset();+ remove.mockReset();});it("attaches the image and a drawing that shows it, locked and centered", async () => {@@ -101,6 +103,18 @@ describe("convertImageToDrawing", () => {upload.mockResolvedValueOnce({ uploaded: false, message: "No." });expect(await convertImageToDrawing("n1", "api/attachments/i1/image/a.png")).toBeNull();expect(upload).toHaveBeenCalledOnce();+ expect(remove).not.toHaveBeenCalled();+ expect(toast.showError).toHaveBeenCalledOnce();+ });++ it("removes the copy of the image when the drawing cannot be attached", async () => {+ upload.mockResolvedValueOnce({ uploaded: true, url: "#root/n1?attachmentId=img1" })+ .mockResolvedValueOnce({ uploaded: false, message: "No." });+ remove.mockRejectedValueOnce(new Error("Offline."));++ expect(await convertImageToDrawing("n1", "api/attachments/i1/image/a.png", "c1"))+ .toBeNull();+ expect(remove).toHaveBeenCalledExactlyOnceWith("attachments/img1", "c1");expect(toast.showError).toHaveBeenCalledOnce();});});diff --git a/apps/client/src/widgets/type_widgets/canvas/image_to_drawing.ts b/apps/client/src/widgets/type_widgets/canvas/image_to_drawing.tsindex 675d2ca427..6c5d737eb4 100644--- a/apps/client/src/widgets/type_widgets/canvas/image_to_drawing.ts+++ b/apps/client/src/widgets/type_widgets/canvas/image_to_drawing.ts@@ -15,11 +15,13 @@ import { randomString } from "../../../services/utils";* The drawing refers to the image by file ID, so the image is attached again, titled with that ID.*/export async function convertImageToDrawing(noteId: string, src: string, componentId?: string) {+ let imageAttachmentId: string | undefined;try {const image = await fetchImage(src);const { width, height } = await getNaturalSize(image);const fileId = `${randomString(20)}.${imageExtensionForMime(image.type)}`;- await upload(noteId, new File([ image ], fileId, { type: image.type }), componentId);+ const imageFile = new File([ image ], fileId, { type: image.type });+ imageAttachmentId = await upload(noteId, imageFile, componentId);const { convertToExcalidrawElements } = await import("@excalidraw/excalidraw");const elements = convertToExcalidrawElements([ {@@ -40,6 +42,10 @@ export async function convertImageToDrawing(noteId: string, src: string, compone} catch (e) {console.error(`Could not convert the image '${src}' to a drawing canvas`, e);toast.showError(t("image.cannot-convert-to-drawing"));+ if (imageAttachmentId) {+ // The next save of the note schedules a copy left behind for erasure.+ await server.remove(`attachments/${imageAttachmentId}`, componentId).catch(() => {});+ }return null;}}diff --git a/apps/server/src/routes/api/files.spec.ts b/apps/server/src/routes/api/files.spec.tsindex fd4f57bbda..0316068ca0 100644--- a/apps/server/src/routes/api/files.spec.ts+++ b/apps/server/src/routes/api/files.spec.ts@@ -1,3 +1,4 @@+import { CANVAS_ATTACHMENT_MIME } from "@triliumnext/commons";import { becca, cls, note_service as noteService, ValidationError } from "@triliumnext/core";import type { Request } from "express";import fs from "fs";@@ -43,6 +44,30 @@ describe("Files API", () => {expect(becca.getAttachmentOrThrow(attachmentId).getContent()).toBe("edited-att");});+ it("saves a canvas drawing to a temp file with the images it refers to inside", () => {+ const drawing = cls.init(() => {+ const { note } = noteService.createNewNote({+ parentNoteId: "root", title: "Drawing note", type: "text", content: "<p>x</p>"+ });+ note.saveAttachment({+ role: "image", mime: "image/png", title: "f1.png", content: "abc"+ });+ return note.saveAttachment({+ role: "file",+ mime: CANVAS_ATTACHMENT_MIME,+ title: "Canvas.excalidraw",+ content: JSON.stringify({ elements: [ { type: "image", fileId: "f1.png" } ] })+ });+ });+ const req = fileReq({ attachmentId: drawing.attachmentId ?? "" });++ const { tmpFilePath } = cls.init(() => filesRoute.saveAttachmentToTmpDir(+ req as unknown as Request<{ attachmentId: string }>));++ const scene = JSON.parse(fs.readFileSync(tmpFilePath, "utf8"));+ expect(scene.files?.["f1.png"]?.dataURL).toBe("data:image/png;base64,YWJj");+ });+it("rejects uploading from an unknown temp path", () => {expect(() => cls.init(() => filesRoute.uploadModifiedFileToNote(fileReq({ noteId }, undefined, {}, { filePath: "/not/a/temp/file" })diff --git a/apps/server/src/routes/api/files.ts b/apps/server/src/routes/api/files.tsindex 479da5b101..0967ed499f 100644--- a/apps/server/src/routes/api/files.ts+++ b/apps/server/src/routes/api/files.ts@@ -5,7 +5,7 @@ import fs from "fs";import path from "path";import tmp from "tmp";-import { becca } from "@triliumnext/core";+import { becca, note_service as noteService } from "@triliumnext/core";import dataDirs from "../../services/data_dir.js";import { getLog } from "@triliumnext/core";import utils from "../../services/utils.js";@@ -21,7 +21,7 @@ function saveNoteToTmpDir(req: Request<{ noteId: string }>) {function saveAttachmentToTmpDir(req: Request<{ attachmentId: string }>) {const attachment = becca.getAttachmentOrThrow(req.params.attachmentId);const fileName = attachment.getFileName();- const content = attachment.getContent();+ const content = noteService.getPortableContent(attachment);if (!attachment.attachmentId) {throw new ValidationError("Missing attachment ID.");diff --git a/apps/server/src/services/notes.spec.ts b/apps/server/src/services/notes.spec.tsindex 3f6e97ccd7..64c5569ecf 100644--- a/apps/server/src/services/notes.spec.ts+++ b/apps/server/src/services/notes.spec.ts@@ -311,7 +311,7 @@ describe("checkImageAttachments", () => {expect(shown.utcDateScheduledForErasureSince).toBeTruthy();});- it("copies along with a foreign canvas drawing the images the note lacks", () => {+ it("copies with a foreign drawing the images the note lacks and keeps the rest", () => {const source = buildNote({title: "Source",attachments: [@@ -332,6 +332,8 @@ describe("checkImageAttachments", () => {attachments: [ { title: "f2.png", role: "image", mime: "image/png" } ]});mockAttachmentSaves(target);+ const [ kept ] = target.getAttachments();+ kept.utcDateScheduledForErasureSince = "2025-01-01 00:00:00.000Z";const copies: BAttachment[] = [];const setContent = vi.spyOn(BAttachment.prototype, "setContent")@@ -347,6 +349,7 @@ describe("checkImageAttachments", () => {{ ownerId: target.noteId, title: "Canvas.excalidraw" },{ ownerId: target.noteId, title: "f1.png" }]);+ expect(kept.utcDateScheduledForErasureSince).toBeNull();} finally {getAttachments.mockRestore();setContent.mockRestore();diff --git a/packages/trilium-core/src/becca/entities/battachment.spec.ts b/packages/trilium-core/src/becca/entities/battachment.spec.tsindex 8340372cbd..ac6783fa00 100644--- a/packages/trilium-core/src/becca/entities/battachment.spec.ts+++ b/packages/trilium-core/src/becca/entities/battachment.spec.ts@@ -1,6 +1,6 @@import { afterEach, describe, expect, it, vi } from "vitest";-import type { AttachmentRow } from "@triliumnext/commons";+import { type AttachmentRow, CANVAS_ATTACHMENT_MIME } from "@triliumnext/commons";import { getContext } from "../../services/context.js";import noteService from "../../services/notes.js";@@ -321,6 +321,26 @@ describe("BAttachment (real DB)", () => {expect(unwrapStringOrBuffer(note.getContent())).toBe(parentContent);});+ it("puts in a converted canvas drawing the images that it refers to", () => {+ const note = createNote();+ const drawing = getContext().init(() => {+ note.saveAttachment({+ role: "image", mime: "image/png", title: "f1.png", content: "abc"+ });+ return note.saveAttachment({+ role: "file",+ mime: CANVAS_ATTACHMENT_MIME,+ title: "Canvas.excalidraw",+ content: JSON.stringify({ elements: [ { type: "image", fileId: "f1.png" } ] })+ });+ });++ const { note: created } = getContext().init(() => drawing.convertToNote());++ const scene = JSON.parse(unwrapStringOrBuffer(created.getContent()));+ expect(scene.files?.["f1.png"]?.dataURL).toBe("data:image/png;base64,YWJj");+ });+it("turns an embed of the attachment into an include of the new note", () => {const note = createNote({ content: "<p>placeholder</p>" });const att = getContext().init(() =>diff --git a/packages/trilium-core/src/becca/entities/battachment.ts b/packages/trilium-core/src/becca/entities/battachment.tsindex a1a6fc9769..f29f556b66 100644--- a/packages/trilium-core/src/becca/entities/battachment.ts+++ b/packages/trilium-core/src/becca/entities/battachment.ts@@ -171,7 +171,7 @@ class BAttachment extends AbstractBeccaEntity<BAttachment> {title: this.title,type: (attachmentRoleToNoteTypeMapping as any)[this.role],mime: this.mime,- content: this.getContent(),+ content: noteService.getPortableContent(this),isProtected: this.isProtected});diff --git a/packages/trilium-core/src/routes/api/files.spec.ts b/packages/trilium-core/src/routes/api/files.spec.tsindex 73853a3260..8c0e9e009c 100644--- a/packages/trilium-core/src/routes/api/files.spec.ts+++ b/packages/trilium-core/src/routes/api/files.spec.ts@@ -1,3 +1,4 @@+import { CANVAS_ATTACHMENT_MIME } from "@triliumnext/commons";import ExcelJS from "exceljs";import { beforeAll, describe, expect, it } from "vitest";@@ -118,6 +119,27 @@ describe("Files API (core)", () => {expect(res.headers["Content-Disposition"]).toBeUndefined();});+ it("downloads and opens a canvas drawing with the images it refers to inside", async () => {+ const { noteId } = await createTextNote(api, { title: "Has a drawing" });+ const scene = { elements: [ { type: "image", fileId: "f1.png" } ], files: {} };+ for (const body of [+ { role: "file", mime: CANVAS_ATTACHMENT_MIME, title: "Canvas.excalidraw",+ content: JSON.stringify(scene) },+ { role: "image", mime: "image/png", title: "f1.png", content: "abc" }+ ]) {+ const save = await api.post(`/api/notes/${noteId}/attachments`, { body });+ expect(save.status).toBe(204);+ }+ const list = await api.get<AttachmentPojo[]>(`/api/notes/${noteId}/attachments`);+ const drawing = list.body.find(({ mime }) => mime === CANVAS_ATTACHMENT_MIME);++ for (const action of [ "download", "open" ]) {+ const res = await api.get(`/api/attachments/${drawing?.attachmentId}/${action}`);+ expect(JSON.parse(String(res.body)).files["f1.png"]?.dataURL)+ .toBe("data:image/png;base64,YWJj");+ }+ });+it("404s when downloading a missing attachment", async () => {const res = await api.get("/api/attachments/missingAttachment123/download");expect(res.status).toBe(404);diff --git a/packages/trilium-core/src/routes/helpers.ts b/packages/trilium-core/src/routes/helpers.tsindex 204faadbed..5ca8facf89 100644--- a/packages/trilium-core/src/routes/helpers.ts+++ b/packages/trilium-core/src/routes/helpers.ts@@ -2,6 +2,7 @@ import { isSvgMime } from "@triliumnext/commons";import { Response } from "../http_interface";import becca from "../becca/becca";import BNote from "../becca/entities/bnote";+import noteService from "../services/notes";import protected_session from "../services/protected_session";import BAttachment from "../becca/entities/battachment";import { unwrapStringOrBuffer } from "../services/utils/binary";@@ -39,7 +40,7 @@ export function downloadData(noteOrAttachment: BNote | BAttachment, res: Responsreturn sendSanitizedSvg(res, noteOrAttachment.getContent());}- res.send(noteOrAttachment.getContent());+ res.send(noteService.getPortableContent(noteOrAttachment));}/**diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.tsindex efb928792d..2b00bcbfa6 100644--- a/packages/trilium-core/src/services/notes.spec.ts+++ b/packages/trilium-core/src/services/notes.spec.ts@@ -1,3 +1,4 @@+import { CANVAS_ATTACHMENT_MIME } from "@triliumnext/commons";import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";import becca from "../becca/becca.js";@@ -14,7 +15,7 @@ import { fakeRequestProvider } from "../test/request_provider.js";import { initRequest } from "./request.js";import { getSql } from "./sql/index.js";import TaskContext from "./task_context.js";-import { encodeUtf8 } from "./utils/binary.js";+import { decodeUtf8, encodeUtf8 } from "./utils/binary.js";/*** The pure link-extraction helpers (findBookmarks, findLlmChatLinks) and the@@ -660,6 +661,63 @@ describe("notes service (real DB)", () => {expect(note.getAttachments().map((a) => a.attachmentId)).toStrictEqual([ own.attachmentId ]);});++ it("keeps the images that a drawing saved along with the content shows again", () => {+ const note = createNote("root", { title: "spec-update-drawing" }).note;+ const drawing = saveDrawing(note, sceneOf([]));+ const image = getContext().init(() => note.saveAttachment(PNG));+ const content = "<figure class=\"include-note\" "+ + `data-attachment-id="${drawing.attachmentId}"></figure>`;+ const isScheduled = () =>+ !!becca.getAttachment(image.attachmentId ?? "")?.utcDateScheduledForErasureSince;++ getContext().init(() => noteService.updateNoteData(note.noteId, content));+ expect(isScheduled()).toBe(true);++ getContext().init(() => noteService.updateNoteData(note.noteId, content, [ {+ ...DRAWING, attachmentId: drawing.attachmentId, ownerId: note.noteId,+ content: sceneOf([ PNG.title ])+ } ]));+ expect(isScheduled()).toBe(false);+ });+ });++ describe("getPortableContent", () => {+ it("puts in a canvas drawing the images that it refers to", () => {+ const note = createNote("root", { title: "spec-portable" }).note;+ const inline = {+ id: "inline", mimeType: "image/png", dataURL: "data:image/png;base64,AA=="+ };+ const drawing = saveDrawing(note, sceneOf([ PNG.title, "inline" ], { inline }));+ getContext().init(() => {+ note.saveAttachment(PNG);+ note.saveAttachment({ ...PNG, title: "unused.png" });+ note.saveAttachment({ ...PNG, title: "inline" });+ });++ const portable = JSON.parse(decodeUtf8(noteService.getPortableContent(drawing)));+ expect(portable.files).toEqual({+ inline,+ [PNG.title]: {+ id: PNG.title,+ mimeType: "image/png",+ dataURL: "data:image/png;base64,AQID",+ created: expect.any(Number)+ }+ });+ const { elements } = JSON.parse(sceneOf([ PNG.title, "inline" ]));+ expect(portable.elements).toEqual(elements);++ const json = getContext().init(() => note.saveAttachment({+ role: "file",+ mime: "application/json",+ title: "a.json",+ content: sceneOf([ PNG.title ])+ }));+ expect(decodeUtf8(noteService.getPortableContent(json))).toBe(sceneOf([ PNG.title ]));+ const malformed = saveDrawing(note, "{");+ expect(decodeUtf8(noteService.getPortableContent(malformed))).toBe("{");+ });});describe("duplicateSubtree", () => {@@ -766,6 +824,21 @@ describe("notes service (real DB)", () => {});});+const DRAWING = { role: "file", mime: CANVAS_ATTACHMENT_MIME, title: "Canvas.excalidraw" };+const PNG = {+ role: "image", mime: "image/png", title: "f1.png", content: new Uint8Array([ 1, 2, 3 ])+};++/** A canvas scene with an image element for each of `fileIds`, and `files` inline. */+function sceneOf(fileIds: string[], files: object = {}) {+ const elements = fileIds.map((fileId) => ({ type: "image", fileId }));+ return JSON.stringify({ type: "excalidraw", version: 2, elements, files });+}++function saveDrawing(note: BNote, content: string) {+ return getContext().init(() => note.saveAttachment({ ...DRAWING, content }));+}+/*** Characterization of the attachment-title derivation used by saveAttachments* (the inner label of an inline base64 `<a>` link → a plain-text title). Locksdiff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.tsindex 46eefb1434..0b54ca5f2e 100644--- a/packages/trilium-core/src/services/notes.ts+++ b/packages/trilium-core/src/services/notes.ts@@ -23,7 +23,7 @@ import { evaluateTemplateSafe } from "./safe_template.js";import { sanitizeHtml } from "./sanitizer.js";import { getSql } from "./sql/index.js";import type TaskContext from "./task_context.js";-import { decodeBase64, decodeUtf8 } from "./utils/binary.js";+import { decodeBase64, decodeUtf8, encodeBase64 } from "./utils/binary.js";import date_utils from "./utils/date.js";import { isValidEntityId, newEntityId, replaceAll, toMap, unescapeHtml } from "./utils/index.js";import ws from "./ws.js";@@ -546,9 +546,13 @@ export function checkImageAttachments(note: BNote, content: string) {}const attachments = note.getAttachments();+ const existingAttachmentIds = new Set(attachments.map((att) => att.attachmentId));+ // The attachments of other notes that the content refers to, which this note takes copies of.+ const unknownAttachments = isCanvas ? [] : becca.getAttachments(Array.from(foundAttachmentIds)+ .filter((foundAttId) => !existingAttachmentIds.has(foundAttId)));const foundFileIds = isCanvas? collectCanvasImageFileIds(content)- : collectDrawingImageFileIds(attachments, foundAttachmentIds);+ : collectDrawingImageFileIds([ ...attachments, ...unknownAttachments ], foundAttachmentIds);for (const attachment of attachments) {// Only attachments that live in the note content are auto-scheduled for erasure when nothing@@ -593,10 +597,6 @@ export function checkImageAttachments(note: BNote, content: string) {return { forceFrontendReload: false, content };}- const existingAttachmentIds = new Set<string | undefined>(attachments.map((att) => att.attachmentId));- const unknownAttachmentIds = Array.from(foundAttachmentIds).filter((foundAttId) => !existingAttachmentIds.has(foundAttId));- const unknownAttachments = becca.getAttachments(unknownAttachmentIds);-for (const unknownAttachment of unknownAttachments) {// A role says who made the attachment and why, so the copy's role is decided by how this note// refers to it. Still a link preview's picture here — the whole preview having been pasted —@@ -724,6 +724,47 @@ function copyDrawingImages(note: BNote, drawing: BAttachment) {}}+/**+ * The content of `entity` for a copy outside its note, such as a download: a canvas drawing gets+ * the images that it refers to by file ID inline, from the image attachments of its owner.+ */+function getPortableContent(entity: BNote | BAttachment) {+ const content = entity.getContent();+ const owner = entity instanceof BAttachment && entity.mime === CANVAS_ATTACHMENT_MIME+ ? becca.getNote(entity.ownerId)+ : null;+ if (!owner) {+ return content;+ }++ const json = decodeUtf8(content);+ let scene: { files?: Record<string, unknown> } | null = null;+ try {+ scene = JSON.parse(json);+ } catch {+ // Malformed content goes out as it is stored.+ }+ if (!scene || typeof scene !== "object") {+ return content;+ }++ const files = { ...scene.files };+ const fileIds = collectCanvasImageFileIds(json);+ for (const image of owner.getAttachments()) {+ if (!fileIds.has(image.title) || files[image.title]) {+ continue;+ }++ files[image.title] = {+ id: image.title,+ mimeType: image.mime,+ dataURL: `data:${image.mime};base64,${encodeBase64(image.getContent())}`,+ created: Date.now()+ };+ }+ return JSON.stringify({ ...scene, files });+}+function findImageLinks(content: string, foundLinks: FoundLink[]) {const re = /src="[^"]*api\/images\/([a-zA-Z0-9_]+)\//g;let match;@@ -1139,10 +1180,7 @@ function updateNoteData(noteId: string, content: string, attachments: AttachmentsaveRevisionIfNeeded(note);- const { forceFrontendReload, content: newContent } = saveLinks(note, content);-- note.setContent(newContent, { forceFrontendReload });-+ // Saved before the links, so that `checkImageAttachments()` reads the drawings of this save.if (attachments?.length > 0) {const existingAttachmentsByTitle = toMap(note.getAttachments(), "title");@@ -1171,6 +1209,10 @@ function updateNoteData(noteId: string, content: string, attachments: Attachment}}}++ const { forceFrontendReload, content: newContent } = saveLinks(note, content);++ note.setContent(newContent, { forceFrontendReload });}export interface UndeleteNoteOptions {@@ -1558,6 +1600,7 @@ export default {createNewNote,createNewNoteWithTarget,updateNoteData,+ getPortableContent,undeleteNote,protectNoteRecursively,duplicateSubtree,