// 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.importCottonValuesimportFoundation/// The holding area.publicenumMeetingFiles {/// The directory under the container root.publicstaticlet 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.publicstaticfunc 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.publicstaticfunc 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.publicstaticfunc makeDirectory( act: String, bundleIdentifier: String? = Bundle.main.bundleIdentifier ) throws -> URL {guardlet directory = directory(act: act, bundleIdentifier: bundleIdentifier) else {throwMeetingFilesFailure.noContainer }tryFileManager.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.publicstaticvar 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.publicstaticvar deliveredFileName: String { "audio.\(MeetingAudioFormats.recordedExtension)" }/// **Every capture this app is still holding** — one entry per/// `recordings/<act>/` 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.publicstaticfunc captures( bundleIdentifier: String? = Bundle.main.bundleIdentifier ) -> [MeetingCaptureOnDisk] {guardlet 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.publicstaticfunc captures(under root: URL) -> [MeetingCaptureOnDisk] {guardlet acts = try? FileManager.default.contentsOfDirectory( at: root, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles])else { return [] }return acts.sorted { $0.lastPathComponent < $1.lastPathComponent }.compactMap { act inguard (try? act.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true,let files = try? FileManager.default.contentsOfDirectory( at: act, includingPropertiesForKeys: [.creationDateKey], options: [.skipsHiddenFiles])else { returnnil }// 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) }guardlet audio else { returnnil }let made = (try? audio.resourceValues(forKeys: [.creationDateKey]))?.creationDatereturnMeetingCaptureOnDisk( 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.publicstaticfunc discard(act: String, bundleIdentifier: String? = Bundle.main.bundleIdentifier) {guardlet 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.publicprotocolMeetingAudioStore: Sendable {/// Make the act's directory and answer the FILE to write into.funcopen(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/<act>/` under the App Group container.publicstructAppGroupMeetingAudioStore: MeetingAudioStore {privatelet bundleIdentifier: String?publicinit(bundleIdentifier: String? = Bundle.main.bundleIdentifier) {self.bundleIdentifier = bundleIdentifier }publicfuncopen(act: String, named: String) throws -> URL {tryMeetingFiles.makeDirectory(act: act, bundleIdentifier: bundleIdentifier) .appendingPathComponent(named.isEmpty ? MeetingFiles.capturedFileName : named) }publicfunc captures() -> [MeetingCaptureOnDisk] {MeetingFiles.captures(bundleIdentifier: bundleIdentifier) }publicfunc discard(act: String) {MeetingFiles.discard(act: act, bundleIdentifier: bundleIdentifier) }}/// Why the holding area could not be used.publicenumMeetingFilesFailure: 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 couldNotCopypublicvar errorDescription: String? {switchself {case .noContainer:return"Cotton has nowhere on this phone to keep the recording."case .couldNotCopy:return"That file couldn't be read." } }}