refactor: allow for manual browser install

This commit is contained in:
zhom
2025-08-09 10:42:22 +04:00
parent 18a0254ca7
commit 7df92ae8ee
+37 -21
View File
@@ -1156,11 +1156,6 @@ impl BrowserRunner {
browser_dir.push(browser_type.as_str()); browser_dir.push(browser_type.as_str());
browser_dir.push(&version); browser_dir.push(&version);
// Clean up any failed previous download
if let Err(e) = registry.cleanup_failed_download(&browser_str, &version) {
println!("Warning: Failed to cleanup previous download: {e}");
}
create_dir_all(&browser_dir).map_err(|e| format!("Failed to create browser directory: {e}"))?; create_dir_all(&browser_dir).map_err(|e| format!("Failed to create browser directory: {e}"))?;
// Mark download as started in registry // Mark download as started in registry
@@ -1171,7 +1166,9 @@ impl BrowserRunner {
// Use the download module // Use the download module
let downloader = crate::download::Downloader::instance(); let downloader = crate::download::Downloader::instance();
let download_path = match downloader // Attempt to download the archive. If the download fails but an archive with the
// expected filename already exists (manual download), continue using that file.
let download_path: PathBuf = match downloader
.download_browser( .download_browser(
&app_handle, &app_handle,
browser_type.clone(), browser_type.clone(),
@@ -1183,15 +1180,25 @@ impl BrowserRunner {
{ {
Ok(path) => path, Ok(path) => path,
Err(e) => { Err(e) => {
// Clean up failed download // Check if the expected archive is already present (manual download)
let _ = registry.cleanup_failed_download(&browser_str, &version); let expected_archive_path = browser_dir.join(&download_info.filename);
let _ = registry.save(); if expected_archive_path.exists() {
// Remove browser-version pair from downloading set on error println!(
{ "Download failed, but found existing archive at {}. Continuing with extraction.",
let mut downloading = DOWNLOADING_BROWSERS.lock().unwrap(); expected_archive_path.display()
downloading.remove(&download_key); );
expected_archive_path
} else {
// Remove only the registry entry; keep any files (including a partially downloaded archive)
let _ = registry.remove_browser(&browser_str, &version);
let _ = registry.save();
// Remove browser-version pair from downloading set on error
{
let mut downloading = DOWNLOADING_BROWSERS.lock().unwrap();
downloading.remove(&download_key);
}
return Err(format!("Failed to download browser: {e}").into());
} }
return Err(format!("Failed to download browser: {e}").into());
} }
}; };
@@ -1209,14 +1216,12 @@ impl BrowserRunner {
.await .await
{ {
Ok(_) => { Ok(_) => {
// Clean up the downloaded archive // Do not remove the archive here. We keep it until verification succeeds.
if let Err(e) = std::fs::remove_file(&download_path) {
println!("Warning: Could not delete archive file: {e}");
}
} }
Err(e) => { Err(e) => {
// Clean up failed download // Do not remove the archive or extracted files. Just drop the registry entry
let _ = registry.cleanup_failed_download(&browser_str, &version); // so it won't be reported as downloaded.
let _ = registry.remove_browser(&browser_str, &version);
let _ = registry.save(); let _ = registry.save();
// Remove browser-version pair from downloading set on error // Remove browser-version pair from downloading set on error
{ {
@@ -1303,7 +1308,8 @@ impl BrowserRunner {
} }
} }
let _ = registry.cleanup_failed_download(&browser_str, &version); // Do not delete files on verification failure; keep archive for manual retry.
let _ = registry.remove_browser(&browser_str, &version);
let _ = registry.save(); let _ = registry.save();
// Remove browser-version pair from downloading set on verification failure // Remove browser-version pair from downloading set on verification failure
{ {
@@ -1320,6 +1326,16 @@ impl BrowserRunner {
.save() .save()
.map_err(|e| format!("Failed to save registry: {e}"))?; .map_err(|e| format!("Failed to save registry: {e}"))?;
// Now that verification succeeded, remove the archive file if it exists
if download_info.is_archive {
let archive_path = browser_dir.join(&download_info.filename);
if archive_path.exists() {
if let Err(e) = std::fs::remove_file(&archive_path) {
println!("Warning: Could not delete archive file after verification: {e}");
}
}
}
// If this is Camoufox, automatically download GeoIP database // If this is Camoufox, automatically download GeoIP database
if browser_str == "camoufox" { if browser_str == "camoufox" {
use crate::geoip_downloader::GeoIPDownloader; use crate::geoip_downloader::GeoIPDownloader;