feat: show real download progress in the files sidebar

- the download dialog starts from the size fs/stat reported, so the bar is
  determinate even when the response has no Content-Length (the relay
  forwards the body chunked); the server's own total wins when it has one
- the line reads "512.0 KB / 20.0 MB (2.5%)": below 10% one decimal
  shows that the transfer is moving
- bump to 0.5.4
This commit is contained in:
blumia committed 2026-11-26 12:46:25 +08:00
1 parent 511c3b006b
commit 545778c6f3
4 files changed
+37 -6

No files matched your search

+25 -4
View File
@@ -93,6 +93,11 @@ class _FilesPanelState extends State<FilesPanel> {
bool _previewLoading = false;
String? _previewError;
Uint8List? _previewBytes;
/// The file's size from `fs/stat`, even when too large to preview: the
/// download dialog shows a determinate bar against it when the response
/// carries no Content-Length (the relay forwards bodies chunked).
int? _previewSize;
String? _previewText;
bool _previewBinary = false;
bool _previewImage = false;
@@ -231,6 +236,7 @@ class _FilesPanelState extends State<FilesPanel> {
_previewLoading = true;
_previewError = null;
_previewBytes = null;
_previewSize = null;
_previewText = null;
_previewBinary = false;
_previewImage = false;
@@ -240,6 +246,9 @@ class _FilesPanelState extends State<FilesPanel> {
if (client == null) return;
try {
final stat = await client.fsStat(entry.path, directory: _root);
if (stat != null && stat.size > 0 && mounted) {
setState(() => _previewSize = stat.size);
}
if (stat != null && stat.size > 2 * 1024 * 1024) {
if (!mounted || _previewPath != entry.path) return;
setState(() {
@@ -305,6 +314,7 @@ class _FilesPanelState extends State<FilesPanel> {
barrierDismissible: false,
builder: (_) => _DownloadDialog(
name: _previewName,
expectedSize: _previewSize,
start: (onProgress, cancel) => client.downloadFile(path,
directory: _root, onProgress: onProgress, cancel: cancel),
),
@@ -941,11 +951,16 @@ class _FilesPanelState extends State<FilesPanel> {
/// the dialog closes with no bytes, so nothing is saved.
class _DownloadDialog extends StatefulWidget {
final String name;
/// The file's size from `fs/stat`; the progress bar is determinate against
/// it from the first byte when the response has no Content-Length.
final int? expectedSize;
final Future<Uint8List> Function(
void Function(int received, int? total) onProgress,
CancelToken cancel) start;
const _DownloadDialog({required this.name, required this.start});
const _DownloadDialog(
{required this.name, this.expectedSize, required this.start});
@override
State<_DownloadDialog> createState() => _DownloadDialogState();
@@ -962,6 +977,7 @@ class _DownloadDialogState extends State<_DownloadDialog> {
@override
void initState() {
super.initState();
_total = (widget.expectedSize ?? 0) > 0 ? widget.expectedSize : null;
_run();
}
@@ -969,14 +985,17 @@ class _DownloadDialogState extends State<_DownloadDialog> {
try {
final bytes = await widget.start((received, total) {
if (!mounted) return;
// The server's own total is more accurate than the stat we started
// with; keep the latter when the response is chunked.
if (total != null) _total = total;
// Chunks arrive every few dozen KB; repaint a few times a second at
// most, and always on the last one.
final now = DateTime.now().millisecondsSinceEpoch;
if (total != null && received < total && now - _lastPaint < 80) return;
final known = _total;
if (known != null && received < known && now - _lastPaint < 80) return;
_lastPaint = now;
setState(() {
_received = received;
_total = total;
});
}, _cancel);
if (mounted) Navigator.of(context).pop(bytes);
@@ -1020,7 +1039,9 @@ class _DownloadDialogState extends State<_DownloadDialog> {
? trArgs('{0} / {1} ({2}%)', [
_size(_received),
_size(total),
(fraction! * 100).toStringAsFixed(0),
// Below 10% one decimal says the transfer is moving;
// whole numbers are enough above it.
(fraction! * 100).toStringAsFixed(fraction < 0.1 ? 1 : 0),
])
: trArgs('Downloaded {0}', [_size(_received)]),
style: TextStyle(fontSize: 12, color: theme.hintColor),