From 242720cc3cf28ebe9085b34fb1644211703255ba Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Thu, 14 May 2026 16:29:44 +0800 Subject: [PATCH] feat: use race_download with R2 for agent/JRE downloads --- crates/dbx-core/src/update.rs | 3 +- src-tauri/src/commands/agents.rs | 65 ++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/crates/dbx-core/src/update.rs b/crates/dbx-core/src/update.rs index bba49c4f9..57c01420c 100644 --- a/crates/dbx-core/src/update.rs +++ b/crates/dbx-core/src/update.rs @@ -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 { .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}"))?; diff --git a/src-tauri/src/commands/agents.rs b/src-tauri/src/commands/agents.rs index c6a446836..4211f95ca 100644 --- a/src-tauri/src/commands/agents.rs +++ b/src-tauri/src/commands/agents.rs @@ -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>> = 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 Result { .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 { 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}"))?;