Complete chat file previews downloads and explorer actions
This commit is contained in:
@@ -198,7 +198,16 @@ bool isMarkdownArtifact(String contentType, String 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 }
|
||||
enum ArtifactPreviewFormat {
|
||||
image,
|
||||
video,
|
||||
audio,
|
||||
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
|
||||
@@ -210,6 +219,7 @@ ArtifactPreviewFormat artifactPreviewFormat(String contentType, String name) {
|
||||
if (isVideoContentType(contentType)) return ArtifactPreviewFormat.video;
|
||||
final mime = contentType.split(';').first.trim().toLowerCase();
|
||||
final ext = _extensionOf(name);
|
||||
if (mime.startsWith('audio/')) return ArtifactPreviewFormat.audio;
|
||||
if (mime == 'text/markdown' || ext == 'md' || ext == 'markdown') {
|
||||
return ArtifactPreviewFormat.markdown;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ enum ArtifactPreviewKind {
|
||||
/// fetched as bytes, never autoplayed), as the web viewer does.
|
||||
video,
|
||||
|
||||
/// A same-origin audio file, played with the shared media controller.
|
||||
audio,
|
||||
|
||||
/// A CSV/TSV drawn as a table from its fetched text.
|
||||
table,
|
||||
|
||||
@@ -68,13 +71,16 @@ const String artifactPreviewExternalUrl =
|
||||
|
||||
/// Classifies [artifact] against the configured server origin.
|
||||
///
|
||||
/// The format comes from [artifactPreviewFormat]: images are drawn from the
|
||||
/// The format comes from [format] (the shared [artifactPreviewFormat] unless a
|
||||
/// caller such as the chat Files panel has its own rule): 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,
|
||||
ArtifactPreviewFormat Function(String contentType, String name) format =
|
||||
artifactPreviewFormat,
|
||||
}) {
|
||||
if (!artifact.hasUrl) {
|
||||
return const ArtifactPreviewPlan(
|
||||
@@ -98,12 +104,10 @@ ArtifactPreviewPlan planArtifactPreview({
|
||||
message: artifactPreviewExternalUrl,
|
||||
);
|
||||
}
|
||||
final kind = switch (artifactPreviewFormat(
|
||||
artifact.contentType,
|
||||
artifact.name,
|
||||
)) {
|
||||
final kind = switch (format(artifact.contentType, artifact.name)) {
|
||||
ArtifactPreviewFormat.image => ArtifactPreviewKind.image,
|
||||
ArtifactPreviewFormat.video => ArtifactPreviewKind.video,
|
||||
ArtifactPreviewFormat.audio => ArtifactPreviewKind.audio,
|
||||
ArtifactPreviewFormat.markdown => ArtifactPreviewKind.markdown,
|
||||
ArtifactPreviewFormat.text => ArtifactPreviewKind.text,
|
||||
ArtifactPreviewFormat.table => ArtifactPreviewKind.table,
|
||||
|
||||
@@ -224,6 +224,8 @@ IconData artifactKindIcon(ArtifactRecord artifact, {required bool cupertino}) {
|
||||
cupertino ? CupertinoIcons.photo : Icons.image_outlined,
|
||||
ArtifactPreviewFormat.video =>
|
||||
cupertino ? CupertinoIcons.film : Icons.movie_outlined,
|
||||
ArtifactPreviewFormat.audio =>
|
||||
cupertino ? CupertinoIcons.music_note : Icons.audio_file_outlined,
|
||||
ArtifactPreviewFormat.markdown =>
|
||||
cupertino ? CupertinoIcons.doc_text : Icons.article_outlined,
|
||||
ArtifactPreviewFormat.text =>
|
||||
|
||||
@@ -4,7 +4,7 @@ import 'package:video_player/video_player.dart';
|
||||
|
||||
import '../../../ui/adaptive/host.dart';
|
||||
|
||||
/// Creates the player for one video url; tests inject a fake controller.
|
||||
/// Creates the player for one media url; tests inject a fake controller.
|
||||
typedef ArtifactVideoControllerFactory =
|
||||
VideoPlayerController Function(Uri uri);
|
||||
|
||||
@@ -13,6 +13,8 @@ VideoPlayerController defaultArtifactVideoController(Uri uri) =>
|
||||
|
||||
const String artifactVideoPlayLabel = 'Play video';
|
||||
const String artifactVideoPauseLabel = 'Pause video';
|
||||
const String artifactAudioPlayLabel = 'Play audio';
|
||||
const String artifactAudioPauseLabel = 'Pause audio';
|
||||
const String artifactVideoError = 'The video could not be played.';
|
||||
|
||||
/// Plays one recorded same-origin video in the viewer, as the web viewer's
|
||||
@@ -22,10 +24,12 @@ class ArtifactVideoPlayer extends StatefulWidget {
|
||||
super.key,
|
||||
required this.uri,
|
||||
this.controllerFactory = defaultArtifactVideoController,
|
||||
this.audio = false,
|
||||
});
|
||||
|
||||
final Uri uri;
|
||||
final ArtifactVideoControllerFactory controllerFactory;
|
||||
final bool audio;
|
||||
|
||||
@override
|
||||
State<ArtifactVideoPlayer> createState() => _ArtifactVideoPlayerState();
|
||||
@@ -95,23 +99,29 @@ class _ArtifactVideoPlayerState extends State<ArtifactVideoPlayer> {
|
||||
);
|
||||
}
|
||||
final label = value.isPlaying
|
||||
? artifactVideoPauseLabel
|
||||
: artifactVideoPlayLabel;
|
||||
? (widget.audio ? artifactAudioPauseLabel : artifactVideoPauseLabel)
|
||||
: (widget.audio ? artifactAudioPlayLabel : artifactVideoPlayLabel);
|
||||
final icon = cupertino
|
||||
? (value.isPlaying
|
||||
? CupertinoIcons.pause_fill
|
||||
: CupertinoIcons.play_fill)
|
||||
: (value.isPlaying ? Icons.pause : Icons.play_arrow);
|
||||
return Column(
|
||||
key: const Key('artifact-video'),
|
||||
key: Key(widget.audio ? 'artifact-audio' : 'artifact-video'),
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Flexible(
|
||||
child: AspectRatio(
|
||||
aspectRatio: value.aspectRatio,
|
||||
child: VideoPlayer(_controller),
|
||||
if (!widget.audio)
|
||||
Flexible(
|
||||
child: AspectRatio(
|
||||
aspectRatio: value.aspectRatio,
|
||||
child: VideoPlayer(_controller),
|
||||
),
|
||||
)
|
||||
else
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(24),
|
||||
child: Icon(Icons.audio_file_outlined, size: 64),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
child: Row(
|
||||
|
||||
@@ -30,6 +30,7 @@ class ArtifactViewerBar extends StatelessWidget {
|
||||
required this.count,
|
||||
required this.fetch,
|
||||
required this.fileBridge,
|
||||
this.downloadResolved,
|
||||
required this.onOpen,
|
||||
required this.onCopy,
|
||||
required this.onPrevious,
|
||||
@@ -44,6 +45,7 @@ class ArtifactViewerBar extends StatelessWidget {
|
||||
final int count;
|
||||
final ArtifactFetch fetch;
|
||||
final ArtifactFileBridge fileBridge;
|
||||
final ResolvedArtifactUrl? downloadResolved;
|
||||
final VoidCallback onOpen;
|
||||
final VoidCallback onCopy;
|
||||
final VoidCallback onPrevious;
|
||||
@@ -123,7 +125,7 @@ class ArtifactViewerBar extends StatelessWidget {
|
||||
'${page.group.visit}\u0000${page.artifact.url}',
|
||||
),
|
||||
artifact: page.artifact,
|
||||
resolved: resolved,
|
||||
resolved: downloadResolved ?? resolved,
|
||||
fetch: fetch,
|
||||
bridge: fileBridge,
|
||||
compact: true,
|
||||
|
||||
@@ -8,6 +8,7 @@ import '../../../ui/adaptive/floating_bar_layout.dart';
|
||||
import '../actions/artifact_file_actions.dart';
|
||||
import '../actions/artifact_file_bridge.dart';
|
||||
import '../domain/artifact_preview.dart';
|
||||
import '../domain/artifact_url.dart';
|
||||
import '../domain/gallery_navigation.dart';
|
||||
import 'artifact_html_view.dart';
|
||||
import 'artifact_viewer_bar.dart';
|
||||
@@ -39,6 +40,7 @@ class ArtifactViewerBody extends StatelessWidget {
|
||||
required this.onCopy,
|
||||
this.fetch = fetchArtifact,
|
||||
this.fileBridge = const ArtifactFileBridge(),
|
||||
this.downloadResolved,
|
||||
this.text,
|
||||
this.bytes,
|
||||
this.error,
|
||||
@@ -73,6 +75,7 @@ class ArtifactViewerBody extends StatelessWidget {
|
||||
final VoidCallback onCopy;
|
||||
final ArtifactFetch fetch;
|
||||
final ArtifactFileBridge fileBridge;
|
||||
final ResolvedArtifactUrl? downloadResolved;
|
||||
final String? text;
|
||||
final Uint8List? bytes;
|
||||
final String? error;
|
||||
@@ -138,6 +141,7 @@ class ArtifactViewerBody extends StatelessWidget {
|
||||
count: count,
|
||||
fetch: fetch,
|
||||
fileBridge: fileBridge,
|
||||
downloadResolved: downloadResolved,
|
||||
onOpen: onOpen,
|
||||
onCopy: onCopy,
|
||||
onPrevious: onPrevious,
|
||||
|
||||
@@ -111,6 +111,14 @@ class ArtifactPreviewBody extends StatelessWidget {
|
||||
controllerFactory: videoControllerFactory,
|
||||
),
|
||||
);
|
||||
case ArtifactPreviewKind.audio:
|
||||
return Center(
|
||||
child: ArtifactVideoPlayer(
|
||||
uri: plan.resolved!.uri,
|
||||
controllerFactory: videoControllerFactory,
|
||||
audio: true,
|
||||
),
|
||||
);
|
||||
case ArtifactPreviewKind.unsupported:
|
||||
case ArtifactPreviewKind.unavailable:
|
||||
return const SizedBox.shrink();
|
||||
|
||||
@@ -7,6 +7,7 @@ import '../../../ui/input/arrow_key_pager.dart';
|
||||
import 'artifact_selection_facts.dart';
|
||||
import '../actions/artifact_file_bridge.dart';
|
||||
import '../domain/artifact_groups.dart';
|
||||
import '../domain/artifact_kind.dart';
|
||||
import '../domain/artifact_preview.dart';
|
||||
import '../domain/artifact_preview_failure.dart';
|
||||
import '../domain/artifact_url.dart';
|
||||
@@ -43,6 +44,8 @@ class ArtifactViewerPage extends StatefulWidget {
|
||||
required this.selectedIdentity,
|
||||
this.includeExternalDocuments = false,
|
||||
this.configuredEndpoint = '',
|
||||
this.downloadUrl,
|
||||
this.previewFormat = artifactPreviewFormat,
|
||||
this.fetch = fetchArtifact,
|
||||
this.markdownBuilder = defaultArtifactMarkdown,
|
||||
this.openExternal = openArtifactExternally,
|
||||
@@ -60,6 +63,11 @@ class ArtifactViewerPage extends StatefulWidget {
|
||||
final bool includeExternalDocuments;
|
||||
|
||||
final String configuredEndpoint;
|
||||
final String? downloadUrl;
|
||||
|
||||
/// The preview rule; the Files panel passes its own (every HTML previews).
|
||||
final ArtifactPreviewFormat Function(String contentType, String name)
|
||||
previewFormat;
|
||||
final ArtifactFetch fetch;
|
||||
final ArtifactMarkdownBuilder markdownBuilder;
|
||||
final ArtifactExternalOpen openExternal;
|
||||
@@ -110,6 +118,7 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage>
|
||||
return planArtifactPreview(
|
||||
artifact: page.artifact,
|
||||
configuredEndpoint: widget.configuredEndpoint,
|
||||
format: widget.previewFormat,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -224,6 +233,9 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage>
|
||||
Widget buildFrame(BuildContext context, {required bool fullscreen}) {
|
||||
final page = _page;
|
||||
final plan = _plan;
|
||||
final downloadResolved = widget.downloadUrl == null
|
||||
? null
|
||||
: resolveArtifactUrl(widget.configuredEndpoint, widget.downloadUrl!);
|
||||
final title = page == null
|
||||
? artifactViewerTitle
|
||||
: artifactDisplayName(page.artifact);
|
||||
@@ -266,6 +278,7 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage>
|
||||
status: _status,
|
||||
fetch: widget.fetch,
|
||||
fileBridge: widget.fileBridge,
|
||||
downloadResolved: downloadResolved,
|
||||
onPrevious: () => _step(-1),
|
||||
onNext: () => _step(1),
|
||||
// A failure is never cached, so loading again fetches afresh.
|
||||
|
||||
Reference in New Issue
Block a user