Ver Fonte

vs code的国内下载

黄中银 há 2 semanas atrás
pai
commit
32d1fefad1
2 ficheiros alterados com 61 adições e 44 exclusões
  1. 59 31
      src-tauri/src/commands/install.rs
  2. 2 13
      src-tauri/src/commands/software.rs

+ 59 - 31
src-tauri/src/commands/install.rs

@@ -547,37 +547,34 @@ where
         let mirror_config = get_vscode_mirror_config().await;
         let mirror = mirror_config.mirror.as_str();
 
-        let download_url: String;
         let client = reqwest::Client::new();
 
-        if mirror == "azure-china" {
-            // Azure 中国 CDN:先获取最新版本信息
-            // 从官方 API 获取最新版本的 commit hash 和版本号
-            let update_response = client
-                .get("https://update.code.visualstudio.com/api/update/win32-x64/stable/latest")
-                .header("User-Agent", "Claude-AI-Installer")
-                .send()
-                .await
-                .map_err(|e| format!("Failed to get VS Code version info: {}", e))?;
-
-            let update_info: serde_json::Value = update_response.json().await.map_err(|e| e.to_string())?;
-
-            let commit_hash = update_info.get("version")
-                .and_then(|v| v.as_str())
-                .ok_or("Failed to get VS Code commit hash")?;
-
-            let version = update_info.get("productVersion")
-                .and_then(|v| v.as_str())
-                .ok_or("Failed to get VS Code version")?;
-
-            // Azure 中国 CDN URL 格式
-            download_url = format!(
-                "https://vscode.cdn.azure.cn/stable/{}/VSCodeSetup-x64-{}.exe",
-                commit_hash, version
-            );
+        // 从官方 API 获取下载地址
+        let update_response = client
+            .get("https://update.code.visualstudio.com/api/update/win32-x64/stable/latest")
+            .header("User-Agent", "Claude-AI-Installer")
+            .send()
+            .await
+            .map_err(|e| format!("Failed to get VS Code version info: {}", e))?;
+
+        let update_info: serde_json::Value = update_response.json().await.map_err(|e| e.to_string())?;
+
+        let official_url = update_info.get("url")
+            .and_then(|v| v.as_str())
+            .ok_or("Failed to get VS Code download URL")?;
+
+        // 根据镜像配置决定下载地址
+        let download_url: String = if mirror == "azure-china" {
+            // 国内镜像:将官方下载地址中的域名替换为 vscode.cdn.azure.cn
+            if let Ok(mut url) = url::Url::parse(official_url) {
+                let _ = url.set_host(Some("vscode.cdn.azure.cn"));
+                url.to_string()
+            } else {
+                official_url.to_string()
+            }
         } else {
-            // 官方源
-            download_url = "https://code.visualstudio.com/sha/download?build=stable&os=win32-x64".to_string();
+            // 官方源:直接使用官方 API 返回的下载地址
+            official_url.to_string()
         };
 
         let temp_dir = std::env::temp_dir();
@@ -662,15 +659,46 @@ where
             "software": "VS Code"
         })));
 
-        // 下载并安装 .deb 包
-        let download_url = "https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64";
+        // 获取镜像配置
+        let mirror_config = get_vscode_mirror_config().await;
+        let mirror = mirror_config.mirror.as_str();
+
+        let client = reqwest::Client::new();
+
+        // 从官方 API 获取下载地址
+        let update_response = client
+            .get("https://update.code.visualstudio.com/api/update/linux-deb-x64/stable/latest")
+            .header("User-Agent", "Claude-AI-Installer")
+            .send()
+            .await
+            .map_err(|e| format!("Failed to get VS Code version info: {}", e))?;
+
+        let update_info: serde_json::Value = update_response.json().await.map_err(|e| e.to_string())?;
+
+        let official_url = update_info.get("url")
+            .and_then(|v| v.as_str())
+            .ok_or("Failed to get VS Code download URL")?;
+
+        // 根据镜像配置决定下载地址
+        let download_url: String = if mirror == "azure-china" {
+            // 国内镜像:将官方下载地址中的域名替换为 vscode.cdn.azure.cn
+            if let Ok(mut url) = url::Url::parse(official_url) {
+                let _ = url.set_host(Some("vscode.cdn.azure.cn"));
+                url.to_string()
+            } else {
+                official_url.to_string()
+            }
+        } else {
+            // 官方源:直接使用官方 API 返回的下载地址
+            official_url.to_string()
+        };
 
         let temp_dir = std::env::temp_dir();
         let deb_path = temp_dir.join("code.deb");
 
         // 使用流式下载并报告进度
         let mut last_logged_percent: i32 = 0;
-        download_file_with_progress(download_url, &deb_path, |downloaded, total, percent| {
+        download_file_with_progress(&download_url, &deb_path, |downloaded, total, percent| {
             let progress = 10.0 + (percent * 0.5);
             let downloaded_mb = downloaded as f64 / 1024.0 / 1024.0;
             let total_mb = total as f64 / 1024.0 / 1024.0;

+ 2 - 13
src-tauri/src/commands/software.rs

@@ -1,7 +1,7 @@
 use crate::utils::shell::run_shell_hidden;
 use serde::{Deserialize, Serialize};
 use regex::Regex;
-use super::config::{get_git_mirror_config, get_nodejs_mirror_config, get_vscode_mirror_config};
+use super::config::{get_git_mirror_config, get_nodejs_mirror_config};
 
 #[derive(Serialize, Deserialize)]
 pub struct InstalledInfo {
@@ -355,19 +355,8 @@ async fn get_git_versions_from_huaweicloud(client: &reqwest::Client) -> Vec<Stri
 }
 
 async fn get_vscode_versions() -> Result<VersionResult, String> {
-    // 获取镜像配置
-    let mirror_config = get_vscode_mirror_config().await;
-    let mirror = mirror_config.mirror.as_str();
-
     let client = reqwest::Client::new();
 
-    // 根据镜像源显示名称(VS Code 版本列表 API 只有官方源)
-    let mirror_name = if mirror == "azure-china" {
-        "Azure 中国 CDN"
-    } else {
-        "VS Code 官方"
-    };
-
     // 从 VS Code 官方 API 获取版本列表
     let response = client
         .get("https://update.code.visualstudio.com/api/releases/stable")
@@ -405,7 +394,7 @@ async fn get_vscode_versions() -> Result<VersionResult, String> {
 
     Ok(VersionResult {
         versions: version_items,
-        warning: Some(format!("下载源: {}", mirror_name)),
+        warning: None,
     })
 }