f21365f609
Keep the Material-only file explorer path while preserving public APIs used by callers. Remove unreachable Cupertino widgets, hosts, routes, and tests so the app remains behaviorally unchanged on Material screens.
132 lines
3.9 KiB
Dart
132 lines
3.9 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
import '../../artifacts/actions/artifact_file.dart';
|
|
import '../../artifacts/actions/artifact_file_bridge.dart';
|
|
import '../../artifacts/actions/artifact_transfer.dart';
|
|
import '../../artifacts/ui/artifact_viewer_fetch.dart';
|
|
import '../data/directory_listing.dart';
|
|
import 'files_preview.dart';
|
|
import 'files_urls.dart';
|
|
|
|
const String fileDownloadFailed = 'Could not download the file. Try again.';
|
|
|
|
/// Opens an attachment url in the browser (web only); tests inject a recorder.
|
|
typedef FileUrlLauncher = Future<bool> Function(Uri url);
|
|
|
|
Future<bool> launchFileUrl(Uri url) =>
|
|
launchUrl(url, mode: LaunchMode.externalApplication);
|
|
|
|
/// Downloads one working-directory file.
|
|
///
|
|
/// On the web the browser handles the `download=1` url itself: the attachment
|
|
/// disposition makes it save the file with its own name, so no bytes pass
|
|
/// through the app. Everywhere else the bytes are fetched from the inline url
|
|
/// and handed to the host through the artifact file bridge (system "Save as"),
|
|
/// exactly as an artifact download is.
|
|
Future<ArtifactTransferResult> downloadFile({
|
|
required DirectoryEntry entry,
|
|
required FileUrls urls,
|
|
ArtifactFileBridge bridge = const ArtifactFileBridge(),
|
|
ArtifactFetch fetch = fetchArtifact,
|
|
FileUrlLauncher launch = launchFileUrl,
|
|
}) async {
|
|
try {
|
|
if (kIsWeb) {
|
|
final opened = await launch(urls.download);
|
|
return opened
|
|
? ArtifactTransferResult(
|
|
ArtifactTransferState.saved,
|
|
'Downloading ${entry.name}.',
|
|
)
|
|
: const ArtifactTransferResult(
|
|
ArtifactTransferState.failed,
|
|
fileDownloadFailed,
|
|
);
|
|
}
|
|
final ArtifactFetchResult result;
|
|
try {
|
|
result = await fetch(urls.view);
|
|
} on ArtifactFetchException {
|
|
return const ArtifactTransferResult(
|
|
ArtifactTransferState.failed,
|
|
fileDownloadFailed,
|
|
);
|
|
}
|
|
if (!result.ok) {
|
|
return const ArtifactTransferResult(
|
|
ArtifactTransferState.failed,
|
|
fileDownloadFailed,
|
|
);
|
|
}
|
|
return await bridge.saveFile(
|
|
ArtifactFile(
|
|
name: entry.name,
|
|
mimeType: fileContentType(entry.name),
|
|
bytes: result.bytes,
|
|
),
|
|
);
|
|
} catch (_) {
|
|
return const ArtifactTransferResult(
|
|
ArtifactTransferState.failed,
|
|
fileDownloadFailed,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The Download button of the unviewable-file sheet, with its outcome below.
|
|
class FileDownloadButton extends StatefulWidget {
|
|
const FileDownloadButton({super.key, required this.download});
|
|
|
|
/// Runs the transfer; the sheet's owner binds the entry, urls and bridge.
|
|
final Future<ArtifactTransferResult> Function() download;
|
|
|
|
@override
|
|
State<FileDownloadButton> createState() => _FileDownloadButtonState();
|
|
}
|
|
|
|
class _FileDownloadButtonState extends State<FileDownloadButton> {
|
|
bool _busy = false;
|
|
String? _status;
|
|
|
|
Future<void> _download() async {
|
|
if (_busy) return;
|
|
setState(() {
|
|
_busy = true;
|
|
_status = null;
|
|
});
|
|
String status;
|
|
try {
|
|
status = (await widget.download()).message;
|
|
} catch (_) {
|
|
status = fileDownloadFailed;
|
|
}
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_busy = false;
|
|
_status = status.isEmpty ? null : status;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
FilledButton.icon(
|
|
onPressed: _busy ? null : _download,
|
|
icon: _busy
|
|
? const SizedBox(
|
|
width: 18,
|
|
height: 18,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(Icons.download_outlined),
|
|
label: const Text('Download'),
|
|
),
|
|
if (_status != null)
|
|
Padding(padding: const EdgeInsets.only(top: 8), child: Text(_status!)),
|
|
],
|
|
);
|
|
}
|