mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-06-14 19:57:04 +08:00
feat: 优化更新加速逻辑
This commit is contained in:
@@ -245,7 +245,8 @@ jobs:
|
|||||||
echo "tag=${{ github.ref_name }}" >> $GITHUB_OUTPUT
|
echo "tag=${{ github.ref_name }}" >> $GITHUB_OUTPUT
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Upload to Cloudflare R2
|
# 上传所有文件到版本目录
|
||||||
|
- name: Upload to Cloudflare R2 (version directory)
|
||||||
uses: ryand56/r2-upload-action@latest
|
uses: ryand56/r2-upload-action@latest
|
||||||
with:
|
with:
|
||||||
r2-account-id: ${{ secrets.R2_ACCOUNT_ID }}
|
r2-account-id: ${{ secrets.R2_ACCOUNT_ID }}
|
||||||
@@ -254,3 +255,39 @@ jobs:
|
|||||||
r2-bucket: ${{ secrets.R2_BUCKET_NAME }}
|
r2-bucket: ${{ secrets.R2_BUCKET_NAME }}
|
||||||
source-dir: dist
|
source-dir: dist
|
||||||
destination-dir: releases/download/${{ steps.version.outputs.tag }}
|
destination-dir: releases/download/${{ steps.version.outputs.tag }}
|
||||||
|
|
||||||
|
# 准备 yml 文件用于根目录上传
|
||||||
|
# 需要修改 yml 中的 path/url 为包含版本号的完整路径
|
||||||
|
- name: Prepare yml files for root upload
|
||||||
|
run: |
|
||||||
|
mkdir -p dist-yml
|
||||||
|
VERSION_TAG="${{ steps.version.outputs.tag }}"
|
||||||
|
|
||||||
|
# 处理所有 yml 文件
|
||||||
|
for yml_file in dist/*.yml; do
|
||||||
|
filename=$(basename "$yml_file")
|
||||||
|
echo "Processing $filename..."
|
||||||
|
|
||||||
|
# 使用 sed 修改 path 和 url 字段,添加版本目录前缀
|
||||||
|
# path: ChatLab-x.x.x-arm64.dmg → path: vx.x.x/ChatLab-x.x.x-arm64.dmg
|
||||||
|
# url: ChatLab-x.x.x-arm64.dmg → url: vx.x.x/ChatLab-x.x.x-arm64.dmg
|
||||||
|
sed -E "s|^(path: )(.+)|\1${VERSION_TAG}/\2|g; s|^( - url: )(.+)|\1${VERSION_TAG}/\2|g" "$yml_file" > "dist-yml/$filename"
|
||||||
|
|
||||||
|
echo "Original:"
|
||||||
|
cat "$yml_file"
|
||||||
|
echo ""
|
||||||
|
echo "Modified:"
|
||||||
|
cat "dist-yml/$filename"
|
||||||
|
echo "---"
|
||||||
|
done
|
||||||
|
|
||||||
|
# 上传 yml 文件到根目录(覆盖旧版本,electron-updater 从这里检测更新)
|
||||||
|
- name: Upload yml files to Cloudflare R2 (root directory)
|
||||||
|
uses: ryand56/r2-upload-action@latest
|
||||||
|
with:
|
||||||
|
r2-account-id: ${{ secrets.R2_ACCOUNT_ID }}
|
||||||
|
r2-access-key-id: ${{ secrets.R2_ACCESS_KEY_ID }}
|
||||||
|
r2-secret-access-key: ${{ secrets.R2_SECRET_ACCESS_KEY }}
|
||||||
|
r2-bucket: ${{ secrets.R2_BUCKET_NAME }}
|
||||||
|
source-dir: dist-yml
|
||||||
|
destination-dir: releases/download
|
||||||
|
|||||||
+28
-15
@@ -4,14 +4,14 @@ import { platform } from '@electron-toolkit/utils'
|
|||||||
import { logger } from './logger'
|
import { logger } from './logger'
|
||||||
import { getActiveProxyUrl } from './network/proxy'
|
import { getActiveProxyUrl } from './network/proxy'
|
||||||
|
|
||||||
// R2 镜像源 URL(用于中国大陆用户)
|
// R2 镜像源 URL(速度更快,作为主要更新源)
|
||||||
const R2_MIRROR_URL = 'https://chatlab.1app.top/releases/download'
|
const R2_MIRROR_URL = 'https://chatlab.1app.top/releases/download'
|
||||||
|
|
||||||
// 更新源类型
|
// 更新源类型
|
||||||
type UpdateSource = 'github' | 'r2'
|
type UpdateSource = 'github' | 'r2'
|
||||||
|
|
||||||
// 当前使用的更新源
|
// 当前使用的更新源(默认 R2 优先)
|
||||||
let currentSource: UpdateSource = 'github'
|
let currentSource: UpdateSource = 'r2'
|
||||||
|
|
||||||
// 是否已尝试过备用源
|
// 是否已尝试过备用源
|
||||||
let hasTriedFallback = false
|
let hasTriedFallback = false
|
||||||
@@ -44,17 +44,29 @@ function switchToR2Mirror(): void {
|
|||||||
provider: 'generic',
|
provider: 'generic',
|
||||||
url: R2_MIRROR_URL,
|
url: R2_MIRROR_URL,
|
||||||
})
|
})
|
||||||
logger.info(`[Update] 已切换到 R2 镜像源: ${R2_MIRROR_URL}`)
|
logger.info(`[Update] 使用 R2 镜像源: ${R2_MIRROR_URL}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重置为 GitHub 源(下次检查时使用)
|
* 切换到 GitHub 源(备用更新源)
|
||||||
*/
|
*/
|
||||||
function resetToGitHubSource(): void {
|
function switchToGitHub(): void {
|
||||||
currentSource = 'github'
|
currentSource = 'github'
|
||||||
|
// 使用 GitHub 作为 generic provider
|
||||||
|
autoUpdater.setFeedURL({
|
||||||
|
provider: 'github',
|
||||||
|
owner: 'hellodigua',
|
||||||
|
repo: 'ChatLab',
|
||||||
|
})
|
||||||
|
logger.info('[Update] 已切换到 GitHub 备用源')
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重置为默认更新源(R2 优先)
|
||||||
|
*/
|
||||||
|
function resetToDefaultSource(): void {
|
||||||
hasTriedFallback = false
|
hasTriedFallback = false
|
||||||
// electron-updater 默认使用 electron-builder.yml 中的配置(GitHub)
|
switchToR2Mirror()
|
||||||
// 无需显式设置,只需要不调用 setFeedURL 即可
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -233,17 +245,17 @@ const checkUpdate = (win) => {
|
|||||||
autoUpdater.on('error', (err) => {
|
autoUpdater.on('error', (err) => {
|
||||||
logger.error(`[Update] 更新错误 (${currentSource}): ${err.message || err}`)
|
logger.error(`[Update] 更新错误 (${currentSource}): ${err.message || err}`)
|
||||||
|
|
||||||
// 如果是 GitHub 源且为网络错误,尝试切换到 R2 备用源
|
// 如果是 R2 源且为网络错误,尝试切换到 GitHub 备用源
|
||||||
if (currentSource === 'github' && !hasTriedFallback && isNetworkError(err)) {
|
if (currentSource === 'r2' && !hasTriedFallback && isNetworkError(err)) {
|
||||||
hasTriedFallback = true
|
hasTriedFallback = true
|
||||||
logger.info('[Update] GitHub 源访问失败,尝试切换到 R2 镜像源...')
|
logger.info('[Update] R2 镜像源访问失败,尝试切换到 GitHub 备用源...')
|
||||||
|
|
||||||
switchToR2Mirror()
|
switchToGitHub()
|
||||||
|
|
||||||
// 延迟 1 秒后重试检查更新
|
// 延迟 1 秒后重试检查更新
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
autoUpdater.checkForUpdates().catch((retryErr) => {
|
autoUpdater.checkForUpdates().catch((retryErr) => {
|
||||||
logger.error(`[Update] R2 镜像源检查更新也失败: ${retryErr}`)
|
logger.error(`[Update] GitHub 备用源检查更新也失败: ${retryErr}`)
|
||||||
})
|
})
|
||||||
}, 1000)
|
}, 1000)
|
||||||
}
|
}
|
||||||
@@ -252,7 +264,8 @@ const checkUpdate = (win) => {
|
|||||||
// 等待 3 秒再检查更新,确保窗口准备完成,用户进入系统
|
// 等待 3 秒再检查更新,确保窗口准备完成,用户进入系统
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
isManualCheck = false // 自动检查
|
isManualCheck = false // 自动检查
|
||||||
resetToGitHubSource() // 重置为 GitHub 源
|
resetToDefaultSource() // 重置为默认更新源(R2 优先)
|
||||||
|
|
||||||
autoUpdater.checkForUpdates().catch((err) => {
|
autoUpdater.checkForUpdates().catch((err) => {
|
||||||
console.log('[Update] 检查更新失败:', err)
|
console.log('[Update] 检查更新失败:', err)
|
||||||
})
|
})
|
||||||
@@ -269,7 +282,7 @@ const manualCheckForUpdates = () => {
|
|||||||
|
|
||||||
isManualCheck = true // 手动检查
|
isManualCheck = true // 手动检查
|
||||||
isFirstShow = false // 手动检查时,无论结果都显示提示
|
isFirstShow = false // 手动检查时,无论结果都显示提示
|
||||||
resetToGitHubSource() // 重置为 GitHub 源
|
resetToDefaultSource() // 重置为默认更新源(R2 优先)
|
||||||
|
|
||||||
autoUpdater.checkForUpdates().catch((err) => {
|
autoUpdater.checkForUpdates().catch((err) => {
|
||||||
console.log('[Update] 手动检查更新失败:', err)
|
console.log('[Update] 手动检查更新失败:', err)
|
||||||
|
|||||||
Reference in New Issue
Block a user