Skip to content

Build your personal knowledge base with Trilium Notes

fix(client/pdf): blob reloaded when saving

Elian Doranbb374a5ce212
diff --git a/apps/client/src/services/server.ts b/apps/client/src/services/server.ts
index b13653cf3e..381c58a3cf 100644
--- a/apps/client/src/services/server.ts
+++ b/apps/client/src/services/server.ts
@@ -85,13 +85,15 @@ async function remove<T>(url: string, componentId?: string) {
return await call<T>("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 1d69e3b71d..def25cbab3 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 <TextPreview content={blob.content} />;
} else if (note.mime === "application/pdf") {
- return <PdfPreview note={note} />;
+ return <PdfPreview note={note} componentId={parentComponent?.componentId} />;
} else if (note.mime.startsWith("video/")) {
return <VideoPreview note={note} />;
} 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 bb18d44f8d..5ffb92d34a 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<HTMLIFrameElement>(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 7ec05dc6a4..5be826fba6 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
});