[fix] pre and cdn swith
This commit is contained in:
2
next-env.d.ts
vendored
2
next-env.d.ts
vendored
@@ -1,6 +1,6 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
import "./.next/types/routes.d.ts";
|
||||
import "./.next/dev/types/routes.d.ts";
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
||||
|
||||
@@ -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,9 +415,12 @@ 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();
|
||||
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);
|
||||
@@ -423,6 +428,10 @@ pub async fn check_app_update(
|
||||
} else {
|
||||
info!("下载 URL 已经是 CDN 链接: {}", download_url);
|
||||
}
|
||||
} else {
|
||||
// 不使用 CDN,保持原始 URL
|
||||
info!("不使用 CDN,保持原始下载 URL: {}", download_url);
|
||||
}
|
||||
|
||||
// 存储更新对象和 CDN URL 供后续使用
|
||||
let pending = get_pending_update();
|
||||
@@ -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| {
|
||||
|
||||
@@ -64,10 +64,23 @@ export default function Page() {
|
||||
? "检查更新时会包含预发布版本(beta、alpha等)"
|
||||
: "仅检查正式版本"}
|
||||
</p>
|
||||
<Switch
|
||||
isSelected={app.state.useCdn}
|
||||
size="sm"
|
||||
onChange={(e) => app.setUseCdn(e.target.checked)}
|
||||
>
|
||||
使用 CDN 加速下载
|
||||
</Switch>
|
||||
<p className="text-xs text-zinc-500">
|
||||
{app.state.useCdn
|
||||
? "加速下载,避免 GitHub 无法正常访问"
|
||||
: "直接从 GitHub 下载更新文件"}
|
||||
</p>
|
||||
</div>
|
||||
<UpdateChecker
|
||||
customEndpoint={customEndpoint || undefined}
|
||||
includePrerelease={app.state.includePrerelease}
|
||||
useCdn={app.state.useCdn}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -28,11 +28,13 @@ interface UpdateInfo {
|
||||
interface UpdateCheckerProps {
|
||||
customEndpoint?: string
|
||||
includePrerelease?: boolean
|
||||
useCdn?: boolean
|
||||
}
|
||||
|
||||
export function UpdateChecker({
|
||||
customEndpoint,
|
||||
includePrerelease = false,
|
||||
useCdn = true,
|
||||
}: UpdateCheckerProps) {
|
||||
const app = useAppStore()
|
||||
const [checking, setChecking] = useState(false)
|
||||
@@ -73,12 +75,25 @@ export function UpdateChecker({
|
||||
setDownloadCompleted(false)
|
||||
|
||||
try {
|
||||
// 如果有自定义端点,使用自定义端点;否则使用默认端点(GitHub Release 或镜像源)
|
||||
const endpoint = customEndpoint || null
|
||||
// 根据是否包含测试版来选择不同的 endpoint
|
||||
// 如果提供了 customEndpoint,优先使用;否则根据 includePrerelease 动态选择
|
||||
let endpoint: string | null = null
|
||||
if (customEndpoint) {
|
||||
// 如果提供了 customEndpoint,仍然使用它(用于特殊场景)
|
||||
endpoint = customEndpoint
|
||||
} else {
|
||||
// 根据 includePrerelease 选择对应的 endpoint
|
||||
if (includePrerelease) {
|
||||
endpoint = "https://gh-info.okk.cool/repos/plsgo/cstb/releases/latest/pre/tauri"
|
||||
} else {
|
||||
endpoint = "https://gh-info.okk.cool/repos/plsgo/cstb/releases/latest/tauri"
|
||||
}
|
||||
}
|
||||
|
||||
const result = await invoke<UpdateInfo | null>("check_app_update", {
|
||||
endpoint: endpoint,
|
||||
includePrerelease: includePrerelease,
|
||||
useCdn: useCdn,
|
||||
})
|
||||
|
||||
if (result) {
|
||||
@@ -122,8 +137,8 @@ export function UpdateChecker({
|
||||
setDownloadCompleted(false)
|
||||
|
||||
try {
|
||||
// 使用官方 updater 插件,不需要传递 downloadUrl
|
||||
await invoke("download_app_update")
|
||||
// 使用官方 updater 插件,传递 useCdn 参数
|
||||
await invoke("download_app_update", { useCdn: useCdn })
|
||||
|
||||
// 下载完成,标记状态
|
||||
setDownloadProgress(100)
|
||||
|
||||
@@ -12,6 +12,7 @@ const defaultValue = {
|
||||
notice: "",
|
||||
useMirror: true, // 默认使用镜像源(CDN 加速)
|
||||
includePrerelease: false, // 默认不包含预发布版本
|
||||
useCdn: true, // 默认使用 CDN 加速下载
|
||||
autoStart: false,
|
||||
startHidden: false,
|
||||
hiddenOnClose: false,
|
||||
@@ -34,6 +35,7 @@ export const useAppStore = () => {
|
||||
setNotice,
|
||||
setUseMirror,
|
||||
setIncludePrerelease,
|
||||
setUseCdn,
|
||||
setAutoStart,
|
||||
setStartHidden,
|
||||
setHiddenOnClose,
|
||||
@@ -66,6 +68,9 @@ const setUseMirror = (useMirror: boolean) => {
|
||||
const setIncludePrerelease = (includePrerelease: boolean) => {
|
||||
appStore.state.includePrerelease = includePrerelease
|
||||
}
|
||||
const setUseCdn = (useCdn: boolean) => {
|
||||
appStore.state.useCdn = useCdn
|
||||
}
|
||||
|
||||
const setAutoStart = (autoStart: boolean) => {
|
||||
if (autoStart) {
|
||||
@@ -99,6 +104,7 @@ const resetAppStore = () => {
|
||||
setNotice(defaultValue.notice)
|
||||
setUseMirror(defaultValue.useMirror)
|
||||
setIncludePrerelease(defaultValue.includePrerelease)
|
||||
setUseCdn(defaultValue.useCdn)
|
||||
setAutoStart(defaultValue.autoStart)
|
||||
void setStartHidden(defaultValue.startHidden)
|
||||
setHiddenOnClose(defaultValue.hiddenOnClose)
|
||||
|
||||
Reference in New Issue
Block a user