diff --git a/app/lib/features/artifacts/ui/artifact_viewer_page.dart b/app/lib/features/artifacts/ui/artifact_viewer_page.dart index 7d3dc30c..bc06aef7 100644 --- a/app/lib/features/artifacts/ui/artifact_viewer_page.dart +++ b/app/lib/features/artifacts/ui/artifact_viewer_page.dart @@ -63,6 +63,10 @@ class ArtifactViewerPage extends StatefulWidget { final bool includeExternalDocuments; final String configuredEndpoint; + + /// Where Download fetches the shown file. An artifact whose `raw` carries + /// `download_url` (the Files panel's per-file links) overrides it, so paging + /// through a folder downloads the file on screen. final String? downloadUrl; /// The preview rule; the Files panel passes its own (every HTML previews). @@ -235,9 +239,13 @@ class _ArtifactViewerPageState extends State Widget buildFrame(BuildContext context, {required bool fullscreen}) { final page = _page; final plan = _plan; - final downloadResolved = widget.downloadUrl == null + final pageDownload = page?.artifact.raw['download_url']; + final download = pageDownload is String && pageDownload.isNotEmpty + ? pageDownload + : widget.downloadUrl; + final downloadResolved = download == null ? null - : resolveArtifactUrl(widget.configuredEndpoint, widget.downloadUrl!); + : resolveArtifactUrl(widget.configuredEndpoint, download); final title = page == null ? artifactViewerTitle : artifactDisplayName(page.artifact); diff --git a/app/lib/features/chat/files/files_panel.dart b/app/lib/features/chat/files/files_panel.dart index 6767ecc8..a864c59c 100644 --- a/app/lib/features/chat/files/files_panel.dart +++ b/app/lib/features/chat/files/files_panel.dart @@ -217,7 +217,12 @@ class _FilesPanelState extends State { if (widget.onOpen != null) { widget.onOpen!(entry); } else { - widget.viewer.open(context, endpoint: _endpoint, entry: entry); + widget.viewer.open( + context, + endpoint: _endpoint, + entry: entry, + siblings: widget.controller.entries, + ); } } } diff --git a/app/lib/features/chat/files/files_viewer.dart b/app/lib/features/chat/files/files_viewer.dart index ccdbcc92..65cd8024 100644 --- a/app/lib/features/chat/files/files_viewer.dart +++ b/app/lib/features/chat/files/files_viewer.dart @@ -37,10 +37,14 @@ class FilesViewer { 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 open( BuildContext context, { required String endpoint, required DirectoryEntry entry, + List siblings = const [], }) async { final urls = resolveFileUrls(endpoint, entry.path); if (urls == null) { @@ -62,18 +66,28 @@ class FilesViewer { _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], - ); + 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), diff --git a/app/test/features/chat/files/files_panel_test.dart b/app/test/features/chat/files/files_panel_test.dart index 87a6c4df..0ace8ee9 100644 --- a/app/test/features/chat/files/files_panel_test.dart +++ b/app/test/features/chat/files/files_panel_test.dart @@ -1,5 +1,6 @@ import 'dart:convert'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter/cupertino.dart'; import 'package:pipelines_app/features/chat/files/files_preview.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_kind.dart'; @@ -324,4 +325,45 @@ void main() { expect(find.text("Couldn't load this folder."), findsOneWidget); expect(find.textContaining('internal transport'), findsNothing); }); + + testWidgets('arrows step through the folder\'s previewable files and ' + 'Escape closes the viewer', (tester) async { + final fetched = []; + await pump( + tester, + viewer: FilesViewer( + fetch: (url) async { + fetched.add(url.queryParameters['path'] ?? ''); + return ArtifactFetchResult( + status: 200, + bytes: url.queryParameters['path']!.endsWith('.png') + ? base64Decode( + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==', + ) + : utf8.encode('A text file preview'), + ); + }, + ), + ); + Finder shown(String name) => find.descendant( + of: find.byType(ArtifactViewerPage), + matching: find.text(name), + ); + await tester.tap(find.text('notes.txt')); + await tester.pumpAndSettle(); + expect(find.byType(ArtifactViewerPage), findsOneWidget); + expect(shown('notes.txt'), findsWidgets); + // The zip and PDF are not previewable, so the next file is the image. + await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight); + await tester.pumpAndSettle(); + expect(shown('picture.png'), findsWidgets); + expect(fetched, contains('/home/demo/picture.png')); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft); + await tester.pumpAndSettle(); + expect(shown('notes.txt'), findsWidgets); + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pumpAndSettle(); + expect(find.byType(ArtifactViewerPage), findsNothing); + expect(find.text('notes.txt'), findsOneWidget); + }); }