Skip to content

Commit ba398ca

Browse files
committed
refactor: 优化下载配置并添加版本显示功能
- 提取通用下载选项到 setCommonDownloadOptions 函数 - 移除抖音特定 HTTP 头配置,提升通用性 - 在前端 UI 添加版本号显示(桌面端和移动端) - Dockerfile 支持 VERSION 构建参数注入 - 更新 aria2 镜像到最新版本 1.37 - 添加 TypeScript 全局类型定义
1 parent 3729be6 commit ba398ca

File tree

6 files changed

+38
-15
lines changed

6 files changed

+38
-15
lines changed

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
FROM node:18-alpine AS frontend-builder
22

3+
# 接收版本号参数
4+
ARG VERSION=0.1.0
5+
36
WORKDIR /app
47

58
COPY frontend/package.json frontend/pnpm-lock.yaml ./
@@ -8,6 +11,9 @@ RUN npm install -g pnpm && pnpm install
811

912
COPY frontend/ .
1013

14+
# 更新 package.json 中的版本号
15+
RUN node -e "const fs=require('fs');const pkg=JSON.parse(fs.readFileSync('package.json'));pkg.version='${VERSION}';fs.writeFileSync('package.json',JSON.stringify(pkg,null,2)+'\n');"
16+
1117
RUN pnpm build
1218

1319
FROM golang:1.24-alpine AS backend-builder

backend/service/download_service.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ func NewDownloadService(db *gorm.DB, dl downloader.Downloader) *DownloadService
3232
}
3333
}
3434

35+
// setCommonDownloadOptions 设置通用的下载选项
36+
func setCommonDownloadOptions(options map[string]interface{}) {
37+
// BT/Magnet 下载完成后不做种
38+
options["seed-time"] = 0
39+
// 支持 HTTP 重定向(如抖音的 302 重定向)
40+
options["max-tries"] = 5
41+
options["max-connection-per-server"] = 5
42+
options["split"] = 5
43+
options["min-split-size"] = "1M"
44+
options["follow-metalink"] = "true"
45+
options["metalink-preferred-protocol"] = "https"
46+
}
47+
3548
func (s *DownloadService) SubmitDownload(ctx context.Context, req types.DownloadRequest) (*model.DownloadTask, error) {
3649
task := &model.DownloadTask{
3750
URL: req.URL,
@@ -105,14 +118,7 @@ func (s *DownloadService) SubmitDownload(ctx context.Context, req types.Download
105118
}
106119

107120
options := make(map[string]interface{})
108-
// BT/Magnet 下载完成后不做种
109-
options["seed-time"] = 0
110-
111-
// 支持 HTTP 重定向(如抖音的 302 重定向)
112-
options["max-tries"] = 5
113-
options["max-connection-per-server"] = 5
114-
options["split"] = 5
115-
options["min-split-size"] = "1M"
121+
setCommonDownloadOptions(options)
116122

117123
if downloadPath != "" {
118124
if strings.HasSuffix(downloadPath, "/") {
@@ -271,16 +277,11 @@ func (s *DownloadService) RetryTask(ctx context.Context, id uint) error {
271277
}
272278

273279
options := make(map[string]interface{})
280+
setCommonDownloadOptions(options)
274281
if task.Filename != "" {
275282
options["out"] = task.Filename
276283
}
277284

278-
// 添加重定向和防盗链支持
279-
options["max-tries"] = 5
280-
options["max-connection-per-server"] = 5
281-
options["split"] = 5
282-
options["min-split-size"] = "1M"
283-
284285
gid, err := s.downloader.AddURI(ctx, []string{task.URL}, options)
285286
if err != nil {
286287
return fmt.Errorf("failed to retry download: %w", err)

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ services:
2121
- mynest
2222

2323
aria2:
24-
image: p3terx/aria2-pro
24+
image: p3terx/aria2-pro:latest
2525
container_name: mynest-aria2
2626
restart: unless-stopped
2727
environment:

frontend/src/App.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ function Navigation({ user, onLogout, onRefreshUser }: { user: User | null; onLo
4646
<Link to="/" className="flex items-center space-x-2 flex-shrink-0">
4747
<h1 className="text-xl font-bold">🪹 MyNest</h1>
4848
<span className="hidden sm:inline text-sm text-muted-foreground">链接的归巢</span>
49+
<span className="hidden lg:inline text-xs text-muted-foreground/60 px-2 py-0.5 rounded-full bg-muted/50">
50+
v{__APP_VERSION__}
51+
</span>
4952
</Link>
5053

5154
{/* 桌面端导航 */}
@@ -95,6 +98,7 @@ function Navigation({ user, onLogout, onRefreshUser }: { user: User | null; onLo
9598
{user && (
9699
<div className="pb-4 border-b">
97100
<p className="text-sm font-medium">{user.username}</p>
101+
<p className="text-xs text-muted-foreground mt-1">v{__APP_VERSION__}</p>
98102
</div>
99103
)}
100104
{navLinks.map((link) => (

frontend/src/vite-env.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <reference types="vite/client" />
2+
3+
declare const __APP_VERSION__: string

frontend/vite.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { defineConfig } from 'vite'
22
import react from '@vitejs/plugin-react'
33
import path from 'path'
4+
import { readFileSync } from 'fs'
5+
6+
// 从 package.json 读取版本号
7+
const packageJson = JSON.parse(
8+
readFileSync(path.resolve(__dirname, 'package.json'), 'utf-8')
9+
)
410

511
export default defineConfig({
612
plugins: [react()],
@@ -9,6 +15,9 @@ export default defineConfig({
915
'@': path.resolve(__dirname, './src'),
1016
},
1117
},
18+
define: {
19+
'__APP_VERSION__': JSON.stringify(packageJson.version),
20+
},
1221
server: {
1322
port: 3000,
1423
proxy: {

0 commit comments

Comments
 (0)