129 lines
4.1 KiB
Dart
129 lines
4.1 KiB
Dart
import 'artifact_kind.dart';
|
|
import 'artifact_record.dart';
|
|
import 'artifact_url.dart';
|
|
|
|
/// What the in-app viewer can do with one recorded artifact.
|
|
enum ArtifactPreviewKind {
|
|
image,
|
|
text,
|
|
markdown,
|
|
|
|
/// A same-origin video, played in the viewer from its resolved url (never
|
|
/// fetched as bytes, never autoplayed), as the web viewer does.
|
|
video,
|
|
|
|
/// A CSV/TSV drawn as a table from its fetched text.
|
|
table,
|
|
|
|
/// An HTML document rendered inertly (no scripts, no navigation).
|
|
html,
|
|
|
|
/// A recorded file the viewer cannot draw (archives, PDFs, ...).
|
|
unsupported,
|
|
|
|
/// No safe same-origin url to fetch: absent, unsafe or another origin.
|
|
unavailable,
|
|
}
|
|
|
|
/// One artifact's preview decision: the safe resolved url, the content kind and
|
|
/// the exact wording for a state that cannot be previewed.
|
|
///
|
|
/// An external url is deliberately [ArtifactPreviewKind.unavailable]: the viewer
|
|
/// never fetches another origin, so a recorded url on a different host is marked
|
|
/// instead of being requested with the app's credentials. [canFetch] is the one
|
|
/// gate the viewer's fetch consults.
|
|
class ArtifactPreviewPlan {
|
|
const ArtifactPreviewPlan({
|
|
required this.kind,
|
|
required this.resolved,
|
|
required this.message,
|
|
this.delimiter = ',',
|
|
});
|
|
|
|
final ArtifactPreviewKind kind;
|
|
final ResolvedArtifactUrl? resolved;
|
|
|
|
/// The explicit reason a non-previewable artifact is not drawn; empty for an
|
|
/// image, text or markdown document that may be fetched.
|
|
final String message;
|
|
|
|
/// The field separator of a [ArtifactPreviewKind.table] preview.
|
|
final String delimiter;
|
|
|
|
bool get canFetch =>
|
|
resolved != null &&
|
|
resolved!.external == false &&
|
|
(kind == ArtifactPreviewKind.image ||
|
|
kind == ArtifactPreviewKind.text ||
|
|
kind == ArtifactPreviewKind.markdown ||
|
|
kind == ArtifactPreviewKind.table ||
|
|
kind == ArtifactPreviewKind.html);
|
|
}
|
|
|
|
const String artifactPreviewNoUrl = 'No artifact URL recorded.';
|
|
const String artifactPreviewUnsafeUrl =
|
|
'The recorded artifact URL cannot be opened safely.';
|
|
const String artifactPreviewExternalUrl =
|
|
'This artifact is hosted on another origin and is not previewed here.';
|
|
|
|
/// Classifies [artifact] against the configured server origin.
|
|
///
|
|
/// The format comes from [artifactPreviewFormat]: images are drawn from the
|
|
/// resolved bytes, a video plays from the resolved url, markdown goes to the
|
|
/// injected builder and the text family is selectable monospace text.
|
|
/// Everything else is explicitly unsupported rather than faked.
|
|
ArtifactPreviewPlan planArtifactPreview({
|
|
required ArtifactRecord artifact,
|
|
required String configuredEndpoint,
|
|
}) {
|
|
if (!artifact.hasUrl) {
|
|
return const ArtifactPreviewPlan(
|
|
kind: ArtifactPreviewKind.unavailable,
|
|
resolved: null,
|
|
message: artifactPreviewNoUrl,
|
|
);
|
|
}
|
|
final resolved = resolveArtifactUrl(configuredEndpoint, artifact.url);
|
|
if (resolved == null) {
|
|
return const ArtifactPreviewPlan(
|
|
kind: ArtifactPreviewKind.unavailable,
|
|
resolved: null,
|
|
message: artifactPreviewUnsafeUrl,
|
|
);
|
|
}
|
|
if (resolved.external) {
|
|
return ArtifactPreviewPlan(
|
|
kind: ArtifactPreviewKind.unavailable,
|
|
resolved: resolved,
|
|
message: artifactPreviewExternalUrl,
|
|
);
|
|
}
|
|
final kind = switch (artifactPreviewFormat(
|
|
artifact.contentType,
|
|
artifact.name,
|
|
)) {
|
|
ArtifactPreviewFormat.image => ArtifactPreviewKind.image,
|
|
ArtifactPreviewFormat.video => ArtifactPreviewKind.video,
|
|
ArtifactPreviewFormat.markdown => ArtifactPreviewKind.markdown,
|
|
ArtifactPreviewFormat.text => ArtifactPreviewKind.text,
|
|
ArtifactPreviewFormat.table => ArtifactPreviewKind.table,
|
|
ArtifactPreviewFormat.html => ArtifactPreviewKind.html,
|
|
ArtifactPreviewFormat.none => null,
|
|
};
|
|
if (kind != null) {
|
|
return ArtifactPreviewPlan(
|
|
kind: kind,
|
|
resolved: resolved,
|
|
message: '',
|
|
delimiter: isTabSeparatedArtifact(artifact.contentType, artifact.name)
|
|
? '\t'
|
|
: ',',
|
|
);
|
|
}
|
|
return ArtifactPreviewPlan(
|
|
kind: ArtifactPreviewKind.unsupported,
|
|
resolved: resolved,
|
|
message: 'This file type cannot be previewed here.',
|
|
);
|
|
}
|