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

View File

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