[Add] Ioto frontend download

This commit is contained in:
Alex Emmet 2026-02-11 19:11:33 +01:00
commit c17e1de478
3 changed files with 32 additions and 28 deletions

View file

@ -31,6 +31,7 @@ use crate::server::server::start;
use crate::terms::consent_state; use crate::terms::consent_state;
use crate::users::user_manager; use crate::users::user_manager;
use crate::util::config_util::CONFIG; use crate::util::config_util::CONFIG;
use crate::util::file_util::download_and_extract_zip;
use crate::util::file_util::has_dir; use crate::util::file_util::has_dir;
pub static APP_STATE: LazyLock<Arc<Mutex<AppState>>> = pub static APP_STATE: LazyLock<Arc<Mutex<AppState>>> =
@ -135,11 +136,11 @@ async fn main() {
} }
} }
if !has_dir("web") { if !has_dir("web") {
/*download_and_extract_zip( download_and_extract_zip(
"weblink", "https://omega.tensamin.net/api/download/iota_frontend",
"web", "web",
) )
.await;*/ .await;
} }
loop { loop {
if *SHUTDOWN.read().await { if *SHUTDOWN.read().await {

View file

@ -1,6 +1,6 @@
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::{self, BufReader, Read}; use std::io::{self, BufReader, Read, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use sysinfo::System; use sysinfo::System;
use uuid::Uuid; use uuid::Uuid;
@ -249,19 +249,22 @@ pub fn get_used_ram() -> String {
format!("{}/{}", design_byte(used), design_byte(total)) format!("{}/{}", design_byte(used), design_byte(total))
} }
// Helper to download the zip file content to a file on disk use reqwest::Client;
#[allow(dead_code)]
async fn download_zip(url: &str, as_name: &Path) -> Result<(), Box<dyn std::error::Error>> { pub async fn download_zip(url: &str, zip_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
let response = reqwest::get(url).await?; let client = Client::new();
let response = client.get(url).send().await?;
// Check for successful response status
if !response.status().is_success() { 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 bytes = response.bytes().await?;
let body = response.bytes().await?;
io::copy(&mut &*body, &mut zip_file)?; let mut file = File::create(zip_path)?;
file.write_all(&bytes)?;
file.flush()?;
Ok(()) Ok(())
} }
@ -343,30 +346,30 @@ fn extract_zip_contents_to_folder(
Ok(()) Ok(())
} }
#[allow(dead_code)]
pub async fn download_and_extract_zip(url: &str, as_name: &str) { pub async fn download_and_extract_zip(url: &str, as_name: &str) {
let base_dir = PathBuf::from(get_directory()); 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(format!("{}.zip", Uuid::new_v4()));
let zip_path = base_dir.join(&zip_filename);
let target_dir = base_dir.join(as_name); 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 { if let Err(e) = download_zip(url, &zip_path).await {
log_message(format!("Error downloading file: {}", e)); log_message(format!("Download failed: {}", e));
return; 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) { 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 let _ = fs::remove_file(&zip_path);
if let Err(e) = fs::remove_file(&zip_path) {
log_message(format!( log_message(format!(
"Error cleaning up ZIP file {}: {}", "Successfully extracted to {}",
zip_path.display(), target_dir.display()
e ));
));
}
} }

BIN
web

Binary file not shown.