269 lines
7.6 KiB
Dart
269 lines
7.6 KiB
Dart
/// 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<String> _archiveExtensions = <String>{
|
|
'zip',
|
|
'tar',
|
|
'gz',
|
|
'tgz',
|
|
'bz2',
|
|
'xz',
|
|
'rar',
|
|
'7z',
|
|
'jar',
|
|
'war',
|
|
};
|
|
|
|
const Set<String> _archiveMimes = <String>{
|
|
'application/zip',
|
|
'application/gzip',
|
|
'application/x-tar',
|
|
'application/x-7z-compressed',
|
|
'application/x-rar-compressed',
|
|
'application/java-archive',
|
|
};
|
|
|
|
const Set<String> _spreadsheetExtensions = <String>{
|
|
'csv',
|
|
'tsv',
|
|
'xls',
|
|
'xlsx',
|
|
'ods',
|
|
};
|
|
|
|
const Set<String> _spreadsheetMimes = <String>{
|
|
'text/csv',
|
|
'text/tab-separated-values',
|
|
'application/vnd.ms-excel',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
};
|
|
|
|
const Set<String> _codeExtensions = <String>{
|
|
'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<String> _codeMimes = <String>{
|
|
'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<String> _documentExtensions = <String>{
|
|
'txt',
|
|
'md',
|
|
'markdown',
|
|
'rst',
|
|
'doc',
|
|
'docx',
|
|
'odt',
|
|
'rtf',
|
|
'html',
|
|
'htm',
|
|
};
|
|
|
|
const Set<String> _markdownExtensions = <String>{'md', 'markdown', 'txt'};
|
|
|
|
const Set<String> _documentMimes = <String>{
|
|
'application/msword',
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
'application/rtf',
|
|
};
|
|
|
|
const Map<ArtifactKind, String> _kindLabels = <ArtifactKind, String>{
|
|
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));
|
|
}
|
|
|
|
/// How the viewer presents an artifact in-app, or [none] when it only offers
|
|
/// Open/Download.
|
|
enum ArtifactPreviewFormat { image, video, markdown, text, table, html, none }
|
|
|
|
/// The one rule for in-app previews, shared by the gallery's paging and the
|
|
/// viewer's fetch plan. Images and videos come from the content type; markdown
|
|
/// from `text/markdown` or an md/markdown name; HTML rendered inertly;
|
|
/// CSV/TSV as a table; plain
|
|
/// text, logs, JSON and source/config files as selectable monospace text.
|
|
ArtifactPreviewFormat artifactPreviewFormat(String contentType, String name) {
|
|
if (isImageContentType(contentType)) return ArtifactPreviewFormat.image;
|
|
if (isVideoContentType(contentType)) return ArtifactPreviewFormat.video;
|
|
final mime = contentType.split(';').first.trim().toLowerCase();
|
|
final ext = _extensionOf(name);
|
|
if (mime == 'text/markdown' || ext == 'md' || ext == 'markdown') {
|
|
return ArtifactPreviewFormat.markdown;
|
|
}
|
|
if (mime == 'text/html' || ext == 'html' || ext == 'htm') {
|
|
// A turn's session export (and its repairs) stays external-only, opened
|
|
// in its own tab as on the web; other HTML documents preview inertly.
|
|
final lower = name.toLowerCase();
|
|
if (lower == 'session.html' || lower.startsWith('session-repair-')) {
|
|
return ArtifactPreviewFormat.none;
|
|
}
|
|
return ArtifactPreviewFormat.html;
|
|
}
|
|
if (mime == 'text/csv' ||
|
|
mime == 'text/tab-separated-values' ||
|
|
ext == 'csv' ||
|
|
ext == 'tsv') {
|
|
return ArtifactPreviewFormat.table;
|
|
}
|
|
final kind = artifactKind(contentType, name);
|
|
if (kind == ArtifactKind.json ||
|
|
kind == ArtifactKind.log ||
|
|
kind == ArtifactKind.code ||
|
|
mime == 'text/plain' ||
|
|
ext == 'txt' ||
|
|
ext == 'rst') {
|
|
return ArtifactPreviewFormat.text;
|
|
}
|
|
return ArtifactPreviewFormat.none;
|
|
}
|
|
|
|
/// Whether a delimited artifact is tab-separated rather than comma-separated.
|
|
bool isTabSeparatedArtifact(String contentType, String name) =>
|
|
contentType.split(';').first.trim().toLowerCase() ==
|
|
'text/tab-separated-values' ||
|
|
_extensionOf(name) == 'tsv';
|
|
|
|
/// Whether an artifact opens inside the viewer (see [artifactPreviewFormat]).
|
|
bool isViewerArtifact(String contentType, String name) =>
|
|
artifactPreviewFormat(contentType, name) != ArtifactPreviewFormat.none;
|
|
|
|
/// 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';
|
|
}
|