[fix] pre and cdn swith

This commit is contained in:
2025-11-09 00:07:44 +08:00
parent 5e663dc79e
commit 4b7735575a
5 changed files with 72 additions and 18 deletions

View File

@@ -346,9 +346,11 @@ pub async fn check_app_update(
app: tauri::AppHandle,
endpoint: Option<String>,
include_prerelease: Option<bool>,
use_cdn: Option<bool>,
) -> Result<Option<UpdateInfo>, String> {
info!("开始检查更新...");
info!("include_prerelease: {:?}", include_prerelease);
info!("use_cdn: {:?}", use_cdn);
// 构建更新端点 URL
// Tauri updater 需要支持 {{target}} 和 {{arch}} 变量的端点
@@ -413,15 +415,22 @@ pub async fn check_app_update(
// Update 类型没有实现 Debug trait所以不能使用 {:?} 格式化
// 如果需要更多信息,可以单独记录各个字段
// 将下载 URL 替换为 CDN 链接
// 根据 use_cdn 参数决定是否使用 CDN 链接
let mut download_url = update.download_url.to_string();
let original_url = download_url.clone();
// 如果 URL 不是 CDN 链接,则在 CDN 域名后拼接原 URL
if !download_url.contains("cdn.upup.cool") {
download_url = format!("https://cdn.upup.cool/{}", original_url);
info!("将下载 URL 从 {} 替换为 CDN 链接: {}", original_url, download_url);
let use_cdn_value = use_cdn.unwrap_or(true); // 默认使用 CDN
if use_cdn_value {
// 如果 URL 不是 CDN 链接,则在 CDN 域名后拼接原 URL
if !download_url.contains("cdn.upup.cool") {
download_url = format!("https://cdn.upup.cool/{}", original_url);
info!("将下载 URL 从 {} 替换为 CDN 链接: {}", original_url, download_url);
} else {
info!("下载 URL 已经是 CDN 链接: {}", download_url);
}
} else {
info!("下载 URL 已经是 CDN 链接: {}", download_url);
// 不使用 CDN保持原始 URL
info!("不使用 CDN保持原始下载 URL: {}", download_url);
}
// 存储更新对象和 CDN URL 供后续使用
@@ -457,8 +466,10 @@ pub async fn check_app_update(
#[tauri::command]
pub async fn download_app_update(
app: tauri::AppHandle,
use_cdn: Option<bool>,
) -> Result<(), String> {
info!("开始下载更新...");
info!("use_cdn: {:?}", use_cdn);
let pending = get_pending_update();
// 检查是否有待下载的更新
@@ -477,28 +488,37 @@ pub async fn download_app_update(
let app_handle_progress = app.clone();
let app_handle_complete = app.clone();
// 在锁内获取 update 和 CDN URL然后在锁外使用
// 根据 use_cdn 参数决定使用原始 URL 还是 CDN URL
let use_cdn_value = use_cdn.unwrap_or(true); // 默认使用 CDN
// 在锁内获取 update 和 URL然后在锁外使用
let cloned_data = {
let update_guard = pending.lock().unwrap();
if let Some(ref update_with_cdn) = *update_guard {
info!("准备下载更新 - 版本: {}, 原始 URL: {}, CDN URL: {}",
let download_url = if use_cdn_value {
update_with_cdn.cdn_url.clone()
} else {
update_with_cdn.update.download_url.to_string()
};
info!("准备下载更新 - 版本: {}, 原始 URL: {}, CDN URL: {}, 使用 URL: {}",
update_with_cdn.update.version,
update_with_cdn.update.download_url,
update_with_cdn.cdn_url);
Some((update_with_cdn.update.clone(), update_with_cdn.cdn_url.clone()))
update_with_cdn.cdn_url,
download_url);
Some((update_with_cdn.update.clone(), update_with_cdn.cdn_url.clone(), download_url))
} else {
None
}
};
// 现在锁已经释放,可以安全地下载
if let Some((update, cdn_url)) = cloned_data {
info!("开始使用 CDN URL 下载更新文件: {}", cdn_url);
if let Some((update, cdn_url, download_url)) = cloned_data {
info!("开始下载更新文件: {} (使用 CDN: {})", download_url, use_cdn_value);
// 使用 reqwest 手动下载文件
let client = reqwest::Client::new();
let mut response = client
.get(&cdn_url)
.get(&download_url)
.send()
.await
.map_err(|e| {