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(export): answer a single-note export of an image or file note
Elian Doranb0f461fa195f
diff --git a/packages/trilium-core/src/routes/api/export.spec.ts b/packages/trilium-core/src/routes/api/export.spec.tsindex f42e8da5ec..ddef27245a 100644--- a/packages/trilium-core/src/routes/api/export.spec.ts+++ b/packages/trilium-core/src/routes/api/export.spec.ts@@ -72,6 +72,25 @@ describe("Export API (core)", () => {expect(res.headers["Content-Type"]).toBe("text/plain");});+ it("answers a single-note export of an image or file note instead of leaving it hanging", async () => {+ // Regression: exportSingleNote used to *return* a [400, message] tuple, which this route+ // discards (it writes to `res` itself and has no result handler), so the request got no+ // response at all. It now throws, which the catch below turns into a real reply.+ for (const type of ["image", "file"] as const) {+ const created = await api.post<{ branch: { branchId: string } }>(+ "/api/notes/root/children?target=into",+ { body: { title: `Binary ${type}`, type, mime: "application/octet-stream", content: "" } }+ );+ expect(created.status).toBe(200);++ const res = await api.get<string>(`/api/branches/${created.body.branch.branchId}/export/single/html/exportTask`);++ expect(res.status).toBe(500);+ expect(res.headers["Content-Type"]).toBe("text/plain");+ expect(String(res.body)).toContain(`Note type '${type}' cannot be exported as single file.`);+ }+ });+it("exports an unhandled note type as a .dat file instead of a zero-byte .undefined download", async () => {// Regression: note types mapByNoteType doesn't handle (book, webView, mindMap, …) used to fall// through to res.send(undefined), producing a "<title>.undefined", zero-byte download. They nowdiff --git a/packages/trilium-core/src/services/export/single.spec.ts b/packages/trilium-core/src/services/export/single.spec.tsindex 48e513f0d6..41c78078ca 100644--- a/packages/trilium-core/src/services/export/single.spec.ts+++ b/packages/trilium-core/src/services/export/single.spec.ts@@ -234,18 +234,23 @@ describe("exportSingleNote", () => {expect(res.send).toHaveBeenCalledWith("# hi");});- it("returns a 400 tuple for note types and formats it cannot export", () => {- // NOTE: routes/api/export.ts discards this return value and never sends a response of its- // own, so these rejections currently leave the HTTP request unanswered.+ it("throws a ValidationError for note types and formats it cannot export", () => {+ // A returned [status, body] tuple used to be dropped on the floor by the route, leaving the+ // request unanswered; throwing an HttpError is what actually reaches the client.for (const type of ["image", "file"] as const) {- const { result, res } = exportNote(buildNote({ type, title: "Binary" }), "html");-- expect(result).toEqual([400, `Note type '${type}' cannot be exported as single file.`]);- expect(res.send).not.toHaveBeenCalled();+ const note = buildNote({ type, title: "Binary" });++ expect(() => exportNote(note, "html")).toThrow(+ expect.objectContaining({+ name: "ValidationError",+ statusCode: 400,+ message: `Note type '${type}' cannot be exported as single file.`+ })+ );}- const { result, res } = exportNote(buildNote({ type: "text", title: "Texty" }), "pdf");- expect(result).toEqual([400, "Unrecognized format 'pdf'"]);- expect(res.send).not.toHaveBeenCalled();+ expect(() => exportNote(buildNote({ type: "text", title: "Texty" }), "pdf")).toThrow(+ expect.objectContaining({ name: "ValidationError", statusCode: 400, message: "Unrecognized format 'pdf'" })+ );});});diff --git a/packages/trilium-core/src/services/export/single.ts b/packages/trilium-core/src/services/export/single.tsindex b7746fa036..fedd72a957 100644--- a/packages/trilium-core/src/services/export/single.ts+++ b/packages/trilium-core/src/services/export/single.ts@@ -7,6 +7,7 @@ import mimeTypes from "mime-types";import becca from "../../becca/becca.js";import type BBranch from "../../becca/entities/bbranch.js";import type BNote from "../../becca/entities/bnote.js";+import { ValidationError } from "../../errors.js";import type TaskContext from "../task_context.js";import { escapeHtml,getContentDisposition } from "../utils/index.js";import mdService from "./markdown.js";@@ -17,12 +18,16 @@ import { encodeBase64 } from "../utils/binary.js";function exportSingleNote(taskContext: TaskContext<"export">, branch: BBranch, format: ExportFormat, res: Response) {const note = branch.getNote();+ // These are thrown rather than returned: the export route writes straight to `res` and has no+ // result handler, so a returned [status, body] tuple was silently dropped and the request was+ // left unanswered. Throwing reaches the route's catch, which both answers the request and+ // reports the failure to the task context (i.e. to the client's export progress UI).if (note.type === "image" || note.type === "file") {- return [400, `Note type '${note.type}' cannot be exported as single file.`];+ throw new ValidationError(`Note type '${note.type}' cannot be exported as single file.`);}if (format !== "html" && format !== "markdown") {- return [400, `Unrecognized format '${format}'`];+ throw new ValidationError(`Unrecognized format '${format}'`);}const { payload, extension, mime } = mapByNoteType(note, note.getContent(), format);