// Where audio waits between the microphone and the meeting row. // // A meeting's bytes are written by this app, copied by the crate, and then // deleted by this app. In between they live HERE: one directory under the App // Group container, a sub-directory per act, named by an id this side minted — // so a recovery marker and the bytes it names can always be matched up, and so // discarding one act never reaches another's. // // It sits beside — never inside — the layout Rust builds in the same container // (`cotton-client/src/layout.rs` owns `client`, `media`, `identity`, `files`, // `cache`, `backup`, `tls` and the four top-level files), which is the same // rule `ShareChannel.inboxDirectoryName` states for the share extension's // copies. Nothing below the boundary knows this name. // // **Imports stage here too**, although the directory is called `recordings`. // A file picked from Files is a recording that happened somewhere else, it // needs exactly the same holding area for exactly the same span — after the // security-scoped original is out of reach and before the crate has copied it — // and a second directory with a second cleanup rule would be a second place for // orphaned bytes to accumulate. import CottonValues import Foundation /// The holding area. public enum MeetingFiles { /// The directory under the container root. public static let directoryName = "recordings" /// A fresh id for one act of recording or importing. Not a meeting id — /// the crate mints that — and it never reaches the wire; it names a /// directory and ties a marker to its bytes. public static func newActId() -> String { UUID().uuidString.lowercased() } /// The directory one act's audio lives in, or `nil` with no App Group — /// which is a library test with no container, never a shipping build. public static func directory( act: String, bundleIdentifier: String? = Bundle.main.bundleIdentifier ) -> URL? { ShareChannel.container(bundleIdentifier: bundleIdentifier)? .appendingPathComponent(directoryName, isDirectory: true) .appendingPathComponent(act, isDirectory: true) } /// The directory, made. Throws whatever `FileManager` throws: a recorder /// that cannot write anywhere must say so before it opens a microphone, not /// after an hour of a meeting. public static func makeDirectory( act: String, bundleIdentifier: String? = Bundle.main.bundleIdentifier ) throws -> URL { guard let directory = directory(act: act, bundleIdentifier: bundleIdentifier) else { throw MeetingFilesFailure.noContainer } try FileManager.default.createDirectory( at: directory, withIntermediateDirectories: true) return directory } /// Where a recording this phone makes is written WHILE IT RUNS — the Core /// Audio File the microphone fills (``MeetingAudioFormats/capturedExtension``). /// /// The name is fixed rather than the act's id repeated: the directory /// already carries the id, and a fixed leaf is what makes the path /// derivable from the marker alone. public static var capturedFileName: String { "audio.\(MeetingAudioFormats.capturedExtension)" } /// Where the same recording is ENCODED to before it is filed — the MPEG-4 /// file `meetings.create` is handed. /// /// **Beside the capture file, not over it.** The capture file is the only /// copy of the audio until the crate has taken it, and an encode that wrote /// over its own input would be this app destroying a recording to make a /// smaller one. Both go when the act's directory does, which is after /// `meetings.create` has accepted the recording and never before. public static var deliveredFileName: String { "audio.\(MeetingAudioFormats.recordedExtension)" } /// **Every capture this app is still holding** — one entry per /// `recordings//` with audio in it. /// /// The marker is a convenience and this is the evidence /// (`MeetingRecovery`): a launch sweeps this so a capture no marker names is /// still offered rather than sitting on the disk for ever with nothing /// pointing at it. It is a directory LISTING and a file's date — no byte of /// any recording is read here. /// /// A directory with nothing this phone would send in it answers nothing: a /// half-written act, or one whose audio has already gone, is not a recording /// to offer. public static func captures( bundleIdentifier: String? = Bundle.main.bundleIdentifier ) -> [MeetingCaptureOnDisk] { guard let root = ShareChannel.container(bundleIdentifier: bundleIdentifier)? .appendingPathComponent(directoryName, isDirectory: true) else { return [] } return captures(under: root) } /// The same, over a directory named outright — the whole of the rule, so /// the App Group is the only thing the call above adds and a test can drive /// this against a temporary directory it actually has. public static func captures(under root: URL) -> [MeetingCaptureOnDisk] { guard let acts = try? FileManager.default.contentsOfDirectory( at: root, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles]) else { return [] } return acts.sorted { $0.lastPathComponent < $1.lastPathComponent }.compactMap { act in guard (try? act.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true, let files = try? FileManager.default.contentsOfDirectory( at: act, includingPropertiesForKeys: [.creationDateKey], options: [.skipsHiddenFiles]) else { return nil } // The CAPTURE first where there is one: an act that reached the // encode has the delivered `.m4a` beside it, and the capture is the // file the marker names and the one that is certainly this // recording's. let audio = files.first { $0.lastPathComponent == capturedFileName } ?? files.sorted { $0.lastPathComponent < $1.lastPathComponent } .first { MeetingAudioFormats.accepts(fileName: $0.lastPathComponent) } guard let audio else { return nil } let made = (try? audio.resourceValues(forKeys: [.creationDateKey]))?.creationDate return MeetingCaptureOnDisk( act: act.lastPathComponent, path: audio.path(percentEncoded: false), startedAt: made.map(CottonDate.format) ?? "" ) } } /// Drop one act's whole directory — the audio and anything beside it. /// /// **Only ever called once the crate has the bytes, or once the person has /// said Discard.** Nothing in this app deletes a recording on its own /// opinion that a call failed. public static func discard(act: String, bundleIdentifier: String? = Bundle.main.bundleIdentifier) { guard let directory = directory(act: act, bundleIdentifier: bundleIdentifier) else { return } try? FileManager.default.removeItem(at: directory) } } /// The holding area, behind a seam. /// /// The shipping one is the App Group's container, which **a library test does /// not have** — the entitlement is not in force outside a signed build — so a /// recorder written straight onto `MeetingFiles` could not be driven through a /// single recording without a simulator. Everything above it takes this /// instead, and a test hands down a temporary directory. public protocol MeetingAudioStore: Sendable { /// Make the act's directory and answer the FILE to write into. func open(act: String, named: String) throws -> URL /// Every capture still being held — see ``MeetingFiles/captures(bundleIdentifier:)``. func captures() -> [MeetingCaptureOnDisk] /// Drop the act's whole directory — the audio and anything beside it. func discard(act: String) } /// The shipping one: `recordings//` under the App Group container. public struct AppGroupMeetingAudioStore: MeetingAudioStore { private let bundleIdentifier: String? public init(bundleIdentifier: String? = Bundle.main.bundleIdentifier) { self.bundleIdentifier = bundleIdentifier } public func open(act: String, named: String) throws -> URL { try MeetingFiles.makeDirectory(act: act, bundleIdentifier: bundleIdentifier) .appendingPathComponent(named.isEmpty ? MeetingFiles.capturedFileName : named) } public func captures() -> [MeetingCaptureOnDisk] { MeetingFiles.captures(bundleIdentifier: bundleIdentifier) } public func discard(act: String) { MeetingFiles.discard(act: act, bundleIdentifier: bundleIdentifier) } } /// Why the holding area could not be used. public enum MeetingFilesFailure: Error, Equatable, LocalizedError { /// The App Group entitlement is not in force. A shipping build always has /// it; a library test never does, and this is what it sees. case noContainer /// The picked file could not be copied out of its security-scoped original /// before the scope closed. case couldNotCopy public var errorDescription: String? { switch self { case .noContainer: return "Cotton has nowhere on this phone to keep the recording." case .couldNotCopy: return "That file couldn't be read." } } }