78 lines
2.2 KiB
Rust
78 lines
2.2 KiB
Rust
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()
|
|
}
|