Files
pipelines/app/lib/features/chat/files/files_viewer.dart
T
alan 339cce0424 Page through a folder's files in the file viewer
Opening a file from the Files panel now passes the folder's previewable
files in their shown order, so the arrows, swipes and Previous/Next step
through them and Escape closes the viewer. Download follows the file on
screen.
2026-11-10 00:14:18 +00:00

175 lines
5.9 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../../artifacts/actions/artifact_file_bridge.dart';
import '../../artifacts/actions/artifact_transfer.dart';
import '../../artifacts/domain/artifact_groups.dart';
import '../../artifacts/domain/artifact_kind.dart';
import '../../artifacts/domain/artifact_record.dart';
import '../../artifacts/domain/gallery_navigation.dart';
import '../../artifacts/ui/artifact_external_open.dart';
import '../../artifacts/ui/artifact_viewer_fetch.dart';
import '../../artifacts/ui/artifact_viewer_page.dart';
import '../data/directory_listing.dart';
import '../ui/transcript/chat_markdown_view.dart';
import 'files_download.dart';
import 'files_details.dart';
import 'files_preview.dart';
import 'files_urls.dart';
/// Opens and downloads one working-directory file from the Files panel.
///
/// Previewable files (images, video, audio, markdown, text, tables, HTML) open
/// in the shared artifact viewer over the inline `/files` url; a PDF goes to
/// the host's external handler; anything else gets a sheet with Download and
/// Open externally. The host capabilities are injectable so tests never fetch.
class FilesViewer {
const FilesViewer({
this.bridge = const ArtifactFileBridge(),
this.fetch = fetchArtifact,
this.openExternal = openArtifactExternally,
this.launch = launchFileUrl,
});
final ArtifactFileBridge bridge;
final ArtifactFetch fetch;
final ArtifactExternalOpen openExternal;
final FileUrlLauncher launch;
/// Opens [entry]. [siblings] are the folder's entries in the order shown;
/// the viewer then steps through every previewable one with the arrows,
/// swipes and Previous/Next, and Escape closes it.
Future<void> open(
BuildContext context, {
required String endpoint,
required DirectoryEntry entry,
List<DirectoryEntry> siblings = const [],
}) async {
final urls = resolveFileUrls(endpoint, entry.path);
if (urls == null) {
_showSheet(context, entry, null);
return;
}
final type = fileContentType(entry.name);
if (artifactKind(type, entry.name) == ArtifactKind.pdf) {
var opened = false;
try {
opened = await openExternal(urls.view);
} catch (_) {
/* show fallback actions below */
}
if (!opened && context.mounted) _showSheet(context, entry, urls);
return;
}
if (!isPreviewableFile(entry.name)) {
_showSheet(context, entry, urls);
return;
}
ArtifactRecord? record(DirectoryEntry file) {
if (file.isDir || !isPreviewableFile(file.name)) return null;
final links = resolveFileUrls(endpoint, file.path);
if (links == null) return null;
final kind = fileContentType(file.name);
if (artifactKind(kind, file.name) == ArtifactKind.pdf) return null;
return ArtifactRecord(
name: file.name,
url: links.view.toString(),
contentType: kind,
sizeBytes: file.size ?? 0,
raw: {'download_url': links.download.toString()},
);
}
final artifact = record(entry)!;
final pages = [
for (final file in siblings)
if (file.path == entry.path) artifact else ?record(file),
];
if (!pages.contains(artifact)) pages.add(artifact);
final group = ArtifactGroup(stageKey: 'Files', visit: 1, artifacts: pages);
final page = ArtifactViewerPage(
groups: [group],
selectedIdentity: artifactVisitIdentityOf(group, artifact),
configuredEndpoint: endpoint,
downloadUrl: urls.download.toString(),
previewFormat: filePreviewFormat,
markdownBuilder: (context, text) => ChatMarkdownView(text: text),
fetch: fetch,
openExternal: openExternal,
fileBridge: bridge,
);
if (!context.mounted) return;
Navigator.of(context).push<void>(
usesCupertinoHost(context)
? CupertinoPageRoute<void>(builder: (_) => page)
: MaterialPageRoute<void>(builder: (_) => page),
);
}
/// Downloads [entry] straight from a row or tile menu and reports the
/// outcome (a snack bar, or an alert for a failure on a host without one);
/// without urls the sheet explains instead.
Future<void> download(
BuildContext context, {
required String endpoint,
required DirectoryEntry entry,
}) async {
final urls = resolveFileUrls(endpoint, entry.path);
if (urls == null) {
_showSheet(context, entry, null);
return;
}
final messenger = ScaffoldMessenger.maybeOf(context);
final result = await _download(entry, urls);
if (result.message.isEmpty) return;
if (messenger != null) {
messenger.showSnackBar(SnackBar(content: Text(result.message)));
return;
}
// A Cupertino host has no snack bar; a failure still has to be said.
if (result.state != ArtifactTransferState.failed || !context.mounted) {
return;
}
await showAdaptiveDialog<void>(
context: context,
builder: (dialog) => AlertDialog.adaptive(
title: Text(entry.name),
content: Text(result.message),
actions: [
if (usesCupertinoHost(dialog))
CupertinoDialogAction(
onPressed: () => Navigator.pop(dialog),
child: const Text('OK'),
)
else
TextButton(
onPressed: () => Navigator.pop(dialog),
child: const Text('OK'),
),
],
),
);
}
Future<ArtifactTransferResult> _download(
DirectoryEntry entry,
FileUrls urls,
) => downloadFile(
entry: entry,
urls: urls,
bridge: bridge,
fetch: fetch,
launch: launch,
);
void _showSheet(BuildContext context, DirectoryEntry entry, FileUrls? urls) =>
showFileDetails(
context,
entry: entry,
urls: urls,
download: urls == null ? null : () => _download(entry, urls),
openExternal: openExternal,
);
}