diff --git a/src/main.rs b/src/main.rs index 764139d..b90cfbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ use crate::server::server::start; use crate::terms::consent_state; use crate::users::user_manager; use crate::util::config_util::CONFIG; +use crate::util::file_util::download_and_extract_zip; use crate::util::file_util::has_dir; pub static APP_STATE: LazyLock>> = @@ -135,11 +136,11 @@ async fn main() { } } if !has_dir("web") { - /*download_and_extract_zip( - "weblink", + download_and_extract_zip( + "https://omega.tensamin.net/api/download/iota_frontend", "web", ) - .await;*/ + .await; } loop { if *SHUTDOWN.read().await { diff --git a/src/util/file_util.rs b/src/util/file_util.rs index c27f9f9..1936e03 100644 --- a/src/util/file_util.rs +++ b/src/util/file_util.rs @@ -1,6 +1,6 @@ use std::ffi::OsStr; use std::fs::{self, File}; -use std::io::{self, BufReader, Read}; +use std::io::{self, BufReader, Read, Write}; use std::path::{Path, PathBuf}; use sysinfo::System; use uuid::Uuid; @@ -249,19 +249,22 @@ pub fn get_used_ram() -> String { format!("{}/{}", design_byte(used), design_byte(total)) } -// Helper to download the zip file content to a file on disk -#[allow(dead_code)] -async fn download_zip(url: &str, as_name: &Path) -> Result<(), Box> { - let response = reqwest::get(url).await?; +use reqwest::Client; + +pub async fn download_zip(url: &str, zip_path: &Path) -> Result<(), Box> { + let client = Client::new(); + + let response = client.get(url).send().await?; - // Check for successful response status if !response.status().is_success() { - return Err(format!("Failed to download file: Status {}", response.status()).into()); + return Err(format!("Download failed: {}", response.status()).into()); } - let mut zip_file = File::create(as_name)?; - let body = response.bytes().await?; - io::copy(&mut &*body, &mut zip_file)?; + let bytes = response.bytes().await?; + + let mut file = File::create(zip_path)?; + file.write_all(&bytes)?; + file.flush()?; Ok(()) } @@ -343,30 +346,30 @@ fn extract_zip_contents_to_folder( Ok(()) } -#[allow(dead_code)] pub async fn download_and_extract_zip(url: &str, as_name: &str) { let base_dir = PathBuf::from(get_directory()); - let zip_filename = format!("{}.zip", Uuid::new_v4()); // Use a unique name for the downloaded ZIP file - let zip_path = base_dir.join(&zip_filename); + let zip_path = base_dir.join(format!("{}.zip", Uuid::new_v4())); let target_dir = base_dir.join(as_name); - // Step 1: Download the ZIP file + log_message(format!("Downloading {}...", url)); + if let Err(e) = download_zip(url, &zip_path).await { - log_message(format!("Error downloading file: {}", e)); + log_message(format!("Download failed: {}", e)); return; } - // Step 2: Extract and flatten the ZIP file contents into the target directory + log_message("Download complete. Extracting..."); + if let Err(e) = extract_zip_contents_to_folder(&zip_path, &target_dir) { - log_message(format!("Error extracting ZIP file contents: {}", e)); + log_message(format!("Extraction failed: {}", e)); + let _ = fs::remove_file(&zip_path); + return; } - // Step 3: Clean up the downloaded ZIP file - if let Err(e) = fs::remove_file(&zip_path) { - log_message(format!( - "Error cleaning up ZIP file {}: {}", - zip_path.display(), - e - )); - } + let _ = fs::remove_file(&zip_path); + + log_message(format!( + "Successfully extracted to {}", + target_dir.display() + )); } diff --git a/web b/web deleted file mode 100644 index 51662e0..0000000 Binary files a/web and /dev/null differ