(feat:background) Fix Rust-based file download & Create the 'bg' directory on app setup

This commit is contained in:
KingRainbow44
2023-11-29 23:48:29 -05:00
parent 41c074e462
commit ee4b5ad0d9
4 changed files with 1219 additions and 25 deletions

View File

@@ -1,4 +1,3 @@
use core::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::Path;
@@ -9,17 +8,20 @@ use std::path::Path;
/// url: The URL to download from.
/// to_file: The file to save to.
#[tauri::command]
pub async fn download_file(url: String, to_file: String) -> Result<String, Box<dyn Error>> {
let mut response = reqwest::get(&url).await?;
pub async fn download_file(url: String, to_file: String) -> Result<String, String> {
let mut response = reqwest::get(&url).await
.expect("Failed to send request");
let mut dest = {
let fname = Path::new(&to_file);
match File::create(&fname) {
Ok(f) => f,
Err(e) => return Err(Box::new(e)),
Err(_) => return Err("Failed to create file".to_string()),
}
};
while let Some(chunk) = response.chunk().await? {
dest.write_all(&chunk).await?;
while let Some(chunk) = response.chunk().await
.expect("Unable to read chunk") {
dest.write_all(&chunk)
.expect("Failed to write chunk to file")
}
Ok("Downloaded".to_string())
}