AudioNoteStore.swift 46 lignes · 1624 octets
import Foundation

/// Stockage stable des fichiers WAV pour les notes vocales (interactions et notes de projet).
/// Analogue à `AudioNoteStore` Android : les fichiers sont rangés sous `Application Support/audio/`
/// (pas dans un dossier temporaire purgeable), identifiés par le `localId` de l'entité.
struct AudioNoteStore {
    let rootDirectory: URL

    /// Initialise le store sur un dossier existant ou à créer.
    init(rootDirectory: URL) {
        self.rootDirectory = rootDirectory
    }

    /// Store par défaut sous `Application Support/audio/`.
    static func defaultStore() throws -> AudioNoteStore {
        let appSupport = try FileManager.default.url(
            for: .applicationSupportDirectory,
            in: .userDomainMask,
            appropriateFor: nil,
            create: true
        )
        let dir = appSupport.appendingPathComponent("audio", isDirectory: true)
        try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
        return AudioNoteStore(rootDirectory: dir)
    }

    // MARK: - Chemins stables

    func pathForInteraction(localId: Int64) -> String {
        rootDirectory.appendingPathComponent("interaction_\(localId).wav").path
    }

    func pathForNote(localId: Int64) -> String {
        rootDirectory.appendingPathComponent("note_\(localId).wav").path
    }

    // MARK: - Lecture / suppression

    func readBytes(atPath path: String) -> Data? {
        try? Data(contentsOf: URL(fileURLWithPath: path))
    }

    func deleteFile(atPath path: String) {
        try? FileManager.default.removeItem(atPath: path)
    }
}