From 09b7922f31a76e2584fb295fbe586b81486a7c1b Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Sun, 5 Jun 2022 22:14:09 -0700 Subject: [PATCH] fix download speed for downloads without a known size --- src-tauri/src/downloader.rs | 8 ++++++++ src/utils/download.ts | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/downloader.rs b/src-tauri/src/downloader.rs index 1883170..be6708a 100644 --- a/src-tauri/src/downloader.rs +++ b/src-tauri/src/downloader.rs @@ -41,6 +41,7 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu } }; let mut downloaded: u64 = 0; + let mut total_downloaded: u64 = 0; // File stream let mut stream = res.bytes_stream(); @@ -77,6 +78,8 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu let new = min(downloaded + (chunk.len() as u64), total_size); downloaded = new; + total_downloaded = total_downloaded + chunk.len() as u64; + let mut res_hash = std::collections::HashMap::new(); res_hash.insert( @@ -94,6 +97,11 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu path.to_string(), ); + res_hash.insert( + "total_downloaded".to_string(), + total_downloaded.to_string(), + ); + // Create event to send to frontend window.emit("download_progress", &res_hash).unwrap(); } diff --git a/src/utils/download.ts b/src/utils/download.ts index 680bacd..f7de7ec 100644 --- a/src/utils/download.ts +++ b/src/utils/download.ts @@ -7,6 +7,7 @@ export default class DownloadHandler { path: string, progress: number, total: number, + total_downloaded: number, status: string, startTime: number, error?: string, @@ -24,16 +25,25 @@ export default class DownloadHandler { downloaded: string, total: string, path: string, + total_downloaded: string, } = payload const index = this.downloads.findIndex(download => download.path === obj.path) this.downloads[index].progress = parseInt(obj.downloaded, 10) this.downloads[index].total = parseInt(obj.total, 10) + this.downloads[index].total_downloaded = parseInt(obj.total_downloaded, 10) // Set download speed based on startTime const now = Date.now() const timeDiff = now - this.downloads[index].startTime - const speed = (this.downloads[index].progress / timeDiff) * 1000 + let speed = (this.downloads[index].progress / timeDiff) * 1000 + + if (this.downloads[index].total === 0) { + // If our total is 0, then we are downloading a file without a size + // Calculate the average speed based total_downloaded and startTme + speed = (this.downloads[index].total_downloaded / timeDiff) * 1000 + } + this.downloads[index].speed = byteToString(speed) + '/s' }) @@ -104,6 +114,7 @@ export default class DownloadHandler { path, progress: 0, total: 0, + total_downloaded: 0, status: 'downloading', startTime: Date.now(), onFinish, @@ -134,7 +145,7 @@ export default class DownloadHandler { getTotalAverage() { const files = this.downloads.filter(d => d.status === 'downloading') const total = files.reduce((acc, d) => acc + d.total, 0) - const progress = files.reduce((acc, d) => acc + d.progress, 0) + const progress = files.reduce((acc, d) => d.progress !== 0 ? acc + d.progress : acc + d.total_downloaded, 0) let speedStr = '0 B/s' // Get download speed based on startTimes