mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-12-14 16:14:48 +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 downloaded: u64 = 0;
|
||||||
|
let mut total_downloaded: u64 = 0;
|
||||||
|
|
||||||
// File stream
|
// File stream
|
||||||
let mut stream = res.bytes_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);
|
let new = min(downloaded + (chunk.len() as u64), total_size);
|
||||||
downloaded = new;
|
downloaded = new;
|
||||||
|
|
||||||
|
total_downloaded = total_downloaded + chunk.len() as u64;
|
||||||
|
|
||||||
let mut res_hash = std::collections::HashMap::new();
|
let mut res_hash = std::collections::HashMap::new();
|
||||||
|
|
||||||
res_hash.insert(
|
res_hash.insert(
|
||||||
@@ -94,6 +97,11 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu
|
|||||||
path.to_string(),
|
path.to_string(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
res_hash.insert(
|
||||||
|
"total_downloaded".to_string(),
|
||||||
|
total_downloaded.to_string(),
|
||||||
|
);
|
||||||
|
|
||||||
// Create event to send to frontend
|
// Create event to send to frontend
|
||||||
window.emit("download_progress", &res_hash).unwrap();
|
window.emit("download_progress", &res_hash).unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export default class DownloadHandler {
|
|||||||
path: string,
|
path: string,
|
||||||
progress: number,
|
progress: number,
|
||||||
total: number,
|
total: number,
|
||||||
|
total_downloaded: number,
|
||||||
status: string,
|
status: string,
|
||||||
startTime: number,
|
startTime: number,
|
||||||
error?: string,
|
error?: string,
|
||||||
@@ -24,16 +25,25 @@ export default class DownloadHandler {
|
|||||||
downloaded: string,
|
downloaded: string,
|
||||||
total: string,
|
total: string,
|
||||||
path: string,
|
path: string,
|
||||||
|
total_downloaded: string,
|
||||||
} = payload
|
} = payload
|
||||||
|
|
||||||
const index = this.downloads.findIndex(download => download.path === obj.path)
|
const index = this.downloads.findIndex(download => download.path === obj.path)
|
||||||
this.downloads[index].progress = parseInt(obj.downloaded, 10)
|
this.downloads[index].progress = parseInt(obj.downloaded, 10)
|
||||||
this.downloads[index].total = parseInt(obj.total, 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
|
// Set download speed based on startTime
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
const timeDiff = now - this.downloads[index].startTime
|
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'
|
this.downloads[index].speed = byteToString(speed) + '/s'
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -104,6 +114,7 @@ export default class DownloadHandler {
|
|||||||
path,
|
path,
|
||||||
progress: 0,
|
progress: 0,
|
||||||
total: 0,
|
total: 0,
|
||||||
|
total_downloaded: 0,
|
||||||
status: 'downloading',
|
status: 'downloading',
|
||||||
startTime: Date.now(),
|
startTime: Date.now(),
|
||||||
onFinish,
|
onFinish,
|
||||||
@@ -134,7 +145,7 @@ export default class DownloadHandler {
|
|||||||
getTotalAverage() {
|
getTotalAverage() {
|
||||||
const files = this.downloads.filter(d => d.status === 'downloading')
|
const files = this.downloads.filter(d => d.status === 'downloading')
|
||||||
const total = files.reduce((acc, d) => acc + d.total, 0)
|
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'
|
let speedStr = '0 B/s'
|
||||||
|
|
||||||
// Get download speed based on startTimes
|
// Get download speed based on startTimes
|
||||||
|
|||||||
Reference in New Issue
Block a user