Files
pipelines/app/lib/features/artifacts/domain/artifact_preview.dart
T

119 lines
3.6 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 recorded file the viewer cannot draw (video, CSV, archives, ...).
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,
});
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;
bool get canFetch =>
resolved != null &&
resolved!.external == false &&
(kind == ArtifactPreviewKind.image ||
kind == ArtifactPreviewKind.text ||
kind == ArtifactPreviewKind.markdown);
}
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.
///
/// Images are drawn from the resolved bytes; `text/plain` is selectable text; a
/// `.md`/`.markdown` name or `text/markdown` is handed to the injected markdown
/// builder. 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 mime = artifact.contentType.split(';').first.trim().toLowerCase();
if (isImageContentType(mime)) {
return ArtifactPreviewPlan(
kind: ArtifactPreviewKind.image,
resolved: resolved,
message: '',
);
}
if (mime == 'text/markdown' || _isMarkdownName(artifact.name)) {
return ArtifactPreviewPlan(
kind: ArtifactPreviewKind.markdown,
resolved: resolved,
message: '',
);
}
if (mime == 'text/plain') {
return ArtifactPreviewPlan(
kind: ArtifactPreviewKind.text,
resolved: resolved,
message: '',
);
}
return ArtifactPreviewPlan(
kind: ArtifactPreviewKind.unsupported,
resolved: resolved,
message: 'This file type cannot be previewed here.',
);
}
bool _isMarkdownName(String name) {
final dot = name.lastIndexOf('.');
if (dot == -1) return false;
final extension = name.substring(dot + 1).toLowerCase();
return extension == 'md' || extension == 'markdown';
}