feat(app): add in-app artifact preview with paging and honest limits
This commit is contained in:
@@ -196,13 +196,17 @@ bool isMarkdownArtifact(String contentType, String name) {
|
||||
return _markdownExtensions.contains(_extensionOf(name));
|
||||
}
|
||||
|
||||
/// Whether an artifact opens inside the full-screen viewer: images, videos, and
|
||||
/// markdown/text documents. A `text/plain` log, source file or spreadsheet stays
|
||||
/// an external file because those are evidence, not prose.
|
||||
/// 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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
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';
|
||||
}
|
||||
@@ -7,24 +7,34 @@ import 'artifact_record.dart';
|
||||
/// a visit-scoped key so two url-less records never collapse into one page.
|
||||
class PagedArtifact {
|
||||
const PagedArtifact({
|
||||
required this.stageKey,
|
||||
required this.visit,
|
||||
required this.group,
|
||||
required this.artifact,
|
||||
required this.pageIndex,
|
||||
});
|
||||
|
||||
final String stageKey;
|
||||
final int visit;
|
||||
final ArtifactGroup group;
|
||||
final ArtifactRecord artifact;
|
||||
|
||||
/// Position in the filtered page list.
|
||||
final int pageIndex;
|
||||
|
||||
String get stageKey => group.stageKey;
|
||||
int get visit => group.visit;
|
||||
|
||||
String get identity => artifact.hasUrl
|
||||
? artifact.url
|
||||
: '$stageKey\u0000$visit\u0000$pageIndex\u0000${artifact.name}';
|
||||
}
|
||||
|
||||
/// The gallery row identity of [artifact] inside [group], shared by the row
|
||||
/// list and the viewer so selecting a row opens exactly that page. The recorded
|
||||
/// visit is part of the key, so two visits that recorded the same name stay
|
||||
/// distinct even when their recorded urls match.
|
||||
String artifactVisitIdentityOf(ArtifactGroup group, ArtifactRecord artifact) {
|
||||
final recorded = artifact.hasUrl ? artifact.url : artifact.name;
|
||||
return '${group.stageKey}\u0000${group.visit}\u0000$recorded';
|
||||
}
|
||||
|
||||
/// The artifacts the viewer can page through, in gallery order: group by group,
|
||||
/// each group's images, videos and markdown/text documents in recorded order.
|
||||
/// Other artifacts open externally and are not part of this list. Pure: it only
|
||||
@@ -36,8 +46,7 @@ List<PagedArtifact> pagedArtifacts(List<ArtifactGroup> groups) {
|
||||
if (!isViewerArtifact(artifact.contentType, artifact.name)) continue;
|
||||
pages.add(
|
||||
PagedArtifact(
|
||||
stageKey: group.stageKey,
|
||||
visit: group.visit,
|
||||
group: group,
|
||||
artifact: artifact,
|
||||
pageIndex: pages.length,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user