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
core: fix an issue
Adorian Doranec60d19a1bb1
diff --git a/packages/trilium-core/src/routes/api/files.spec.ts b/packages/trilium-core/src/routes/api/files.spec.tsindex 9c5b6e50eb..8559236985 100644--- a/packages/trilium-core/src/routes/api/files.spec.ts+++ b/packages/trilium-core/src/routes/api/files.spec.ts@@ -291,26 +291,35 @@ describe("Files API (core)", () => {expect(becca.getAttachmentOrThrow(attachmentId).mime).toBe(CANVAS_ATTACHMENT_MIME);});- it("drops the share picture of a drawing whose file it replaces", async () => {- const { noteId } = await createTextNote(api, { title: "Has drawing" });+ it("drops only the share picture of a drawing whose file it replaces", async () => {+ const { noteId } = await createTextNote(api, { title: "Has drawings" });const attach = (role: string, mime: string, title: string) =>api.post(`/api/notes/${noteId}/attachments`, {body: { role, mime, title, content: "<svg/>" }});const getTitles = async () =>(await api.get<AttachmentPojo[]>(`/api/notes/${noteId}/attachments`)).body- .map(({ title }) => title)- .sort();- await attach("file", CANVAS_ATTACHMENT_MIME, "Canvas.excalidraw");+ .map(({ title }) => title);+ const files = [ "notes.txt", "Canvas.excalidraw", "Other.excalidraw" ];+ await attach("file", "text/plain", files[0]);+ await attach("file", CANVAS_ATTACHMENT_MIME, files[1]);+ await attach("file", CANVAS_ATTACHMENT_MIME, files[2]);const list = await api.get<AttachmentPojo[]>(`/api/notes/${noteId}/attachments`);- const drawingId = list.body[0]?.attachmentId ?? "";+ const [ textId, drawingId, otherId ] = files.map((title) =>+ list.body.find((attachment) => attachment.title === title)?.attachmentId ?? "");+ // The picture of the drawing, and look-alikes: one of a file, one not an image.await attach("image", "image/svg+xml", getDrawingPictureTitle(drawingId));- await attach("image", "image/svg+xml", "other.svg");- expect(await getTitles()).toHaveLength(3);+ await attach("image", "image/svg+xml", getDrawingPictureTitle(textId));+ await attach("file", "image/svg+xml", getDrawingPictureTitle(otherId));- const file = upload("{}", "application/octet-stream", "Drawing.excalidraw");- await api.put(`/api/attachments/${drawingId}/file`, { file });- expect(await getTitles()).toEqual([ "Canvas.excalidraw", "other.svg" ]);+ for (const [ index, attachmentId ] of [ textId, otherId, drawingId ].entries()) {+ const name = [ "notes.txt", "Other.excalidraw", "Drawing.excalidraw" ][index];+ const file = upload("{}", "application/octet-stream", name);+ await api.put(`/api/attachments/${attachmentId}/file`, { file });+ }+ expect((await getTitles()).sort()).toEqual([+ ...files, getDrawingPictureTitle(textId), getDrawingPictureTitle(otherId)+ ].sort());});it("reports a request that carries no file instead of writing an empty one", async () => {diff --git a/packages/trilium-core/src/routes/api/files.ts b/packages/trilium-core/src/routes/api/files.tsindex db375fc552..0cc02cd6fc 100644--- a/packages/trilium-core/src/routes/api/files.ts+++ b/packages/trilium-core/src/routes/api/files.ts@@ -1,4 +1,6 @@-import { getAttachedFileMime, getDrawingPictureTitle } from "@triliumnext/commons";+import {+ CANVAS_ATTACHMENT_MIME, getAttachedFileMime, getDrawingPictureTitle, isImageAttachmentRole+} from "@triliumnext/commons";import { Request, Response } from "../../http_interface";import becca from "../../becca/becca";@@ -87,12 +89,18 @@ function updateAttachment(req: Request<{ attachmentId: string }>) {}const note = attachment.getNote();+ const wasDrawing = attachment.mime === CANVAS_ATTACHMENT_MIME;note.saveRevision();attachment.mime = getAttachedFileMime(file.originalname, file.mimetype.toLowerCase());attachment.setContent(file.buffer, { forceSave: true });- // A shared page links the drawing until an edit saves a picture of the new file.- note.getAttachmentByTitle(getDrawingPictureTitle(req.params.attachmentId))?.markAsDeleted();+ if (wasDrawing) {+ // A shared page links the drawing until an edit saves a picture of the new file.+ const picture = note.getAttachmentByTitle(getDrawingPictureTitle(req.params.attachmentId));+ if (picture && isImageAttachmentRole(picture.role)) {+ picture.markAsDeleted();+ }+ }return {uploaded: true