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(server): custom route depending on helper function
Elian Dorane77894271130
diff --git a/apps/server/src/routes/custom.ts b/apps/server/src/routes/custom.tsindex 46ee1b1888..31ea0658ee 100644--- a/apps/server/src/routes/custom.ts+++ b/apps/server/src/routes/custom.ts@@ -1,4 +1,4 @@-import { utils } from "@triliumnext/core";+import { routeHelpers,utils } from "@triliumnext/core";import type { Request, Response, Router } from "express";import becca from "../becca/becca.js";@@ -7,7 +7,6 @@ import cls from "../services/cls.js";import log from "../services/log.js";import scriptService from "../services/script.js";import sql from "../services/sql.js";-import fileService from "./api/files.js";function handleRequest(req: Request, res: Response) {@@ -80,7 +79,7 @@ function handleRequest(req: Request, res: Response) {res.setHeader("Content-Type", "text/plain").status(500).send(errMessage);}} else if (attr.name === "customResourceProvider") {- fileService.downloadNoteInt(attr.noteId, res);+ routeHelpers.downloadNoteInt(attr.noteId, res);} else {throw new Error(`Unrecognized attribute name '${attr.name}'`);}diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.tsindex 364c309704..c1ad909d61 100644--- a/apps/server/src/routes/routes.ts+++ b/apps/server/src/routes/routes.ts@@ -118,9 +118,7 @@ function register(app: express.Application) {}}));- route(GET, "/api/attachments/:attachmentId/download", [auth.checkApiAuthOrElectron], filesRoute.downloadAttachment);- // this "hacky" path is used for easier referencing of CSS resources- route(GET, "/api/attachments/download/:attachmentId", [auth.checkApiAuthOrElectron], filesRoute.downloadAttachment);+apiRoute(PST, "/api/attachments/:attachmentId/save-to-tmp-dir", filesRoute.saveAttachmentToTmpDir);apiRoute(PST, "/api/attachments/:attachmentId/upload-modified-file", filesRoute.uploadModifiedFileToAttachment);route(PUT, "/api/attachments/:attachmentId/file", [auth.checkApiAuthOrElectron, uploadMiddlewareWithErrorHandling, csrfMiddleware], filesRoute.updateAttachment, apiResultHandler);diff --git a/packages/trilium-core/src/index.ts b/packages/trilium-core/src/index.tsindex 3f51dbc451..df899145e8 100644--- a/packages/trilium-core/src/index.ts+++ b/packages/trilium-core/src/index.ts@@ -97,6 +97,7 @@ export { getPlatform, type PlatformProvider } from "./services/platform";export { t } from "i18next";export type { RequestProvider, ExecOpts, CookieJar } from "./services/request";export type * from "./meta";+export * as routeHelpers from "./routes/helpers";export * as becca_easy_mocking from "./test/becca_easy_mocking";export * as becca_mocking from "./test/becca_mocking";diff --git a/packages/trilium-core/src/routes/api/files.ts b/packages/trilium-core/src/routes/api/files.tsindex cd116b886f..397579217d 100644--- a/packages/trilium-core/src/routes/api/files.ts+++ b/packages/trilium-core/src/routes/api/files.ts@@ -1,9 +1,6 @@import { Request, Response } from "express";import becca from "../../becca/becca";-import BAttachment from "../../becca/entities/battachment";-import BNote from "../../becca/entities/bnote";-import protected_session from "../../services/protected_session";-import { utils } from "../..";+import { downloadData, downloadNoteInt } from "../helpers";const downloadFile = (req: Request<{ noteId: string }>, res: Response) => downloadNoteInt(req.params.noteId, res, true);const openFile = (req: Request<{ noteId: string }>, res: Response) => downloadNoteInt(req.params.noteId, res, false);@@ -11,16 +8,6 @@ const openFile = (req: Request<{ noteId: string }>, res: Response) => downloadNoconst downloadAttachment = (req: Request<{ attachmentId: string }>, res: Response) => downloadAttachmentInt(req.params.attachmentId, res, true);const openAttachment = (req: Request<{ attachmentId: string }>, res: Response) => downloadAttachmentInt(req.params.attachmentId, res, false);-function downloadNoteInt(noteId: string, res: Response, contentDisposition = true) {- const note = becca.getNote(noteId);-- if (!note) {- return res.setHeader("Content-Type", "text/plain").status(404).send(`Note '${noteId}' doesn't exist.`);- }-- return downloadData(note, res, contentDisposition);-}-function downloadAttachmentInt(attachmentId: string, res: Response, contentDisposition = true) {const attachment = becca.getAttachment(attachmentId);@@ -31,27 +18,9 @@ function downloadAttachmentInt(attachmentId: string, res: Response, contentDisporeturn downloadData(attachment, res, contentDisposition);}-function downloadData(noteOrAttachment: BNote | BAttachment, res: Response, contentDisposition: boolean) {- if (noteOrAttachment.isProtected && !protected_session.isProtectedSessionAvailable()) {- return res.status(401).send("Protected session not available");- }-- if (contentDisposition) {- const fileName = noteOrAttachment.getFileName();-- res.setHeader("Content-Disposition", utils.getContentDisposition(fileName));- }-- res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");- res.setHeader("Content-Type", noteOrAttachment.mime);-- res.send(noteOrAttachment.getContent());-}-export default {openFile,downloadFile,- downloadNoteInt,openAttachment,downloadAttachment,}diff --git a/packages/trilium-core/src/routes/helpers.ts b/packages/trilium-core/src/routes/helpers.tsnew file mode 100644index 0000000000..7d17678602--- /dev/null+++ b/packages/trilium-core/src/routes/helpers.ts@@ -0,0 +1,33 @@+import { Response } from "express";+import becca from "../becca/becca";+import BNote from "../becca/entities/bnote";+import protected_session from "../services/protected_session";+import BAttachment from "../becca/entities/battachment";+import { utils } from "..";++export function downloadNoteInt(noteId: string, res: Response, contentDisposition = true) {+ const note = becca.getNote(noteId);++ if (!note) {+ return res.setHeader("Content-Type", "text/plain").status(404).send(`Note '${noteId}' doesn't exist.`);+ }++ return downloadData(note, res, contentDisposition);+}++export function downloadData(noteOrAttachment: BNote | BAttachment, res: Response, contentDisposition: boolean) {+ if (noteOrAttachment.isProtected && !protected_session.isProtectedSessionAvailable()) {+ return res.status(401).send("Protected session not available");+ }++ if (contentDisposition) {+ const fileName = noteOrAttachment.getFileName();++ res.setHeader("Content-Disposition", utils.getContentDisposition(fileName));+ }++ res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");+ res.setHeader("Content-Type", noteOrAttachment.mime);++ res.send(noteOrAttachment.getContent());+}diff --git a/packages/trilium-core/src/routes/index.ts b/packages/trilium-core/src/routes/index.tsindex b016a40172..7f2b526f92 100644--- a/packages/trilium-core/src/routes/index.ts+++ b/packages/trilium-core/src/routes/index.ts@@ -198,6 +198,9 @@ export function buildSharedApiRoutes({ route, asyncRoute, apiRoute, asyncApiRout// this "hacky" path is used for easier referencing of CSS resourcesroute(GET, "/api/notes/download/:noteId", [checkApiAuthOrElectron], filesRoute.downloadFile);route(GET, "/api/attachments/:attachmentId/open", [checkApiAuthOrElectron], filesRoute.openAttachment);+ route(GET, "/api/attachments/:attachmentId/download", [checkApiAuthOrElectron], filesRoute.downloadAttachment);+ // this "hacky" path is used for easier referencing of CSS resources+ route(GET, "/api/attachments/download/:attachmentId", [checkApiAuthOrElectron], filesRoute.downloadAttachment);//#endregion}