Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions electron/services/httpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }
}
Expand All @@ -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 }
}
Expand Down