mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-12-16 17:14:36 +01:00
(feat:background) Fix Rust-based file download & Create the 'bg' directory on app setup
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user