use std::{path::{Path, PathBuf}}; use crate::config::{Config, save_config, resolve_path}; use crate::state::{SharedState, ChromiumPhase}; mod download; pub mod utils; pub const CACHE_DIR: &str = "runtime/chrome-cache"; pub async fn start_check(cfg: &mut Config, exe_dir: &Path, state: SharedState) -> Result> { // Status: Checking { let mut s = state.lock().unwrap(); s.chromium = ChromiumPhase::Checking; } // ensure_chromium kann Download triggern (wir geben state weiter, um „Downloading“ zu setzen) match ensure_chromium(cfg, exe_dir, state.clone()).await { Ok(path) => { let mut s = state.lock().unwrap(); s.chromium = ChromiumPhase::Ready; Ok(path) } Err(e) => { let mut s = state.lock().unwrap(); s.chromium = ChromiumPhase::Error(e.to_string()); Err(e) } } } async fn ensure_chromium(cfg: &mut Config, exe_dir: &Path, state: SharedState) -> Result> { // 1) Pfad aus Config if !cfg.chrome.path.is_empty() { let p = resolve_path(exe_dir, &cfg.chrome.path); if p.exists() { return Ok(p); } } // 2) Lokaler Cache? if let Some(p) = find_existing_binary(exe_dir) { cfg.chrome.path = p.to_string_lossy().to_string(); let _ = save_config(exe_dir, cfg); return Ok(p); } // 3) Download erforderlich -> Status: Downloading { let mut s = state.lock().unwrap(); s.chromium = ChromiumPhase::Downloading; } let cache_dir = exe_dir.join(CACHE_DIR); let bin_path_result = tokio::task::spawn_blocking(move || { download::get_or_install_latest(&cache_dir) }).await; let bin_path = match bin_path_result { Ok(Ok(path)) => path, Ok(Err(e)) => return Err(e as Box), Err(e) => return Err(Box::new(e)), }; cfg.chrome.path = bin_path.to_string_lossy().to_string(); let _ = save_config(exe_dir, cfg); Ok(bin_path) } fn find_existing_binary(exe_dir: &Path) -> Option { let cache = exe_dir.join(CACHE_DIR); download::find_latest_local(&cache).ok().flatten() }