increase download chunk to 1MB, add human readable bytes format presentation

This commit is contained in:
yggverse 2024-12-11 10:57:21 +02:00
parent cc1597bbb7
commit c53846649c

View File

@ -497,16 +497,19 @@ impl Page {
cancellable.clone(),
Priority::DEFAULT,
(
0x400, // 1024 bytes per chunk
None, // unlimited
0 // initial totals
0x100000, // 1M bytes per chunk
None, // unlimited
0 // initial totals
),
(
// on chunk
{
let action = action.clone();
move |_, total| action.update.activate(
&format!("Received {total} bytes...")
&format!(
"Received {}...",
format_bytes(total)
)
)
},
// on complete
@ -969,3 +972,16 @@ fn snap_history(navigation: Rc<Navigation>) {
navigation.history.add(request, true)
}
}
/// Format bytes to KB/MB/GB presentation
fn format_bytes(total: usize) -> String {
if total < 1024 {
format!("{} bytes", total)
} else if total < 1024 * 1024 {
format!("{:.2} KB", total as f64 / 1024.0)
} else if total < 1024 * 1024 * 1024 {
format!("{:.2} MB", total as f64 / (1024.0 * 1024.0))
} else {
format!("{:.2} GB", total as f64 / (1024.0 * 1024.0 * 1024.0))
}
}