Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): show when a watched file was last modified

Elian Doran7eb3054598c7
diff --git a/apps/server/src/routes/api/files.spec.ts b/apps/server/src/routes/api/files.spec.ts
index 96d9792fee..1ec65557da 100644
--- a/apps/server/src/routes/api/files.spec.ts
+++ b/apps/server/src/routes/api/files.spec.ts
@@ -1,13 +1,52 @@
import { CANVAS_ATTACHMENT_MIME } from "@triliumnext/commons";
-import { becca, cls, note_service as noteService, ValidationError } from "@triliumnext/core";
+import { becca, cls, note_service as noteService, ValidationError, ws } from "@triliumnext/core";
import type { Request } from "express";
import fs from "fs";
import path from "path";
-import { beforeAll, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import dataDirs from "../../services/data_dir.js";
import filesRoute from "./files.js";
+interface FakeWatcher {
+ path: string;
+ options: { alwaysStat?: boolean };
+ listeners: Map<string, (...args: unknown[]) => void>;
+}
+
+const h = vi.hoisted(() => ({
+ electron: false,
+ watchers: [] as FakeWatcher[]
+}));
+
+vi.mock("chokidar", () => ({
+ default: {
+ watch: (watchedPath: string, options: FakeWatcher["options"]) => {
+ const watcher: FakeWatcher = { path: watchedPath, options, listeners: new Map() };
+ h.watchers.push(watcher);
+ const api = {
+ on(event: string, listener: (...args: unknown[]) => void) {
+ watcher.listeners.set(event, listener);
+ return api;
+ }
+ };
+ return api;
+ }
+ }
+}));
+
+vi.mock("../../services/utils.js", async (importOriginal) => {
+ const actual = await importOriginal<{ default: object }>();
+ return {
+ default: {
+ ...actual.default,
+ get isElectron() {
+ return h.electron;
+ }
+ }
+ };
+});
+
let noteId: string;
let attachmentId: string;
@@ -113,4 +152,38 @@ describe("Files API", () => {
}
});
});
+
+ describe("watching the temp file under Electron", () => {
+ let watchedNoteId: string;
+
+ beforeEach(() => {
+ h.electron = true;
+ h.watchers.length = 0;
+ watchedNoteId = cls.init(() => noteService.createNewNote({
+ parentNoteId: "root", title: "Watched note", type: "file", mime: "text/plain", content: "watched"
+ })).note.noteId;
+ });
+
+ afterEach(() => {
+ h.electron = false;
+ });
+
+ const save = () => cls.init(() => filesRoute.saveNoteToTmpDir(fileReq({ noteId: watchedNoteId }))).tmpFilePath;
+
+ it("reports the modification time of a changed file", () => {
+ const tmpFilePath = save();
+ const [ watcher ] = h.watchers;
+ expect(watcher.options?.alwaysStat).toBe(true);
+
+ const send = vi.spyOn(ws, "sendMessageToAllClients").mockImplementation(() => {});
+ try {
+ watcher.listeners.get("change")?.(tmpFilePath, { mtimeMs: 5000, atimeMs: 9000 });
+ expect(send).toHaveBeenCalledWith(expect.objectContaining({
+ type: "openedFileUpdated", entityId: watchedNoteId, lastModifiedMs: 5000, filePath: tmpFilePath
+ }));
+ } finally {
+ send.mockRestore();
+ }
+ });
+ });
});
diff --git a/apps/server/src/routes/api/files.ts b/apps/server/src/routes/api/files.ts
index 8ecfcffefc..9fbe7d4cd4 100644
--- a/apps/server/src/routes/api/files.ts
+++ b/apps/server/src/routes/api/files.ts
@@ -43,12 +43,12 @@ function saveToTmpDir(fileName: string, content: string | Uint8Array, entityType
getLog().info(`Saved temporary file ${tmpFilePath}`);
if (utils.isElectron) {
- chokidar.watch(tmpFilePath).on("change", (path, stats) => {
+ chokidar.watch(tmpFilePath, { alwaysStat: true }).on("change", (path, stats) => {
ws.sendMessageToAllClients({
type: "openedFileUpdated",
entityType,
entityId,
- lastModifiedMs: stats?.atimeMs,
+ lastModifiedMs: stats?.mtimeMs,
filePath: tmpFilePath
});
});