Skip to content

Build your personal knowledge base with Trilium Notes

fix(notes): copy foreign attachments into notes created with content (closes #8314)

Elian Doran544859ca9a17
diff --git a/packages/trilium-core/src/routes/api/notes.spec.ts b/packages/trilium-core/src/routes/api/notes.spec.ts
index 0065414bc7..b8780a1fda 100644
--- a/packages/trilium-core/src/routes/api/notes.spec.ts
+++ b/packages/trilium-core/src/routes/api/notes.spec.ts
@@ -74,6 +74,45 @@ describe("Notes API (core)", () => {
expect(res.body.branch.parentNoteId).toBe("root");
});
+ // What "cut selection into sub-note" does: the new note is handed HTML carrying the source
+ // note's pictures, and the source then drops them from its own content, scheduling those
+ // attachments for erasure. The new note has to end up owning copies, not pointing at them.
+ it("copies a foreign attachment referenced by the content it is created with", async () => {
+ const source = await createTextNote(api, { content: "<p>source</p>" });
+ const save = await api.post(`/api/notes/${source.noteId}/attachments`, {
+ body: { role: "image", mime: "image/png", title: "picture.png", content: "picture bytes" }
+ });
+ expect(save.status).toBe(204);
+
+ const [ original ] = (await api.get<{ attachmentId: string }[]>(
+ `/api/notes/${source.noteId}/attachments`
+ )).body;
+
+ const created = await api.post<{ note: { noteId: string } }>(
+ "/api/notes/root/children?target=into",
+ {
+ body: {
+ title: "Cut selection",
+ type: "text",
+ content: `<p><img src="api/attachments/${original.attachmentId}/image/picture.png"></p>`
+ }
+ }
+ );
+ expect(created.status).toBe(200);
+ const { noteId } = created.body.note;
+
+ const copies = await api.get<{ attachmentId: string; ownerId: string; title: string }[]>(
+ `/api/notes/${noteId}/attachments`
+ );
+ expect(copies.body).toHaveLength(1);
+ expect(copies.body[0].ownerId).toBe(noteId);
+ expect(copies.body[0].attachmentId).not.toBe(original.attachmentId);
+
+ const blob = await api.get<{ content: string }>(`/api/notes/${noteId}/blob`);
+ expect(blob.body.content).toContain(`api/attachments/${copies.body[0].attachmentId}/image/`);
+ expect(blob.body.content).not.toContain(original.attachmentId);
+ });
+
it("400s when the target query param is invalid", async () => {
const res = await api.post("/api/notes/root/children", {
body: { title: "no target", type: "text" }
diff --git a/packages/trilium-core/src/routes/api/notes.ts b/packages/trilium-core/src/routes/api/notes.ts
index 429ec7da0f..a8ab279325 100644
--- a/packages/trilium-core/src/routes/api/notes.ts
+++ b/packages/trilium-core/src/routes/api/notes.ts
@@ -121,6 +121,18 @@ function createNote(req: Request) {
const { note, branch } = noteService.createNewNoteWithTarget(target, String(targetBranchId), params);
+ // Content handed to us at creation gets the same pass a save gives it — `createNewNote`
+ // deliberately does none of it itself, so every caller taking content from outside runs it (see
+ // `import/single.ts`). Without it, content that came from another note keeps pointing at that
+ // note's attachments: "cut selection into sub-note" hands the new note the selected HTML,
+ // picture URLs and all, and then removes the selection from the source, which schedules the very
+ // attachments the sub-note shows for erasure. The copying step lives here, in the same
+ // transaction (`scanForLinks` runs before this promise's first await), so the note is right
+ // before the response leaves.
+ if (typeof params.content === "string" && params.content) {
+ void noteService.asyncPostProcessContent(note, params.content);
+ }
+
return {
note,
branch