mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-12-13 07:34:36 +01:00
fix download speed for downloads without a known size
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user