Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

fix: prune missing audio safely

altic-dev1c7e29a48ae4
diff --git a/Sources/Fluid/Persistence/DictationAudioHistoryStore.swift b/Sources/Fluid/Persistence/DictationAudioHistoryStore.swift
index ec411bec..dfe747f5 100644
--- a/Sources/Fluid/Persistence/DictationAudioHistoryStore.swift
+++ b/Sources/Fluid/Persistence/DictationAudioHistoryStore.swift
@@ -82,11 +82,51 @@ final nonisolated class DictationAudioHistoryStore: @unchecked Sendable {
return self.fileManager.fileExists(atPath: url.path)
}
- func deleteAudio(fileName: String) {
+ @discardableResult
+ func deleteAudio(fileName: String) -> Int64 {
guard let url = try? self.audioDirectory(createIfNeeded: false).appendingPathComponent(fileName, isDirectory: false) else {
- return
+ return 0
+ }
+
+ return self.deleteAudioFile(at: url) ?? 0
+ }
+
+ func deleteUnreferencedAudioFiles(referencedFileNames: Set<String>) -> (fileCount: Int, byteCount: Int64) {
+ guard let directory = try? self.audioDirectory(createIfNeeded: false),
+ self.fileManager.fileExists(atPath: directory.path)
+ else {
+ return (0, 0)
+ }
+
+ let files = (try? self.fileManager.contentsOfDirectory(
+ at: directory,
+ includingPropertiesForKeys: [.fileSizeKey],
+ options: [.skipsHiddenFiles]
+ )) ?? []
+
+ var fileCount = 0
+ var byteCount: Int64 = 0
+ for file in files where file.pathExtension.lowercased() == "wav" && !referencedFileNames.contains(file.lastPathComponent) {
+ guard let removedBytes = self.deleteAudioFile(at: file) else { continue }
+ fileCount += 1
+ byteCount += removedBytes
+ }
+
+ return (fileCount, byteCount)
+ }
+
+ private func deleteAudioFile(at url: URL) -> Int64? {
+ guard self.fileManager.fileExists(atPath: url.path) else {
+ return nil
+ }
+
+ let fileSize = (try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize).map(Int64.init) ?? 0
+ do {
+ try self.fileManager.removeItem(at: url)
+ return fileSize
+ } catch {
+ return nil
}
- try? self.fileManager.removeItem(at: url)
}
func deleteAllAudioFiles() {
diff --git a/Sources/Fluid/Persistence/TranscriptionHistoryStore.swift b/Sources/Fluid/Persistence/TranscriptionHistoryStore.swift
index f2e101e3..be68446d 100644
--- a/Sources/Fluid/Persistence/TranscriptionHistoryStore.swift
+++ b/Sources/Fluid/Persistence/TranscriptionHistoryStore.swift
@@ -309,11 +309,19 @@ final class TranscriptionHistoryStore: ObservableObject {
guard currentBytes > budgetBytes else { return 0 }
var updatedEntries = self.entries
+ let referencedFileNames = Set(updatedEntries.compactMap { $0.audio?.fileName })
+ let orphanedAudio = DictationAudioHistoryStore.shared.deleteUnreferencedAudioFiles(referencedFileNames: referencedFileNames)
+ if orphanedAudio.fileCount > 0 {
+ currentBytes = max(0, currentBytes - orphanedAudio.byteCount)
+ DebugLogger.shared.info("Pruned orphaned dictation audio (\(orphanedAudio.fileCount) files)", source: "TranscriptionHistoryStore")
+ }
+ guard currentBytes > budgetBytes else { return 0 }
+
var prunedCount = 0
for index in updatedEntries.indices.reversed() {
guard let audio = updatedEntries[index].audio else { continue }
- DictationAudioHistoryStore.shared.deleteAudio(fileName: audio.fileName)
- currentBytes -= Int64(audio.byteCount)
+ let removedBytes = DictationAudioHistoryStore.shared.deleteAudio(fileName: audio.fileName)
+ currentBytes = max(0, currentBytes - removedBytes)
updatedEntries[index] = updatedEntries[index].replacingAudio(nil)
prunedCount += 1
if currentBytes <= budgetBytes {