diff --git a/electron/services/httpService.ts b/electron/services/httpService.ts index 1e9b014..c72f811 100644 --- a/electron/services/httpService.ts +++ b/electron/services/httpService.ts @@ -1417,6 +1417,20 @@ class HttpService { return '.jpg' } + private writeFileIfLarger(fullPath: string, data: Buffer): void { + if (fs.existsSync(fullPath)) { + try { + const stat = fs.statSync(fullPath) + if (!stat.isFile()) return + if (data.length <= stat.size) return + } catch { + // If the existing export cannot be inspected, overwrite it below. + } + } + + fs.writeFileSync(fullPath, data) + } + private async exportMediaForMessages( messages: Message[], talker: string, @@ -1516,9 +1530,7 @@ class HttpService { const targetDir = path.join(sessionDir, 'images') const fullPath = path.join(targetDir, fileName) this.ensureDir(targetDir) - if (!fs.existsSync(fullPath)) { - fs.writeFileSync(fullPath, imageBuffer) - } + this.writeFileIfLarger(fullPath, imageBuffer) const relativePath = `${this.sanitizeFileName(talker, 'session')}/images/${fileName}` return { kind: 'image', fileName, fullPath, relativePath } } @@ -1531,9 +1543,7 @@ class HttpService { const targetDir = path.join(sessionDir, 'images') const fullPath = path.join(targetDir, fileName) this.ensureDir(targetDir) - if (!fs.existsSync(fullPath)) { - fs.copyFileSync(imagePath, fullPath) - } + this.writeFileIfLarger(fullPath, imageBuffer) const relativePath = `${this.sanitizeFileName(talker, 'session')}/images/${fileName}` return { kind: 'image', fileName, fullPath, relativePath } }