/// What an artifact is from the gallery's point of view: the two media kinds it /// previews and the file kinds it labels with a type word. Mirrors the web /// client's `ArtifactKind` vocabulary. enum ArtifactKind { image, video, audio, pdf, json, archive, spreadsheet, log, code, document, other, } const Set _archiveExtensions = { 'zip', 'tar', 'gz', 'tgz', 'bz2', 'xz', 'rar', '7z', 'jar', 'war', }; const Set _archiveMimes = { 'application/zip', 'application/gzip', 'application/x-tar', 'application/x-7z-compressed', 'application/x-rar-compressed', 'application/java-archive', }; const Set _spreadsheetExtensions = { 'csv', 'tsv', 'xls', 'xlsx', 'ods', }; const Set _spreadsheetMimes = { 'text/csv', 'text/tab-separated-values', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', }; const Set _codeExtensions = { 'kt', 'kts', 'java', 'go', 'py', 'rb', 'php', 'rs', 'c', 'h', 'cc', 'cpp', 'hpp', 'cs', 'js', 'jsx', 'ts', 'tsx', 'mjs', 'cjs', 'swift', 'scala', 'sh', 'bash', 'zsh', 'fish', 'ps1', 'sql', 'yaml', 'yml', 'toml', 'ini', 'xml', 'gradle', 'properties', 'proto', }; const Set _codeMimes = { 'application/xml', 'application/x-yaml', 'application/javascript', 'text/javascript', 'text/x-python', 'text/x-java-source', 'text/x-kotlin', 'application/x-sh', 'application/x-httpd-php', 'application/sql', }; const Set _documentExtensions = { 'txt', 'md', 'markdown', 'rst', 'doc', 'docx', 'odt', 'rtf', 'html', 'htm', }; const Set _markdownExtensions = {'md', 'markdown', 'txt'}; const Set _documentMimes = { 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/rtf', }; const Map _kindLabels = { ArtifactKind.image: 'Image', ArtifactKind.video: 'Video', ArtifactKind.audio: 'Audio', ArtifactKind.pdf: 'PDF', ArtifactKind.json: 'JSON', ArtifactKind.archive: 'Archive', ArtifactKind.spreadsheet: 'Spreadsheet', ArtifactKind.log: 'Log', ArtifactKind.code: 'Code', ArtifactKind.document: 'Document', ArtifactKind.other: 'File', }; /// Whether a recorded content type is an image the gallery previews. bool isImageContentType(String contentType) => contentType.startsWith('image/'); /// Whether a recorded content type is a video; videos get a poster tile and are /// never decoded inline or autoplayed. bool isVideoContentType(String contentType) => contentType.startsWith('video/'); /// The lowercased extension of [name], or '' when it carries none. String _extensionOf(String name) { final dot = name.lastIndexOf('.'); return dot == -1 ? '' : name.substring(dot + 1).toLowerCase(); } /// The gallery kind for a recorded content type and name. /// /// Image and video come straight from the content type; the other kinds match /// the normalised MIME first, then the name's extension. Pure and total: an /// unknown type is [ArtifactKind.other], never a guess that changes an action. ArtifactKind artifactKind(String contentType, String name) { if (isImageContentType(contentType)) return ArtifactKind.image; if (isVideoContentType(contentType)) return ArtifactKind.video; final mime = contentType.split(';').first.trim().toLowerCase(); final ext = _extensionOf(name); if (mime.startsWith('audio/')) return ArtifactKind.audio; if (mime == 'application/pdf' || ext == 'pdf') return ArtifactKind.pdf; if (mime.contains('json') || ext == 'json') return ArtifactKind.json; if (_archiveMimes.contains(mime) || _archiveExtensions.contains(ext)) { return ArtifactKind.archive; } if (_spreadsheetMimes.contains(mime) || _spreadsheetExtensions.contains(ext)) { return ArtifactKind.spreadsheet; } if (ext == 'log') return ArtifactKind.log; if (mime == 'text/html') return ArtifactKind.document; if (_codeMimes.contains(mime) || _codeExtensions.contains(ext)) { return ArtifactKind.code; } if (mime.startsWith('text/') || _documentMimes.contains(mime) || _documentExtensions.contains(ext)) { return ArtifactKind.document; } return ArtifactKind.other; } /// The prose word naming [kind], so a type glyph never has to be read alone. String artifactKindLabel(ArtifactKind kind) => _kindLabels[kind] ?? 'File'; /// Whether an artifact is text the viewer can render as markdown: `text/markdown` /// or `text/plain` (parameters ignored), or an md/markdown/txt name. bool isMarkdownArtifact(String contentType, String name) { final mime = contentType.split(';').first.trim().toLowerCase(); if (mime == 'text/markdown' || mime == 'text/plain') return true; return _markdownExtensions.contains(_extensionOf(name)); } /// Whether an artifact opens inside the viewer: images, videos, markdown/text /// documents and any recorded `text/plain` file. The viewer draws text/plain as /// selectable text; a CSV, source file or spreadsheet with its own type stays an /// external file because it is evidence, not prose. Videos are pageable but the /// viewer reports that it cannot draw them rather than faking a player. bool isViewerArtifact(String contentType, String name) { if (isImageContentType(contentType) || isVideoContentType(contentType)) { return true; } final mime = contentType.split(';').first.trim().toLowerCase(); if (mime == 'text/plain') return true; return artifactKind(contentType, name) == ArtifactKind.document && isMarkdownArtifact(contentType, name); } /// Human-readable byte size, matching the web client: whole bytes under 1 KiB, /// otherwise one decimal with a binary unit (KiB/MiB/GiB/TiB/PiB/EiB). String humanBytes(num size) { final value = size.truncate(); const unit = 1024; if (value < unit) return '$value B'; var div = unit; var exp = 0; var scaled = value ~/ unit; while (scaled >= unit) { div *= unit; exp++; scaled ~/= unit; } return '${(value / div).toStringAsFixed(1)} ${'KMGTPE'[exp]}iB'; }