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
feat(spreadsheet): detect orphaned image attachments on save
Elian Doran3ef588c847d6
diff --git a/apps/server/src/services/notes.spec.ts b/apps/server/src/services/notes.spec.tsindex 17e15d5d70..08b1708114 100644--- a/apps/server/src/services/notes.spec.ts+++ b/apps/server/src/services/notes.spec.ts@@ -204,6 +204,66 @@ describe("checkImageAttachments", () => {});});+ describe("Spreadsheet content", () => {+ /** Wraps a drawing source URL into the JSON shape a spreadsheet note persists. */+ function spreadsheetContent(source: string) {+ return JSON.stringify({+ version: 1,+ workbook: {+ resources: [{+ name: "SHEET_DRAWING_PLUGIN",+ data: JSON.stringify({ "sheet-1": { data: { img1: { imageSourceType: "URL", source } }, order: ["img1"] } })+ }]+ }+ });+ }++ it("keeps an attachment referenced by the workbook drawing source alive", () => {+ const note = buildNote({ title: "Sheet", type: "spreadsheet", mime: "application/json", attachments: [{ title: "image.png", role: "image", mime: "image/png" }] });+ mockAttachmentSaves(note);+ const [att] = note.getAttachments();++ checkImageAttachments(note, spreadsheetContent(`api/attachments/${att.attachmentId}/image/image.png`));++ expect(att.save).not.toHaveBeenCalled();+ });++ it("schedules an inserted-then-removed image for erasure", () => {+ const note = buildNote({ title: "Sheet", type: "spreadsheet", mime: "application/json", attachments: [{ title: "image.png", role: "image", mime: "image/png" }] });+ mockAttachmentSaves(note);+ const [att] = note.getAttachments();++ checkImageAttachments(note, spreadsheetContent("api/attachments/someOtherId/image/image.png"));++ expect(att.save).toHaveBeenCalled();+ expect(att.utcDateScheduledForErasureSince).toBeTruthy();+ });++ it("never schedules the preview thumbnail for erasure even though it is unreferenced", () => {+ const note = buildNote({ title: "Sheet", type: "spreadsheet", mime: "application/json", attachments: [{ title: "spreadsheet-export.png", role: "image", mime: "image/png" }] });+ mockAttachmentSaves(note);+ const [thumbnail] = note.getAttachments();++ // Content with no drawing images at all — the thumbnail is the only "image" attachment.+ checkImageAttachments(note, JSON.stringify({ version: 1, workbook: { resources: [] } }));++ expect(thumbnail.save).not.toHaveBeenCalled();+ expect(thumbnail.utcDateScheduledForErasureSince).toBeFalsy();+ });++ it("cancels erasure when the image is re-referenced", () => {+ const note = buildNote({ title: "Sheet", type: "spreadsheet", mime: "application/json", attachments: [{ title: "image.png", role: "image", mime: "image/png" }] });+ mockAttachmentSaves(note);+ const [att] = note.getAttachments();+ att.utcDateScheduledForErasureSince = "2025-01-01 00:00:00.000Z";++ checkImageAttachments(note, spreadsheetContent(`api/attachments/${att.attachmentId}/image/image.png`));++ expect(att.save).toHaveBeenCalled();+ expect(att.utcDateScheduledForErasureSince).toBeNull();+ });+ });+describe("foreign attachment copying", () => {it("replaces foreign attachment IDs in HTML content", () => {const note = buildNote({ title: "Test" });diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.tsindex 4f4fbb008f..f760e8a3b8 100644--- a/packages/trilium-core/src/services/notes.ts+++ b/packages/trilium-core/src/services/notes.ts@@ -416,11 +416,20 @@ function protectNote(note: BNote, protect: boolean) {}}+/**+ * Title of the spreadsheet preview image. Unlike the inline drawing images, this attachment is not+ * referenced from the note content — it is the note's rendered thumbnail, looked up by title by the+ * image endpoint — so it must be exempt from the orphan-erasure scheduling below.+ */+const SPREADSHEET_PREVIEW_ATTACHMENT_TITLE = "spreadsheet-export.png";+export function checkImageAttachments(note: BNote, content: string) {const foundAttachmentIds = new Set<string>();let match;- const patterns = note.isMarkdown()+ // Spreadsheet content is JSON storing inline images as bare `api/attachments/{id}/image/...`+ // URLs (no `src="..."` wrapper), so it scans with the same loose pattern as Markdown.+ const patterns = (note.isMarkdown() || note.type === "spreadsheet")? [//  or similar markdown image syntax/api\/attachments\/([a-zA-Z0-9_]+)\/image/g,@@ -450,6 +459,12 @@ export function checkImageAttachments(note: BNote, content: string) {continue;}+ // The spreadsheet preview thumbnail is never referenced from the content, so leave it alone+ // (otherwise it would be scheduled for erasure on every save).+ if (note.type === "spreadsheet" && attachment.title === SPREADSHEET_PREVIEW_ATTACHMENT_TITLE) {+ continue;+ }+const attachmentInContent = attachment.attachmentId && foundAttachmentIds.has(attachment.attachmentId);if (attachment.utcDateScheduledForErasureSince && attachmentInContent) {@@ -892,7 +907,7 @@ function saveAttachments(note: BNote, content: string) {export function saveLinks(note: BNote, content: string | Uint8Array) {- if ((note.type !== "text" && note.type !== "relationMap" && note.type !== "llmChat" && !note.isMarkdown()) || (note.isProtected && !protectedSessionService.isProtectedSessionAvailable())) {+ if ((note.type !== "text" && note.type !== "relationMap" && note.type !== "llmChat" && note.type !== "spreadsheet" && !note.isMarkdown()) || (note.isProtected && !protectedSessionService.isProtectedSessionAvailable())) {return {forceFrontendReload: false,content@@ -916,6 +931,11 @@ export function saveLinks(note: BNote, content: string | Uint8Array) {findMarkdownImageLinks(content, foundLinks);findMarkdownInternalLinks(content, foundLinks);({ forceFrontendReload, content } = checkImageAttachments(note, content));+ } else if (note.type === "spreadsheet" && typeof content === "string") {+ // Spreadsheet images are stored as attachments referenced from the workbook JSON; scan for+ // orphans (inserted-then-removed images) so they get scheduled for erasure. There are no+ // Trilium internal links to extract from spreadsheet content.+ ({ forceFrontendReload, content } = checkImageAttachments(note, content));} else if (note.type === "relationMap" && typeof content === "string") {findRelationMapLinks(content, foundLinks);} else if (note.type === "llmChat" && typeof content === "string") {