Files
pipelines/app/lib/features/chat/files/files_viewer.dart
T

161 lines
5.1 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;
Future<void> open(
BuildContext context, {
required String endpoint,
required DirectoryEntry entry,
}) 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;
}
final artifact = ArtifactRecord(
name: entry.name,
url: urls.view.toString(),
contentType: type,
sizeBytes: entry.size ?? 0,
raw: const {},
);
final group = ArtifactGroup(
stageKey: 'Files',
visit: 1,
artifacts: [artifact],
);
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,
);
}