refactor(tts): reuse the shared atomic write helper for the audio cache

This commit is contained in:
Chris Scott committed 2026-10-11 00:14:14 -04:00
1 parent 169ee25a8a
commit bda123b6e1
4 files changed
+57 -70

No files matched your search

+4 -12
View File
@@ -1,12 +1,12 @@
import { Hono } from 'hono'
import { z } from 'zod'
import { Database } from 'bun:sqlite'
import { createHash, randomUUID } from 'crypto'
import { readFile, writeFile, readdir, stat, unlink, rename } from 'fs/promises'
import { createHash } from 'crypto'
import { readFile, readdir, stat, unlink } from 'fs/promises'
import { join } from 'path'
import { SettingsService } from '../services/settings'
import { logger } from '../utils/logger'
import { mkdirSafe } from '../utils/fs-safe'
import { mkdirSafe, writeFileAtomic } from '../utils/fs-safe'
import { getWorkspacePath } from '@opencode-manager/shared/config/env'
import {
normalizeToBaseUrl,
@@ -124,15 +124,7 @@ async function cacheAudio(cacheKey: string, audioData: Buffer): Promise<void> {
await cleanupOldestFiles(audioData.length)
}
const tempPath = join(TTS_CACHE_DIR, `${cacheKey}.${process.pid}.${randomUUID()}.tmp`)
try {
await writeFile(tempPath, audioData)
await rename(tempPath, filePath)
} catch (error) {
await unlink(tempPath).catch(() => undefined)
throw error
}
await writeFileAtomic(filePath, audioData)
}
class TTSUpstreamError extends Error {
+1 -1
View File
@@ -26,7 +26,7 @@ export function canonicalPathSync(target: string): string {
}
}
export async function writeFileAtomic(filePath: string, content: string, options: { mode?: number } = {}): Promise<void> {
export async function writeFileAtomic(filePath: string, content: string | Buffer, options: { mode?: number } = {}): Promise<void> {
const dir = path.dirname(filePath)
await mkdirSafe(dir)
const tempPath = path.join(dir, `.${path.basename(filePath)}.ocm-tmp-${process.pid}-${Date.now()}`)