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
fix(desktop): reuse the temp file of an unchanged drawing
Elian Doran92cb17726a4d
diff --git a/apps/server/src/routes/api/files.spec.ts b/apps/server/src/routes/api/files.spec.tsindex bdda0bb2c3..f5f986a333 100644--- a/apps/server/src/routes/api/files.spec.ts+++ b/apps/server/src/routes/api/files.spec.ts@@ -195,6 +195,29 @@ describe("Files API", () => {expect(path.basename(second)).toMatch(/-Renamed note\.txt$/);});+ it("reuses the temp file of an unchanged canvas drawing", () => {+ const drawing = cls.init(() => {+ const { note } = noteService.createNewNote({+ parentNoteId: "root", title: "Watched drawing", 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 ?? "" }) as unknown as Request<{ attachmentId: string }>;+ const first = cls.init(() => filesRoute.saveAttachmentToTmpDir(req)).tmpFilePath;+ const now = vi.spyOn(Date, "now").mockReturnValue(Date.now() + 60_000);+ try {+ expect(cls.init(() => filesRoute.saveAttachmentToTmpDir(req)).tmpFilePath).toBe(first);+ } finally {+ now.mockRestore();+ }+ });+it("reports the modification time of a changed file", () => {const tmpFilePath = save();const [ watcher ] = h.watchers;diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.tsindex 881583b8fa..f856bfe1ae 100644--- a/packages/trilium-core/src/services/notes.spec.ts+++ b/packages/trilium-core/src/services/notes.spec.ts@@ -754,6 +754,12 @@ describe("notes service (real DB)", () => {});const { elements } = JSON.parse(sceneOf([ PNG.title, "inline" ]));expect(portable.elements).toEqual(elements);+ const now = vi.spyOn(Date, "now").mockReturnValue(Date.now() + 60_000);+ try {+ expect(JSON.parse(decodeUtf8(noteService.getPortableContent(drawing)))).toEqual(portable);+ } finally {+ now.mockRestore();+ }const json = getContext().init(() => note.saveAttachment({role: "file",diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.tsindex 7042e25e94..1e041e5717 100644--- a/packages/trilium-core/src/services/notes.ts+++ b/packages/trilium-core/src/services/notes.ts@@ -793,6 +793,7 @@ function copyDrawingImages(note: BNote, drawing: BAttachment, copiedDrawing: BAt/*** 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.+ * The output depends only on the stored entities, so an unchanged drawing yields the same bytes.*/function getPortableContent(entity: BNote | BAttachment) {const content = entity.getContent();@@ -825,7 +826,7 @@ function getPortableContent(entity: BNote | BAttachment) {id: image.title,mimeType: image.mime,dataURL: `data:${image.mime};base64,${encodeBase64(image.getContent())}`,- created: Date.now()+ created: dayjs(image.getUtcDateChanged()).valueOf()};}return JSON.stringify({ ...scene, files });