|
| 1 | +use std::{ |
| 2 | + fs, |
| 3 | + io::{self, Cursor}, |
| 4 | + net::TcpListener, |
| 5 | + path::{Path, PathBuf}, |
| 6 | +}; |
| 7 | + |
| 8 | +use tauri::Manager; |
| 9 | +use tungstenite::connect; |
| 10 | +use zip::ZipArchive; |
| 11 | + |
| 12 | +pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> { |
| 13 | + fs::create_dir_all(&dst)?; |
| 14 | + for entry in fs::read_dir(src)? { |
| 15 | + let entry = entry?; |
| 16 | + let ty = entry.file_type()?; |
| 17 | + if ty.is_dir() { |
| 18 | + copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; |
| 19 | + } else { |
| 20 | + fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?; |
| 21 | + } |
| 22 | + } |
| 23 | + Ok(()) |
| 24 | +} |
| 25 | + |
| 26 | +pub fn get_cef_paths(app_handle: &tauri::AppHandle) -> Result<(PathBuf, PathBuf), String> { |
| 27 | + let app_resource_dir = app_handle |
| 28 | + .path() |
| 29 | + .resource_dir() |
| 30 | + .map_err(|_| "app resource directory not found")?; |
| 31 | + let app_cache_dir = app_handle |
| 32 | + .path() |
| 33 | + .app_cache_dir() |
| 34 | + .map_err(|_| "app cache directory not found")?; |
| 35 | + |
| 36 | + let cef_dir = app_resource_dir.join("cef"); |
| 37 | + let cef_cache = app_cache_dir.join("cefcache"); |
| 38 | + |
| 39 | + Ok((cef_dir, cef_cache)) |
| 40 | +} |
| 41 | + |
| 42 | +pub fn wait_for_cef(port: u16) -> Result<(), String> { |
| 43 | + for _ in 0..10 { |
| 44 | + std::thread::sleep(std::time::Duration::from_millis(500)); |
| 45 | + if healthcheck("localhost", port) { |
| 46 | + return Ok(()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + Err("CEF healthcheck failed".into()) |
| 51 | +} |
| 52 | + |
| 53 | +pub fn healthcheck(host: &str, port: u16) -> bool { |
| 54 | + let address = format!("ws://{}:{}", host, port); |
| 55 | + connect(address.clone()).is_ok() |
| 56 | +} |
| 57 | + |
| 58 | +pub fn find_available_port() -> Result<u16, String> { |
| 59 | + for port in 10000..50000 { |
| 60 | + if let Ok(_) = TcpListener::bind(format!("0.0.0.0:{}", port)) { |
| 61 | + return Ok(port); |
| 62 | + } |
| 63 | + } |
| 64 | + Err("No available ports found".into()) |
| 65 | +} |
| 66 | + |
| 67 | +pub fn compare_checksums(url: &str, checksum_path: impl AsRef<Path>) -> Result<bool, String> { |
| 68 | + let existing = fs::read_to_string(checksum_path) |
| 69 | + .map_err(|e| format!("failed to read existing checksum: {e}"))?; |
| 70 | + let new = download_checksum(url)?; |
| 71 | + |
| 72 | + Ok(existing == new) |
| 73 | +} |
| 74 | + |
| 75 | +pub fn download_checksum(url: &str) -> Result<String, String> { |
| 76 | + let checksum = reqwest::blocking::get(format!("{url}.sha256")) |
| 77 | + .map_err(|e| format!("failed to download checksum: {e}"))? |
| 78 | + .text() |
| 79 | + .map_err(|e| format!("failed to read checksum response: {e}"))?; |
| 80 | + Ok(checksum) |
| 81 | +} |
| 82 | + |
| 83 | +pub fn download_and_extract(url: &str, dir: impl AsRef<Path>) -> Result<(), String> { |
| 84 | + _ = fs::remove_dir_all(&dir); |
| 85 | + fs::create_dir_all(&dir).map_err(|e| format!("failed to create CEF dir: {e}"))?; |
| 86 | + |
| 87 | + let data = reqwest::blocking::get(url) |
| 88 | + .and_then(|r| r.bytes()) |
| 89 | + .and_then(|b| Ok(b.to_vec())) |
| 90 | + .map_err(|e| format!("failed to download CEF: {e}"))?; |
| 91 | + |
| 92 | + ZipArchive::new(Cursor::new(data)) |
| 93 | + .and_then(|mut archive| archive.extract(&dir)) |
| 94 | + .map_err(|e| { |
| 95 | + format!( |
| 96 | + "failed to extract CEF archive to {}: {e}", |
| 97 | + dir.as_ref().display() |
| 98 | + ) |
| 99 | + })?; |
| 100 | + |
| 101 | + let checksum = download_checksum(url)?; |
| 102 | + fs::write(dir.as_ref().join("checksum"), checksum) |
| 103 | + .map_err(|e| format!("failed to save checksum: {e}"))?; |
| 104 | + |
| 105 | + Ok(()) |
| 106 | +} |
0 commit comments