feat: use race_download with R2 for agent/JRE downloads

This commit is contained in:
t8y2 2026-05-14 16:29:44 +08:00
parent 6994b4549e
commit 242720cc3c
2 changed files with 60 additions and 8 deletions

View File

@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
const LATEST_JSON_PATH: &str = "https://github.com/t8y2/dbx/releases/latest/download/latest.json";
const LATEST_JSON_R2_PATH: &str = "releases/latest/latest.json";
const GITHUB_RELEASE_API_PREFIX: &str = "https://api.github.com/repos/t8y2/dbx/releases/tags/v";
const RELEASE_URL_PREFIX: &str = "https://github.com/t8y2/dbx/releases/tag/v";
@ -36,7 +37,7 @@ pub async fn fetch_latest_release() -> Result<TauriRelease, String> {
.build()
.map_err(|e| format!("Failed to create HTTP client: {e}"))?;
let resp = crate::race_github_proxies(&client, LATEST_JSON_PATH, "dbx-update-checker")
let resp = crate::race_download(&client, LATEST_JSON_PATH, LATEST_JSON_R2_PATH, "dbx-update-checker")
.await
.map_err(|e| format!("Failed to check updates: {e}"))?;

View File

@ -9,6 +9,7 @@ use dbx_core::agent_manager::{
use dbx_core::connection::AppState;
const REGISTRY_PATH: &str = "https://github.com/t8y2/dbx-agents/releases/latest/download/agent-registry.json";
const REGISTRY_R2_PATH: &str = "agents/agent-registry.json";
static REGISTRY_CACHE: std::sync::LazyLock<Mutex<Option<(std::time::Instant, AgentRegistry)>>> =
std::sync::LazyLock::new(|| Mutex::new(None));
@ -104,7 +105,15 @@ pub async fn install_agent(
"step": "jre", "downloaded": 0u64, "total": platform_jre.size,
}),
);
download_with_progress(&app, "jre", &platform_jre.url, &jre_archive, platform_jre.size).await?;
download_with_progress(
&app,
"jre",
&platform_jre.url,
&github_url_to_r2_path(&platform_jre.url, "jre"),
&jre_archive,
platform_jre.size,
)
.await?;
let _ = app.emit(
"agent-install-progress",
serde_json::json!({
@ -122,7 +131,15 @@ pub async fn install_agent(
"step": "driver", "downloaded": 0u64, "total": driver.jar.size,
}),
);
download_with_progress(&app, "driver", &driver.jar.url, &jar_path, driver.jar.size).await?;
download_with_progress(
&app,
"driver",
&driver.jar.url,
&github_url_to_r2_path(&driver.jar.url, "driver"),
&jar_path,
driver.jar.size,
)
.await?;
let mut local_state = am.load_state();
if let Some(jre_info) = registry.resolve_jre(jre_key) {
@ -175,7 +192,15 @@ pub async fn upgrade_all_agents(app: tauri::AppHandle, state: State<'_, Arc<AppS
"db_type": db_type, "current": current, "total_drivers": total_drivers,
}),
);
download_with_progress(&app, "jre", &platform_jre.url, &jre_archive, platform_jre.size).await?;
download_with_progress(
&app,
"jre",
&platform_jre.url,
&github_url_to_r2_path(&platform_jre.url, "jre"),
&jre_archive,
platform_jre.size,
)
.await?;
let _ = app.emit(
"agent-install-progress",
serde_json::json!({
@ -195,7 +220,15 @@ pub async fn upgrade_all_agents(app: tauri::AppHandle, state: State<'_, Arc<AppS
"db_type": db_type, "current": current, "total_drivers": total_drivers,
}),
);
download_with_progress(&app, "driver", &driver.jar.url, &jar_path, driver.jar.size).await?;
download_with_progress(
&app,
"driver",
&driver.jar.url,
&github_url_to_r2_path(&driver.jar.url, "driver"),
&jar_path,
driver.jar.size,
)
.await?;
let mut local_state = am.load_state();
if let Some(jre_info) = registry.resolve_jre(jre_key) {
@ -311,7 +344,15 @@ pub async fn reinstall_jre(
let platform_jre =
jre_info.platforms.get(platform).ok_or_else(|| format!("No JRE {key} available for platform: {platform}"))?;
let jre_archive = am.base_dir().join("jre-download.tar.gz");
download_with_progress(&app, "jre", &platform_jre.url, &jre_archive, platform_jre.size).await?;
download_with_progress(
&app,
"jre",
&platform_jre.url,
&github_url_to_r2_path(&platform_jre.url, "jre"),
&jre_archive,
platform_jre.size,
)
.await?;
extract_archive(&jre_archive, &jre_dir)?;
std::fs::remove_file(&jre_archive).ok();
let mut local_state = am.load_state();
@ -334,7 +375,7 @@ async fn fetch_registry() -> Result<AgentRegistry, String> {
.timeout(std::time::Duration::from_secs(10))
.build()
.map_err(|e| format!("Failed to create HTTP client: {e}"))?;
let resp = dbx_core::race_github_proxies(&client, REGISTRY_PATH, "dbx-agent-manager")
let resp = dbx_core::race_download(&client, REGISTRY_PATH, REGISTRY_R2_PATH, "dbx-agent-manager")
.await
.map_err(|e| format!("Failed to fetch agent registry: {e}"))?;
let reg: AgentRegistry = resp.json().await.map_err(|e| format!("Failed to parse registry: {e}"))?;
@ -342,10 +383,20 @@ async fn fetch_registry() -> Result<AgentRegistry, String> {
Ok(reg)
}
fn github_url_to_r2_path(github_url: &str, category: &str) -> String {
let filename = github_url.rsplit('/').next().unwrap_or(github_url);
match category {
"jre" => format!("agents/jre/{filename}"),
"driver" => format!("agents/drivers/{filename}"),
_ => format!("agents/{filename}"),
}
}
async fn download_with_progress(
app: &tauri::AppHandle,
step: &str,
url: &str,
r2_path: &str,
dest: &std::path::Path,
total_size: u64,
) -> Result<(), String> {
@ -357,7 +408,7 @@ async fn download_with_progress(
.build()
.map_err(|e| format!("Failed to create HTTP client: {e}"))?;
let resp = dbx_core::race_github_proxies(&client, url, "dbx-agent-manager")
let resp = dbx_core::race_download(&client, url, r2_path, "dbx-agent-manager")
.await
.map_err(|e| format!("Failed to download {url}: {e}"))?;