From 4ecf406660f64cc8a84de030310ed53ee88baa81 Mon Sep 17 00:00:00 2001 From: Administrator Date: Sat, 21 Mar 2026 13:31:54 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20folder=20monitor=20re-det?= =?UTF-8?q?ect=20deleted=20files,=20atomic=20sync=20save?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Folder monitor: clear _seenFiles entry on file unlink so re-added files (e.g. re-encoded) are detected again - Sync IPC save (beforeunload): use atomic write pattern with backup (.bak) creation, matching the async _atomicWrite behavior Co-Authored-By: Claude Opus 4.6 (1M context) --- lib/folder-monitor.js | 5 +++++ main.js | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/folder-monitor.js b/lib/folder-monitor.js index 6fde2ca..1c6914f 100644 --- a/lib/folder-monitor.js +++ b/lib/folder-monitor.js @@ -35,6 +35,11 @@ class FolderMonitor extends EventEmitter { this._watcher = chokidar.watch(folderPath, watchOptions); this._watcher.on('add', (filePath) => this._onNewFile(filePath)); + this._watcher.on('unlink', (filePath) => { + // Allow re-added files (e.g. re-encoded) to be detected again + const normalized = filePath.replace(/\\/g, '/').toLowerCase(); + this._seenFiles.delete(normalized); + }); this._watcher.on('error', (err) => this.emit('error', err)); } diff --git a/main.js b/main.js index cef8d0c..8f5c539 100644 --- a/main.js +++ b/main.js @@ -883,11 +883,22 @@ ipcMain.handle('save-global-settings', async (_event, globalSettings) => { }); // Synchronous save for beforeunload — blocks renderer until write completes +// Uses atomic write pattern (tmp + backup + rename) to prevent corruption ipcMain.on('save-global-settings-sync', (event, globalSettings) => { try { const current = configStore.load(); current.globalSettings = globalSettings; - fs.writeFileSync(configStore.filePath, JSON.stringify(current, null, 2)); + const data = JSON.stringify(current, null, 2); + const tmpPath = configStore.filePath + '.tmp'; + const backupPath = configStore.filePath + '.bak'; + fs.writeFileSync(tmpPath, data, 'utf-8'); + if (fs.existsSync(configStore.filePath)) { + const existing = fs.readFileSync(configStore.filePath, 'utf-8'); + if (existing && existing.trim().length > 2) { + fs.writeFileSync(backupPath, existing, 'utf-8'); + } + } + fs.renameSync(tmpPath, configStore.filePath); } catch {} event.returnValue = true; });