Added Core Features chroium, config,console_ui,headless,webserver,states

This commit is contained in:
Joey Pillunat 2025-10-24 11:52:34 +02:00
parent 4c6c6d572c
commit c3c60efb81
13 changed files with 688 additions and 0 deletions

78
Core/chromium/mod.rs Normal file
View file

@ -0,0 +1,78 @@
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<PathBuf, Box<dyn std::error::Error>>
{
// 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<PathBuf, Box<dyn std::error::Error>>
{
// 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<dyn std::error::Error>),
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<PathBuf> {
let cache = exe_dir.join(CACHE_DIR);
download::find_latest_local(&cache).ok().flatten()
}