//! The kinds of media Cotton keeps, each kind's ceiling and the content types //! it takes, and the type each file extension carries. The phone's storage //! (cotton-core media/keys.rs, which builds the keys) and the Worker's upload //! gates (apps/business media.ts, as WASM) read the same numbers. /// Every content type a meeting's audio may be stored as. /// /// This IS `media_limits(MediaKind::Meeting).content_types`: the server signs /// an upload against it and the phone refuses before it copies a byte, so a /// type missing here is a recording the owner cannot file at all. /// /// Wider than one spelling per container on purpose — 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. pub const MEETING_AUDIO_TYPES: &[&str] = &[ // AAC in an MPEG-4 container — Voice Memos, the app's own recorder, and // anything a Mac exported. "audio/mp4", "audio/m4a", "audio/x-m4a", "audio/aac", "audio/x-aac", // MP3. "audio/mpeg", "audio/mp3", // WAV, whose spellings are famously four. "audio/wav", "audio/x-wav", "audio/wave", "audio/vnd.wave", // Opus in an Ogg container — EVERY WhatsApp voice note, which is the // recording the owner shares into Cotton most often. "audio/ogg", "audio/opus", "application/ogg", // FLAC, which a person who keeps their own recordings well will have. "audio/flac", "audio/x-flac", // AMR — a 3GPP voice recording, still what some phones hand over. "audio/amr", "audio/3gpp", // Core Audio, which an iOS recorder can produce. "audio/x-caf", ]; /// The kinds of media Cotton stores, each with its own ceiling and manners. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum MediaKind { Document, Statement, Meeting, Message, Avatar, Walkthrough, Session, Photo, Video, /// A held repository's own objects: its git bundles, its release assets /// and the attachments its issues and pull requests carry /// (Cotton note "Code hosting — why a repository lives in Cotton"). Repo, /// A book the person reads in the app: an EPUB or a PDF, one object per /// file, keyed by its own bytes (`/books/.`). Book, /// A song the person keeps as a file: one audio object per file, keyed by /// its own bytes (`/music/.`). Song, /// A film's files, under `/films//`: the HLS folder the /// Mac makes, a whole MP4, M4V or MOV the phone plays as it is /// (`whole.`), or the upload waiting for the Mac (`source.`). Film, } impl MediaKind { /// The wire spelling — the same string the TypeScript union carries, so an /// error body or a log line reads identically. pub const fn as_str(self) -> &'static str { match self { MediaKind::Document => "document", MediaKind::Statement => "statement", MediaKind::Meeting => "meeting", MediaKind::Message => "message", MediaKind::Avatar => "avatar", MediaKind::Walkthrough => "walkthrough", MediaKind::Session => "session", MediaKind::Photo => "photo", MediaKind::Video => "video", MediaKind::Repo => "repo", MediaKind::Book => "book", MediaKind::Song => "song", MediaKind::Film => "film", } } /// Every kind, in the order `MEDIA_LIMITS` declares them. pub const ALL: [MediaKind; 13] = [ MediaKind::Document, MediaKind::Meeting, MediaKind::Statement, MediaKind::Message, MediaKind::Avatar, MediaKind::Walkthrough, MediaKind::Session, MediaKind::Photo, MediaKind::Video, MediaKind::Repo, MediaKind::Book, MediaKind::Song, MediaKind::Film, ]; } impl std::fmt::Display for MediaKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) } } /// What the server will mint an upload URL for, for one kind. #[derive(Debug, Clone, Copy)] pub struct MediaLimit { pub max_bytes: u64, pub content_types: &'static [&'static str], } const DOCUMENT_TYPES: &[&str] = &[ "application/pdf", "image/png", "image/jpeg", "image/webp", "image/heic", "image/heif", "application/msword", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "text/plain", "text/csv", // A saved Claude artifact: one self-contained page. Its bytes are only ever // read as text on the server and opened sealed on a device. "text/html", // The picker does not always know, and the pipeline sniffs the bytes // anyway — refusing this would refuse real files for no safety gain. "application/octet-stream", ]; /// DECLARED in cotton-core's `media::meeting_audio`, beside the extension table it /// has to agree with: a container the key can spell and the limit will not sign /// is a recording the owner can file and never transcribe. const MEETING_TYPES: &[&str] = MEETING_AUDIO_TYPES; const STATEMENT_TYPES: &[&str] = &[ "application/pdf", "text/csv", "application/csv", "text/comma-separated-values", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "image/png", "image/jpeg", "image/webp", "image/heic", "image/heif", "application/octet-stream", ]; const MESSAGE_TYPES: &[&str] = &[ "image/jpeg", "image/png", "image/webp", "image/gif", "image/heic", "image/heif", "video/mp4", "video/quicktime", "video/3gpp", "audio/mp4", "audio/m4a", "audio/x-m4a", "audio/aac", "audio/mpeg", "audio/ogg", "audio/opus", // The phone-playable CAF/Opus rendition of a voice note (`voice_rendition`). "audio/x-caf", "application/pdf", "application/msword", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/zip", "text/plain", "text/csv", "text/html", "text/vcard", "text/x-vcard", "application/octet-stream", ]; const AVATAR_TYPES: &[&str] = &[ "image/jpeg", "image/png", "image/webp", "image/heic", "image/heif", ]; const WALKTHROUGH_TYPES: &[&str] = &["image/webp", "image/png", "image/jpeg"]; const SESSION_TYPES: &[&str] = &[ "image/png", "image/jpeg", "image/webp", "image/heic", "image/heif", ]; const PHOTO_TYPES: &[&str] = &[ "image/jpeg", "image/png", "image/webp", "image/heic", "image/gif", ]; const VIDEO_TYPES: &[&str] = &["video/mp4", "video/quicktime", "image/jpeg"]; /// What a held repository's objects are. /// /// `application/x-git-bundle` is the bundle itself — the one object a /// repository's BYTES travel as. The rest is what a release carries and what /// an issue's attachment turns out to be: a Forgejo release asset is usually a /// compressed archive or a database dump, an attachment is usually a picture or /// a log, and a host that sent no type at all sends `application/octet-stream`. /// `text/plain` is also a finished CI job's log, which phase 3 files under the /// same folder. /// /// Deliberately NOT the document list: a repository's objects are not /// something a person picked in a file picker, and a `.docx` under `repos/` /// would be a release asset nobody produced. const REPO_TYPES: &[&str] = &[ "application/x-git-bundle", "application/octet-stream", "application/zip", "application/gzip", "application/x-tar", "text/plain", "application/json", "image/png", "image/jpeg", ]; /// What a book is: an EPUB (`org.idpf.epub-container`) or a PDF, and nothing /// else — the two the reader opens. No `application/octet-stream`: the phone /// knows which of the two it holds from the file, and a key's extension says /// it again. const BOOK_TYPES: &[&str] = &["application/epub+zip", "application/pdf"]; /// The extensions a book key ends in, beside [`BOOK_TYPES`] in the same order. pub const BOOK_EXTENSIONS: &[&str] = &["epub", "pdf"]; /// What a song is: the audio a music library is made of — MP3, AAC or ALAC /// in an MP4 box, FLAC, Ogg Vorbis or Opus, WAV and AIFF — and nothing else. /// No `application/octet-stream`: the phone reads the format from the file, /// and a key's extension says it again. const SONG_TYPES: &[&str] = &[ "audio/mpeg", "audio/mp4", "audio/flac", "audio/ogg", "audio/wav", "audio/aiff", ]; /// The extensions a song key ends in. An Opus file is an Ogg stream, so `opus` /// and `ogg` share `audio/ogg`. pub const SONG_EXTENSIONS: &[&str] = &["mp3", "m4a", "flac", "ogg", "opus", "wav", "aiff"]; /// The content type a book key's extension carries, `None` for one no book /// ends in. pub fn book_content_type(ext: &str) -> Option<&'static str> { Some(match ext.trim_start_matches('.').to_ascii_lowercase().as_str() { "epub" => "application/epub+zip", "pdf" => "application/pdf", _ => return None, }) } /// The content type a song key's extension carries, `None` for one no song /// ends in. pub fn song_content_type(ext: &str) -> Option<&'static str> { Some(match ext.trim_start_matches('.').to_ascii_lowercase().as_str() { "mp3" => "audio/mpeg", "m4a" => "audio/mp4", "flac" => "audio/flac", "ogg" | "opus" => "audio/ogg", "wav" => "audio/wav", "aiff" => "audio/aiff", _ => return None, }) } /// What a film's folder holds: a whole film the phone plays as it is (MP4 or /// MOV), the upload in any container waiting for the Mac, and the HLS /// folder's own files (playlists, fMP4 segments, subtitles, the poster). An /// upload in a container no type names is `application/octet-stream`. const FILM_TYPES: &[&str] = &[ "video/mp4", "video/quicktime", "video/x-matroska", "video/x-msvideo", "video/webm", "video/mp2t", "video/x-ms-wmv", "video/x-flv", "video/mpeg", "application/vnd.apple.mpegurl", "video/iso.segment", "text/vtt", "image/jpeg", "application/octet-stream", ]; /// The extensions a whole film ends in: the containers the phone plays as /// they are (cotton-core's `films::WHOLE_EXTENSIONS`). pub const FILM_WHOLE_EXTENSIONS: &[&str] = &["mp4", "m4v", "mov"]; /// The extensions a film's upload for the Mac ends in: every container a /// film file comes in (cotton-core's `films::VIDEO_EXTENSIONS`). pub const FILM_SOURCE_EXTENSIONS: &[&str] = &["mkv", "avi", "mp4", "m4v", "mov", "webm", "ts", "m2ts", "wmv", "mpg", "mpeg", "flv"]; /// The content type a film file's extension carries, whole or an upload for /// the Mac, `None` for one no film file ends in. pub fn film_content_type(ext: &str) -> Option<&'static str> { Some(match ext.trim_start_matches('.').to_ascii_lowercase().as_str() { "mp4" | "m4v" => "video/mp4", "mov" => "video/quicktime", "mkv" => "video/x-matroska", "avi" => "video/x-msvideo", "webm" => "video/webm", "ts" | "m2ts" => "video/mp2t", "wmv" => "video/x-ms-wmv", "mpg" | "mpeg" => "video/mpeg", "flv" => "video/x-flv", _ => return None, }) } /// The ceiling and the type list for one kind. /// /// The byte ceilings are the ones the Storage-era buckets carried, plus the /// server's own 25 MB statement ceiling; the content-type lists are a PRODUCT /// decision, not a storage one — these are the types each tool actually /// produces. A presigned PUT pins the type it was minted for, so an upload /// cannot smuggle a different one past this list. pub const fn media_limits(kind: MediaKind) -> MediaLimit { match kind { MediaKind::Document => MediaLimit { max_bytes: 104_857_600, content_types: DOCUMENT_TYPES, }, MediaKind::Meeting => MediaLimit { max_bytes: 524_288_000, content_types: MEETING_TYPES, }, MediaKind::Statement => MediaLimit { max_bytes: 26_214_400, content_types: STATEMENT_TYPES, }, MediaKind::Message => MediaLimit { max_bytes: 104_857_600, content_types: MESSAGE_TYPES, }, MediaKind::Avatar => MediaLimit { max_bytes: 10_485_760, content_types: AVATAR_TYPES, }, MediaKind::Walkthrough => MediaLimit { max_bytes: 26_214_400, content_types: WALKTHROUGH_TYPES, }, MediaKind::Session => MediaLimit { max_bytes: 26_214_400, content_types: SESSION_TYPES, }, MediaKind::Photo => MediaLimit { max_bytes: 52_428_800, content_types: PHOTO_TYPES, }, MediaKind::Video => MediaLimit { max_bytes: 524_288_000, content_types: VIDEO_TYPES, }, // Four gibibytes — a FULL bundle of a large repository, which is the // one object here that is not a person's file at all. Tinmen's git is // 371 MB and its releases carry 2.36 GB of nightly database backups, // so this ceiling is the biggest single object either produces with // room over it. MediaKind::Repo => MediaLimit { max_bytes: 4_294_967_296, content_types: REPO_TYPES, }, // 500 MB: a novel is under a megabyte and an illustrated one tens, but // a scanned book is a PDF of 50 to 300 MB; the drain sends anything // over one request's limit in parts. MediaKind::Book => MediaLimit { max_bytes: 524_288_000, content_types: BOOK_TYPES, }, // 1 GB: a song is 3 to 10 MB as MP3 and 20 to 60 MB as FLAC, but a // continuous mix or a whole concert in one hi-res file runs to a // gigabyte; the drain sends anything over one request's limit in parts. MediaKind::Song => MediaLimit { max_bytes: 1_073_741_824, content_types: SONG_TYPES, }, // 20 GiB, the Worker's FILM_MAX_BYTES: a 4K remux runs to tens of // gigabytes, but a film the owner keeps is 1 to 15 GB; the drain sends // anything over one request's limit in parts. MediaKind::Film => MediaLimit { max_bytes: 21_474_836_480, content_types: FILM_TYPES, }, } } /// What the Worker's upload gates read: each kept kind's ceiling, and the type /// each of its extensions carries (a film's whole files and its uploads apart). pub fn upload_table() -> serde_json::Value { use serde_json::{json, Map, Value}; let by_ext = |exts: &[&str], of: fn(&str) -> Option<&'static str>| -> Value { Value::Object(exts.iter().filter_map(|ext| of(ext).map(|t| ((*ext).to_string(), json!(t)))).collect::>()) }; json!({ "book": { "max_bytes": media_limits(MediaKind::Book).max_bytes, "types": by_ext(BOOK_EXTENSIONS, book_content_type) }, "song": { "max_bytes": media_limits(MediaKind::Song).max_bytes, "types": by_ext(SONG_EXTENSIONS, song_content_type) }, "film": { "max_bytes": media_limits(MediaKind::Film).max_bytes, "whole": by_ext(FILM_WHOLE_EXTENSIONS, film_content_type), "source": by_ext(FILM_SOURCE_EXTENSIONS, film_content_type), }, }) }