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(desktop): reopen a renamed note with its new extension
Elian Doran3ad34efab8c2
diff --git a/apps/server/src/routes/api/files.spec.ts b/apps/server/src/routes/api/files.spec.tsindex 6dc1dabb2c..bdda0bb2c3 100644--- a/apps/server/src/routes/api/files.spec.ts+++ b/apps/server/src/routes/api/files.spec.ts@@ -183,6 +183,18 @@ describe("Files API", () => {expect(h.watchers.map((watcher) => watcher.path)).toEqual([ first, second ]);});+ it("starts a new temp file when the note's file name changes", () => {+ const first = save();+ cls.init(() => {+ const note = becca.getNoteOrThrow(watchedNoteId);+ note.title = "Renamed note";+ note.save();+ });+ const second = save();+ expect(second).not.toBe(first);+ expect(path.basename(second)).toMatch(/-Renamed note\.txt$/);+ });+it("reports the modification time of a changed file", () => {const tmpFilePath = save();const [ watcher ] = h.watchers;diff --git a/apps/server/src/routes/api/files.ts b/apps/server/src/routes/api/files.tsindex 14ca72730b..998ca65d88 100644--- a/apps/server/src/routes/api/files.ts+++ b/apps/server/src/routes/api/files.ts@@ -30,14 +30,15 @@ function saveAttachmentToTmpDir(req: Request<{ attachmentId: string }>) {const createdTemporaryFiles = new Set<string>();/** The latest temp file of each entity, keyed by `entityType/entityId`. */-const latestTemporaryFiles = new Map<string, string>();+const latestTemporaryFiles = new Map<string, { tmpFilePath: string; fileName: string }>();function saveToTmpDir(fileName: string, content: string | Uint8Array, entityType: string, entityId: string) {- // Opening an entity again reuses its temp file and watcher, unless the file holds edits that are not uploaded yet.+ // Opening an entity again reuses its temp file and watcher, unless the file holds edits that are not uploaded yet+ // or the entity's file name, and so its extension, has changed.const entityKey = `${entityType}/${entityId}`;const latest = latestTemporaryFiles.get(entityKey);- if (latest && holdsContent(latest, content)) {- return { tmpFilePath: latest };+ if (latest && latest.fileName === fileName && holdsContent(latest.tmpFilePath, content)) {+ return { tmpFilePath: latest.tmpFilePath };}const tmpFileName = `tmp-${process.pid}-${utils.randomString(12)}-${fileName}`;@@ -48,7 +49,7 @@ function saveToTmpDir(fileName: string, content: string | Uint8Array, entityTypeprocess.once("exit", removeTemporaryFiles);}createdTemporaryFiles.add(tmpFilePath);- latestTemporaryFiles.set(entityKey, tmpFilePath);+ latestTemporaryFiles.set(entityKey, { tmpFilePath, fileName });getLog().info(`Saved temporary file ${tmpFilePath}`);