// Which audio files may become a meeting — the rule `Boundary+Meetings.swift` // (CottonKit) offers through and the share extension classifies a recording by. // In `CottonMailbox` because the extension reads it and links no core. import Foundation /// Which audio files may become a meeting on this phone, and what each one is /// called on the wire. /// /// The extensions are the ones a phone actually hands over: a WhatsApp voice /// note (`.opus`, sometimes `.ogg`/`.oga`), a Voice Memo (`.m4a`), an Android /// recorder (`.amr`, `.3gp` audio), and whatever Files carries (`.mp3`, /// `.wav`, `.aac`, `.caf`, `.flac`, `.mp4` for an audio-only container). public enum MeetingAudioFormats { /// `extension -> the content type an object with that extension is written /// with` — the Swift twin of `cotton-core`'s `MEETING_AUDIO_FORMATS`, and /// held equal to it by `MeetingAudioContractTests`, which reads /// `crates/cotton-core/src/media/meeting_audio.rs` by path. /// /// Generated in NEITHER direction, and a twin rather than a lookup because /// the phone has to know what it may OFFER before it copies a byte: a /// picker that showed a `.3gp` and a refusal three seconds later is a /// picker that lied. /// /// **The key's extension follows the FILE.** The crate reads it off the /// path and never off the caller's claim — an Ogg voice note is written at /// `full.opus`, not at an `.m4a` key that lies about its own bytes. public static let formats: [(ext: String, mime: String)] = [ ("m4a", "audio/mp4"), ("mp4", "audio/mp4"), ("aac", "audio/aac"), ("mp3", "audio/mpeg"), ("wav", "audio/wav"), ("opus", "audio/ogg"), ("ogg", "audio/ogg"), ("oga", "audio/ogg"), ("flac", "audio/flac"), ("amr", "audio/amr"), ("caf", "audio/x-caf"), ] /// Every content type a caller may SAY, and the extension a file of that /// type is filed under — the Swift twin of `cotton-rules`'s /// `MEETING_AUDIO_TYPES` (`crates/cotton-rules/src/media.rs`), which is /// what the server signs an upload against. /// /// Wider than one spelling per container on purpose, and the width is the /// crate's own: a picker, a share sheet and a download each report the same /// file differently, and refusing `audio/x-m4a` because the table said /// `audio/mp4` would be a refusal about vocabulary rather than about the /// file. /// /// The phone reads it in the direction the crate does not: **type → the /// extension to file under**, which is what rescues a share that arrives /// with a declared type and a name carrying no extension at all. The crate /// reads the path; this is how the path comes to have something to read. public static let acceptedTypes: [String: String] = [ "audio/mp4": "m4a", "audio/m4a": "m4a", "audio/x-m4a": "m4a", "audio/aac": "aac", "audio/x-aac": "aac", "audio/mpeg": "mp3", "audio/mp3": "mp3", "audio/wav": "wav", "audio/x-wav": "wav", "audio/wave": "wav", "audio/vnd.wave": "wav", "audio/ogg": "ogg", "audio/opus": "opus", "application/ogg": "ogg", "audio/flac": "flac", "audio/x-flac": "flac", "audio/amr": "amr", "audio/3gpp": "amr", "audio/x-caf": "caf", ] /// Every extension this phone will send as a meeting. public static var extensions: Set { Set(formats.map(\.ext)) } /// The wire type for a file name, or `nil` when this phone does not offer /// that extension at all. /// /// **`nil` is a refusal, never a default.** Sending /// `application/octet-stream` for an extension nobody listed would be this /// phone asking the server to guess at a file it has already decided not /// to understand. public static func mime(forFileName name: String) -> String? { mime(forExtension: shareFileExtension(name)) } /// The same, for an extension already in hand. Case- and dot-insensitive, /// because the callers get theirs from three different places — a name the /// share sheet chose, a path the picker copied, a key the crate built. public static func mime(forExtension raw: String) -> String? { let wanted = raw.trimmingCharacters(in: .whitespaces) .trimmingPrefix(".") .lowercased() return formats.first { $0.ext == wanted }?.mime } /// The extension a file of this DECLARED type should be filed under, or /// `nil` for a type the server would not take. /// /// The rescue path, and the only caller is the share branch: a voice memo /// can arrive as `audio/x-m4a` with a name that has no extension at all, /// and the crate reads the path. Without this the phone would hand over a /// path it knows is unreadable and pass on the crate's refusal as though it /// were news. public static func fileExtension(forDeclaredType raw: String) -> String? { acceptedTypes[raw.trimmingCharacters(in: .whitespaces).lowercased()] } /// The lowercased extension of a file name, or `""` when it has none. /// /// The share sheet's own `shareFileExtension` said once, in public: the /// rule is the same one and a second spelling of "where does the extension /// start" is a second answer waiting to disagree. public static func fileExtension(ofFileName name: String) -> String { shareFileExtension(name) } /// True when a name ends in something this phone will send. public static func accepts(fileName name: String) -> Bool { mime(forFileName: name) != nil } /// The name a meeting takes when nothing else names it: the file's own /// stem, with the extension and any directory taken off. /// /// Empty for a name that is nothing but an extension (`.m4a`) or nothing at /// all — and empty means "say nothing", because the crate's own default /// (the file stem for an import, no title at all for a recording, and the /// day drawn by the screen for either) beats a blank line. public static func titleFromFileName(_ name: String) -> String { let base = name.split(whereSeparator: { $0 == "/" || $0 == "\\" }).last.map(String.init) ?? "" guard let dot = base.lastIndex(of: "."), dot != base.startIndex else { return base.trimmingCharacters(in: .whitespacesAndNewlines) } return String(base[base.startIndex..