This commit is contained in:
SpikeHD
2022-05-09 18:32:55 -07:00
parent 6aacba76c6
commit 094b8ab7d3
2 changed files with 15 additions and 5 deletions

View File

@@ -15,7 +15,9 @@ use tauri::{
#[tauri::command]
pub async fn download_file(url: &str, path: &str) -> Result<(), String> {
// Reqwest setup
let res = reqwest::get(url).await.or(Err(format!("Failed to get {}", url)))?;
let res = reqwest::get(url)
.await
.or(Err(format!("Failed to get {}", url)))?;
let total_size = res
.content_length()
.ok_or(format!("Failed to get content length from '{}'", &url))?;
@@ -29,15 +31,22 @@ pub async fn download_file(url: &str, path: &str) -> Result<(), String> {
// Await chunks
while let Some(item) = stream.next().await {
let chunk = item.or(Err(format!("Error while downloading file")));
// Write chunk
file.write_all(&chunk)
.or(Err(format!("Error while writing to file")))?;
let chunk = item.or(Err(format!("Error while downloading file"))).unwrap();
let vect = &chunk.to_vec()[..];
// Write bytes
file.write_all(&vect)
.or(Err(format!("Error while writing file")))?;
// New progress
let new = min(downloaded + (chunk.len() as u64), total_size);
downloaded = new;
// Create event to send to frontend
}
// One more "finish" event
// We are done
return Ok(());
}

View File

@@ -3,6 +3,7 @@
windows_subsystem = "windows"
)]
mod downloader;
mod proxy;
use tauri::{