From bb374a5ce21256f7d389f8a5d9a5d67aee38be68 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 29 Dec 2025 16:46:30 +0200 Subject: [PATCH] fix(client/pdf): blob reloaded when saving --- apps/client/src/services/server.ts | 6 +++-- apps/client/src/widgets/type_widgets/File.tsx | 6 ++--- .../src/widgets/type_widgets/file/Pdf.tsx | 7 +++-- apps/server/src/routes/api/files.ts | 27 ++++++++++--------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/apps/client/src/services/server.ts b/apps/client/src/services/server.ts index b13653cf3..381c58a3c 100644 --- a/apps/client/src/services/server.ts +++ b/apps/client/src/services/server.ts @@ -85,13 +85,15 @@ async function remove(url: string, componentId?: string) { return await call("DELETE", url, componentId); } -async function upload(url: string, fileToUpload: File) { +async function upload(url: string, fileToUpload: File, componentId?: string) { const formData = new FormData(); formData.append("upload", fileToUpload); return await $.ajax({ url: window.glob.baseApiUrl + url, - headers: await getHeaders(), + headers: await getHeaders(componentId ? { + "trilium-component-id": componentId + } : undefined), data: formData, type: "PUT", timeout: 60 * 60 * 1000, diff --git a/apps/client/src/widgets/type_widgets/File.tsx b/apps/client/src/widgets/type_widgets/File.tsx index 1d69e3b71..def25cbab 100644 --- a/apps/client/src/widgets/type_widgets/File.tsx +++ b/apps/client/src/widgets/type_widgets/File.tsx @@ -10,13 +10,13 @@ import { TypeWidgetProps } from "./type_widget"; const TEXT_MAX_NUM_CHARS = 5000; -export default function FileTypeWidget({ note }: TypeWidgetProps) { - const blob = useNoteBlob(note); +export default function FileTypeWidget({ note, parentComponent }: TypeWidgetProps) { + const blob = useNoteBlob(note, parentComponent?.componentId); if (blob?.content) { return ; } else if (note.mime === "application/pdf") { - return ; + return ; } else if (note.mime.startsWith("video/")) { return ; } else if (note.mime.startsWith("audio/")) { diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsx index bb18d44f8..5ffb92d34 100644 --- a/apps/client/src/widgets/type_widgets/file/Pdf.tsx +++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx @@ -12,7 +12,10 @@ const VARIABLE_WHITELIST = new Set([ "main-text-color" ]); -export default function PdfPreview({ note }: { note: FNote }) { +export default function PdfPreview({ note, componentId }: { + note: FNote, + componentId: string | undefined; +}) { const iframeRef = useRef(null); const { onLoad } = useStyleInjection(iframeRef); const historyConfig = useViewModeConfig(note, "pdfHistory"); @@ -21,7 +24,7 @@ export default function PdfPreview({ note }: { note: FNote }) { function handleMessage(event: MessageEvent) { if (event.data?.type === "pdfjs-viewer-document-modified" && event.data?.data) { const blob = new Blob([event.data.data], { type: note.mime }); - server.upload(`notes/${note.noteId}/file`, new File([blob], note.title, { type: note.mime })); + server.upload(`notes/${note.noteId}/file`, new File([blob], note.title, { type: note.mime }), componentId); } if (event.data.type === "pdfjs-viewer-save-view-history" && event.data?.data) { diff --git a/apps/server/src/routes/api/files.ts b/apps/server/src/routes/api/files.ts index 7ec05dc6a..5be826fba 100644 --- a/apps/server/src/routes/api/files.ts +++ b/apps/server/src/routes/api/files.ts @@ -1,20 +1,21 @@ -"use strict"; -import protectedSessionService from "../../services/protected_session.js"; -import utils from "../../services/utils.js"; -import log from "../../services/log.js"; -import noteService from "../../services/notes.js"; -import tmp from "tmp"; + +import chokidar from "chokidar"; +import type { Request, Response } from "express"; import fs from "fs"; import { Readable } from "stream"; -import chokidar from "chokidar"; -import ws from "../../services/ws.js"; +import tmp from "tmp"; + import becca from "../../becca/becca.js"; -import ValidationError from "../../errors/validation_error.js"; -import type { Request, Response } from "express"; -import type BNote from "../../becca/entities/bnote.js"; import type BAttachment from "../../becca/entities/battachment.js"; +import type BNote from "../../becca/entities/bnote.js"; +import ValidationError from "../../errors/validation_error.js"; import dataDirs from "../../services/data_dir.js"; +import log from "../../services/log.js"; +import noteService from "../../services/notes.js"; +import protectedSessionService from "../../services/protected_session.js"; +import utils from "../../services/utils.js"; +import ws from "../../services/ws.js"; function updateFile(req: Request) { const note = becca.getNoteOrThrow(req.params.noteId); @@ -189,8 +190,8 @@ function saveToTmpDir(fileName: string, content: string | Buffer, entityType: st chokidar.watch(tmpObj.name).on("change", (path, stats) => { ws.sendMessageToAllClients({ type: "openedFileUpdated", - entityType: entityType, - entityId: entityId, + entityType, + entityId, lastModifiedMs: stats?.atimeMs, filePath: tmpObj.name });