mirror of
https://github.com/Awuqing/BackupX.git
synced 2026-06-25 11:33:42 +08:00
Compare commits
21 Commits
fix/audit-
...
v1.4.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
827a5a2181 | ||
|
|
970eb154e1 | ||
|
|
d26753c44a | ||
|
|
4251eb9e15 | ||
|
|
94d5fb7286 | ||
|
|
8eb93b3dd9 | ||
|
|
df5c8aa80d | ||
|
|
9a4556f473 | ||
|
|
a772b94ca5 | ||
|
|
3bd15bf3fd | ||
|
|
5ae7fb2f5d | ||
|
|
37ad6b1db1 | ||
|
|
d9e0609089 | ||
|
|
ab9919f15f | ||
|
|
d70b4094af | ||
|
|
eeec7678a1 | ||
|
|
cefbdf3a53 | ||
|
|
4a56ad05fc | ||
|
|
9ea02566cb | ||
|
|
a45b1f7bfb | ||
|
|
3023a089fb |
@@ -55,6 +55,7 @@ RUN apk add --no-cache \
|
|||||||
nginx \
|
nginx \
|
||||||
tzdata \
|
tzdata \
|
||||||
ca-certificates \
|
ca-certificates \
|
||||||
|
docker-cli docker-cli-compose \
|
||||||
# Required by mysql/postgresql backup tasks
|
# Required by mysql/postgresql backup tasks
|
||||||
mysql-client \
|
mysql-client \
|
||||||
postgresql16-client \
|
postgresql16-client \
|
||||||
|
|||||||
32
README.md
32
README.md
@@ -35,10 +35,10 @@
|
|||||||
| 能力 | 说明 |
|
| 能力 | 说明 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| **备份类型** | 文件/目录(多源路径)、MySQL、PostgreSQL、SQLite、SAP HANA |
|
| **备份类型** | 文件/目录(多源路径)、MySQL、PostgreSQL、SQLite、SAP HANA |
|
||||||
| **存储后端** | 阿里云 OSS、腾讯云 COS、七牛云、S3 兼容(AWS/MinIO/R2)、Google Drive、WebDAV、FTP/FTPS、本地磁盘 |
|
| **70+ 存储后端** | 内置阿里云 OSS / 腾讯云 COS / 七牛云 / S3 / Google Drive / WebDAV / FTP + 通过 rclone 集成 SFTP、Azure Blob、Dropbox、OneDrive 等 70+ 后端 |
|
||||||
| **自动调度** | Cron 定时 + 可视化编辑器 + 自动保留策略(按天数/份数清理) |
|
| **自动调度** | Cron 定时 + 可视化编辑器 + 自动保留策略(按天数/份数清理,自动回收空目录) |
|
||||||
| **多节点** | Master-Agent 集群,统一管理多台服务器的备份 |
|
| **多节点** | Master-Agent 集群,统一管理多台服务器的备份,支持远程目录浏览与节点编辑 |
|
||||||
| **安全** | JWT + bcrypt + AES-256-GCM 加密配置 + 可选备份文件加密 + 审计日志 |
|
| **安全** | JWT + bcrypt + AES-256-GCM 加密配置 + 可选备份文件加密 + 完整审计日志 |
|
||||||
| **通知** | 邮件 / Webhook / Telegram,备份成功或失败时自动推送 |
|
| **通知** | 邮件 / Webhook / Telegram,备份成功或失败时自动推送 |
|
||||||
| **部署** | 单二进制 + 内嵌 SQLite,Docker 一键启动,零外部依赖 |
|
| **部署** | 单二进制 + 内嵌 SQLite,Docker 一键启动,零外部依赖 |
|
||||||
|
|
||||||
@@ -120,8 +120,9 @@ make docker-cn # 或用国内镜像构建 Docker(goproxy.cn / npmmir
|
|||||||
| WebDAV | 服务器地址 + 用户名/密码 |
|
| WebDAV | 服务器地址 + 用户名/密码 |
|
||||||
| FTP | 主机 + 端口 + 用户名/密码 |
|
| FTP | 主机 + 端口 + 用户名/密码 |
|
||||||
| 本地磁盘 | 目标目录路径 |
|
| 本地磁盘 | 目标目录路径 |
|
||||||
|
| SFTP / Azure / Dropbox / OneDrive 等 | 选择对应类型后填写必填项,高级配置可折叠展开 |
|
||||||
|
|
||||||
> 国内云厂商只需填 Region 和 AccessKey,系统自动组装 Endpoint。
|
> 国内云厂商只需填 Region 和 AccessKey,系统自动组装 Endpoint。Rclone 类型的配置项按必填/可选分层展示,高级选项默认折叠。
|
||||||
|
|
||||||
添加后点击 **测试连接** 确认配置正确。
|
添加后点击 **测试连接** 确认配置正确。
|
||||||
|
|
||||||
@@ -131,10 +132,12 @@ make docker-cn # 或用国内镜像构建 Docker(goproxy.cn / npmmir
|
|||||||
|
|
||||||
1. **基础信息** — 任务名称、备份类型、Cron 表达式(留空则仅手动执行)
|
1. **基础信息** — 任务名称、备份类型、Cron 表达式(留空则仅手动执行)
|
||||||
2. **源配置** — 文件备份选择源路径(支持多个)、数据库备份填写连接信息
|
2. **源配置** — 文件备份选择源路径(支持多个)、数据库备份填写连接信息
|
||||||
3. **存储与策略** — 选择存储目标、压缩策略、保留天数、是否加密
|
3. **存储与策略** — 选择存储目标(支持多个)、压缩策略、保留天数、是否加密
|
||||||
|
|
||||||
保存后可以点击 **立即执行** 测试,在 **备份记录** 页面实时查看执行日志。
|
保存后可以点击 **立即执行** 测试,在 **备份记录** 页面实时查看执行日志。
|
||||||
|
|
||||||
|
> 删除备份任务时会自动清理远端存储上的备份文件,但保留备份记录以供审计追溯。
|
||||||
|
|
||||||
### 5. 配置通知(可选)
|
### 5. 配置通知(可选)
|
||||||
|
|
||||||
进入 **通知配置** 页面,支持邮件、Webhook、Telegram 三种方式,可分别配置成功/失败时是否推送。
|
进入 **通知配置** 页面,支持邮件、Webhook、Telegram 三种方式,可分别配置成功/失败时是否推送。
|
||||||
@@ -167,6 +170,8 @@ environment:
|
|||||||
- BACKUPX_BACKUP_MAX_CONCURRENT=4
|
- BACKUPX_BACKUP_MAX_CONCURRENT=4
|
||||||
```
|
```
|
||||||
|
|
||||||
|
版本更新:在 **系统设置** 页面点击「检查更新」查看是否有新版本,然后手动执行 `docker compose pull && docker compose up -d` 完成升级。
|
||||||
|
|
||||||
### 裸机部署
|
### 裸机部署
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -243,7 +248,10 @@ BackupX 支持 Master-Agent 模式管理多台服务器:
|
|||||||
2. 在远程服务器部署 Agent 并使用 Token 连接 Master
|
2. 在远程服务器部署 Agent 并使用 Token 连接 Master
|
||||||
3. 创建备份任务时选择对应节点,Master 自动下发任务
|
3. 创建备份任务时选择对应节点,Master 自动下发任务
|
||||||
|
|
||||||
创建文件备份任务时,可通过可视化目录浏览器远程选择 Agent 节点上的目录,无需手动输入路径。
|
- 本机节点自动检测 IP 地址和版本信息
|
||||||
|
- 远程节点通过 Agent 心跳上报系统信息(主机名、IP、OS、架构、版本)
|
||||||
|
- 支持在控制台直接编辑节点名称
|
||||||
|
- 创建文件备份任务时可通过目录浏览器远程选择 Agent 节点上的目录
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -268,7 +276,7 @@ make docker-cn # 国内 Docker 构建(镜像加速)
|
|||||||
### 发版
|
### 发版
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git tag v1.2.3 && git push --tags
|
git tag v1.4.3 && git push --tags
|
||||||
# GitHub Actions 自动:编译双架构二进制 → 发布 GitHub Release → 推送 Docker Hub 镜像
|
# GitHub Actions 自动:编译双架构二进制 → 发布 GitHub Release → 推送 Docker Hub 镜像
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -295,12 +303,16 @@ git tag v1.2.3 && git push --tags
|
|||||||
| | `POST /backup/records/:id/restore` | 恢复 |
|
| | `POST /backup/records/:id/restore` | 恢复 |
|
||||||
| **存储目标** | `GET\|POST /storage-targets` | 列表 / 添加 |
|
| **存储目标** | `GET\|POST /storage-targets` | 列表 / 添加 |
|
||||||
| | `POST /storage-targets/test` | 测试连接 |
|
| | `POST /storage-targets/test` | 测试连接 |
|
||||||
|
| | `GET /storage-targets/rclone/backends` | Rclone 后端列表 |
|
||||||
| **节点** | `GET\|POST /nodes` | 列表 / 添加 |
|
| **节点** | `GET\|POST /nodes` | 列表 / 添加 |
|
||||||
|
| | `PUT /nodes/:id` | 编辑节点 |
|
||||||
| | `GET /nodes/:id/fs/list` | 目录浏览 |
|
| | `GET /nodes/:id/fs/list` | 目录浏览 |
|
||||||
|
| | `POST /agent/heartbeat` | Agent 心跳(Token 认证) |
|
||||||
| **通知** | `GET\|POST /notifications` | 列表 / 添加 |
|
| **通知** | `GET\|POST /notifications` | 列表 / 添加 |
|
||||||
| **仪表盘** | `GET /dashboard/stats` | 概览统计 |
|
| **仪表盘** | `GET /dashboard/stats` | 概览统计 |
|
||||||
| **审计日志** | `GET /audit-logs` | 操作审计 |
|
| **审计日志** | `GET /audit-logs` | 操作审计 |
|
||||||
| **系统** | `GET /system/info` | 系统信息 |
|
| **系统** | `GET /system/info` | 系统信息 |
|
||||||
|
| | `GET /system/update-check` | 检查版本更新 |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -308,9 +320,9 @@ git tag v1.2.3 && git push --tags
|
|||||||
|
|
||||||
| 组件 | 技术 |
|
| 组件 | 技术 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| **后端** | Go · Gin · GORM · SQLite · robfig/cron |
|
| **后端** | Go · Gin · GORM · SQLite · robfig/cron · rclone |
|
||||||
| **前端** | React 18 · TypeScript · ArcoDesign · Vite · Zustand · ECharts |
|
| **前端** | React 18 · TypeScript · ArcoDesign · Vite · Zustand · ECharts |
|
||||||
| **存储** | AWS SDK v2 · Google Drive API v3 · gowebdav · jlaffaye/ftp |
|
| **存储** | rclone(70+ 后端)· AWS SDK v2 · Google Drive API v3 |
|
||||||
| **安全** | JWT · bcrypt · AES-256-GCM |
|
| **安全** | JWT · bcrypt · AES-256-GCM |
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|||||||
32
README_EN.md
32
README_EN.md
@@ -35,10 +35,10 @@
|
|||||||
| Capability | Details |
|
| Capability | Details |
|
||||||
|-----------|---------|
|
|-----------|---------|
|
||||||
| **Backup Types** | Files/Directories (multi-source), MySQL, PostgreSQL, SQLite, SAP HANA |
|
| **Backup Types** | Files/Directories (multi-source), MySQL, PostgreSQL, SQLite, SAP HANA |
|
||||||
| **Storage Backends** | Alibaba Cloud OSS, Tencent COS, Qiniu Kodo, S3-compatible (AWS/MinIO/R2), Google Drive, WebDAV, FTP/FTPS, Local Disk |
|
| **70+ Storage Backends** | Built-in Alibaba OSS / Tencent COS / Qiniu / S3 / Google Drive / WebDAV / FTP + 70+ backends via rclone (SFTP, Azure Blob, Dropbox, OneDrive, etc.) |
|
||||||
| **Scheduling** | Cron-based scheduling + visual editor + auto-retention policy (by days/count) |
|
| **Scheduling** | Cron-based + visual editor + auto-retention policy (by days/count, auto empty directory cleanup) |
|
||||||
| **Multi-Node** | Master-Agent cluster for managing backups across multiple servers |
|
| **Multi-Node** | Master-Agent cluster for managing backups across multiple servers with remote directory browsing and node editing |
|
||||||
| **Security** | JWT + bcrypt + AES-256-GCM encrypted config + optional backup encryption + audit logs |
|
| **Security** | JWT + bcrypt + AES-256-GCM encrypted config + optional backup encryption + comprehensive audit logs |
|
||||||
| **Notifications** | Email / Webhook / Telegram — push on success or failure |
|
| **Notifications** | Email / Webhook / Telegram — push on success or failure |
|
||||||
| **Deployment** | Single binary + embedded SQLite, Docker one-click, zero external dependencies |
|
| **Deployment** | Single binary + embedded SQLite, Docker one-click, zero external dependencies |
|
||||||
|
|
||||||
@@ -120,6 +120,9 @@ Go to **Storage Targets** → **Add**, choose a storage type and enter credentia
|
|||||||
| WebDAV | Server URL + Username/Password |
|
| WebDAV | Server URL + Username/Password |
|
||||||
| FTP | Host + Port + Username/Password |
|
| FTP | Host + Port + Username/Password |
|
||||||
| Local Disk | Target directory path |
|
| Local Disk | Target directory path |
|
||||||
|
| SFTP / Azure / Dropbox / OneDrive etc. | Select the type, fill in required fields; advanced options are collapsible |
|
||||||
|
|
||||||
|
> For Chinese cloud providers, just enter Region and AccessKey — the system auto-assembles the Endpoint. Rclone-type configs separate required fields from optional advanced options (collapsed by default).
|
||||||
|
|
||||||
Click **Test Connection** to verify.
|
Click **Test Connection** to verify.
|
||||||
|
|
||||||
@@ -129,10 +132,12 @@ Go to **Backup Tasks** → **Create**, complete 3 steps:
|
|||||||
|
|
||||||
1. **Basic Info** — Task name, backup type, Cron expression (leave empty for manual-only)
|
1. **Basic Info** — Task name, backup type, Cron expression (leave empty for manual-only)
|
||||||
2. **Source Config** — File backup: select source paths (supports multiple); Database: enter connection info
|
2. **Source Config** — File backup: select source paths (supports multiple); Database: enter connection info
|
||||||
3. **Storage & Policy** — Select storage target(s), compression, retention days, encryption toggle
|
3. **Storage & Policy** — Select storage target(s) (supports multiple), compression, retention days, encryption toggle
|
||||||
|
|
||||||
Save, then click **Run Now** to test. View real-time logs in **Backup Records**.
|
Save, then click **Run Now** to test. View real-time logs in **Backup Records**.
|
||||||
|
|
||||||
|
> Deleting a backup task automatically cleans up remote storage files while preserving backup records for audit purposes.
|
||||||
|
|
||||||
### 5. Set Up Notifications (Optional)
|
### 5. Set Up Notifications (Optional)
|
||||||
|
|
||||||
Go to **Notifications** to configure Email, Webhook, or Telegram alerts for backup success/failure.
|
Go to **Notifications** to configure Email, Webhook, or Telegram alerts for backup success/failure.
|
||||||
@@ -165,6 +170,8 @@ environment:
|
|||||||
- BACKUPX_BACKUP_MAX_CONCURRENT=4
|
- BACKUPX_BACKUP_MAX_CONCURRENT=4
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To upgrade: go to **System Settings**, click "Check for Updates" to see if a new version is available, then run `docker compose pull && docker compose up -d`.
|
||||||
|
|
||||||
### Bare Metal
|
### Bare Metal
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -239,7 +246,10 @@ BackupX supports Master-Agent mode for managing multiple servers:
|
|||||||
2. Deploy Agent on remote server, connect using the Token
|
2. Deploy Agent on remote server, connect using the Token
|
||||||
3. Create backup tasks and assign to specific nodes — Master dispatches automatically
|
3. Create backup tasks and assign to specific nodes — Master dispatches automatically
|
||||||
|
|
||||||
The visual directory browser lets you pick directories on remote Agent nodes — no manual path typing.
|
- Local node auto-detects IP address and version
|
||||||
|
- Remote nodes report system info via Agent heartbeat (hostname, IP, OS, architecture, version)
|
||||||
|
- Node names can be edited directly from the console
|
||||||
|
- Visual directory browser lets you pick directories on remote Agent nodes
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -264,7 +274,7 @@ make docker-cn # Docker build with China mirrors
|
|||||||
### Release
|
### Release
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git tag v1.2.3 && git push --tags
|
git tag v1.4.3 && git push --tags
|
||||||
# GitHub Actions: compile dual-arch binaries → publish GitHub Release → push Docker Hub image
|
# GitHub Actions: compile dual-arch binaries → publish GitHub Release → push Docker Hub image
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -291,12 +301,16 @@ All endpoints prefixed with `/api`, authenticated via JWT Bearer Token.
|
|||||||
| | `POST /backup/records/:id/restore` | Restore |
|
| | `POST /backup/records/:id/restore` | Restore |
|
||||||
| **Storage Targets** | `GET\|POST /storage-targets` | List / Add |
|
| **Storage Targets** | `GET\|POST /storage-targets` | List / Add |
|
||||||
| | `POST /storage-targets/test` | Test connection |
|
| | `POST /storage-targets/test` | Test connection |
|
||||||
|
| | `GET /storage-targets/rclone/backends` | Rclone backend list |
|
||||||
| **Nodes** | `GET\|POST /nodes` | List / Add |
|
| **Nodes** | `GET\|POST /nodes` | List / Add |
|
||||||
|
| | `PUT /nodes/:id` | Edit node |
|
||||||
| | `GET /nodes/:id/fs/list` | Directory browser |
|
| | `GET /nodes/:id/fs/list` | Directory browser |
|
||||||
|
| | `POST /agent/heartbeat` | Agent heartbeat (Token auth) |
|
||||||
| **Notifications** | `GET\|POST /notifications` | List / Add |
|
| **Notifications** | `GET\|POST /notifications` | List / Add |
|
||||||
| **Dashboard** | `GET /dashboard/stats` | Overview stats |
|
| **Dashboard** | `GET /dashboard/stats` | Overview stats |
|
||||||
| **Audit Logs** | `GET /audit-logs` | Operation audit |
|
| **Audit Logs** | `GET /audit-logs` | Operation audit |
|
||||||
| **System** | `GET /system/info` | System info |
|
| **System** | `GET /system/info` | System info |
|
||||||
|
| | `GET /system/update-check` | Check for updates |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -304,9 +318,9 @@ All endpoints prefixed with `/api`, authenticated via JWT Bearer Token.
|
|||||||
|
|
||||||
| Component | Technology |
|
| Component | Technology |
|
||||||
|-----------|-----------|
|
|-----------|-----------|
|
||||||
| **Backend** | Go · Gin · GORM · SQLite · robfig/cron |
|
| **Backend** | Go · Gin · GORM · SQLite · robfig/cron · rclone |
|
||||||
| **Frontend** | React 18 · TypeScript · ArcoDesign · Vite · Zustand · ECharts |
|
| **Frontend** | React 18 · TypeScript · ArcoDesign · Vite · Zustand · ECharts |
|
||||||
| **Storage** | AWS SDK v2 · Google Drive API v3 · gowebdav · jlaffaye/ftp |
|
| **Storage** | rclone (70+ backends) · AWS SDK v2 · Google Drive API v3 |
|
||||||
| **Security** | JWT · bcrypt · AES-256-GCM |
|
| **Security** | JWT · bcrypt · AES-256-GCM |
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ services:
|
|||||||
- "8340:8340"
|
- "8340:8340"
|
||||||
volumes:
|
volumes:
|
||||||
- backupx-data:/app/data
|
- backupx-data:/app/data
|
||||||
|
- /var/run/docker.sock:/var/run/docker.sock # 支持 Web 一键更新
|
||||||
# 挂载需要备份的宿主机目录(按需添加,:ro 表示只读):
|
# 挂载需要备份的宿主机目录(按需添加,:ro 表示只读):
|
||||||
# - /var/www:/mnt/www:ro
|
# - /var/www:/mnt/www:ro
|
||||||
# - /etc/nginx:/mnt/nginx-conf:ro
|
# - /etc/nginx:/mnt/nginx-conf:ro
|
||||||
|
|||||||
@@ -73,10 +73,13 @@ func New(ctx context.Context, cfg config.Config, version string) (*Application,
|
|||||||
storageRclone.NewFTPFactory(),
|
storageRclone.NewFTPFactory(),
|
||||||
storageRclone.NewRcloneFactory(),
|
storageRclone.NewRcloneFactory(),
|
||||||
)
|
)
|
||||||
|
// 将全部 rclone 后端注册为独立存储类型(sftp、azureblob、dropbox 等与 s3、ftp 完全平级)
|
||||||
|
storageRclone.RegisterAllBackends(storageRegistry)
|
||||||
storageTargetService := service.NewStorageTargetService(storageTargetRepo, oauthSessionRepo, storageRegistry, configCipher)
|
storageTargetService := service.NewStorageTargetService(storageTargetRepo, oauthSessionRepo, storageRegistry, configCipher)
|
||||||
storageTargetService.SetBackupTaskRepository(backupTaskRepo)
|
storageTargetService.SetBackupTaskRepository(backupTaskRepo)
|
||||||
storageTargetService.SetBackupRecordRepository(backupRecordRepo)
|
storageTargetService.SetBackupRecordRepository(backupRecordRepo)
|
||||||
backupTaskService := service.NewBackupTaskService(backupTaskRepo, storageTargetRepo, configCipher)
|
backupTaskService := service.NewBackupTaskService(backupTaskRepo, storageTargetRepo, configCipher)
|
||||||
|
backupTaskService.SetRecordsAndStorage(backupRecordRepo, storageRegistry)
|
||||||
backupRunnerRegistry := backup.NewRegistry(backup.NewFileRunner(), backup.NewSQLiteRunner(), backup.NewMySQLRunner(nil), backup.NewPostgreSQLRunner(nil), backup.NewSAPHANARunner(nil))
|
backupRunnerRegistry := backup.NewRegistry(backup.NewFileRunner(), backup.NewSQLiteRunner(), backup.NewMySQLRunner(nil), backup.NewPostgreSQLRunner(nil), backup.NewSAPHANARunner(nil))
|
||||||
logHub := backup.NewLogHub()
|
logHub := backup.NewLogHub()
|
||||||
retentionService := backupretention.NewService(backupRecordRepo)
|
retentionService := backupretention.NewService(backupRecordRepo)
|
||||||
@@ -92,6 +95,7 @@ func New(ctx context.Context, cfg config.Config, version string) (*Application,
|
|||||||
backupExecutionService := service.NewBackupExecutionService(backupTaskRepo, backupRecordRepo, storageTargetRepo, storageRegistry, backupRunnerRegistry, logHub, retentionService, configCipher, notificationService, cfg.Backup.TempDir, cfg.Backup.MaxConcurrent, cfg.Backup.Retries, cfg.Backup.BandwidthLimit)
|
backupExecutionService := service.NewBackupExecutionService(backupTaskRepo, backupRecordRepo, storageTargetRepo, storageRegistry, backupRunnerRegistry, logHub, retentionService, configCipher, notificationService, cfg.Backup.TempDir, cfg.Backup.MaxConcurrent, cfg.Backup.Retries, cfg.Backup.BandwidthLimit)
|
||||||
schedulerService := scheduler.NewService(backupTaskRepo, backupExecutionService, appLogger)
|
schedulerService := scheduler.NewService(backupTaskRepo, backupExecutionService, appLogger)
|
||||||
backupTaskService.SetScheduler(schedulerService)
|
backupTaskService.SetScheduler(schedulerService)
|
||||||
|
// 审计日志注入延迟到 auditService 创建后(见下方)
|
||||||
backupRecordService := service.NewBackupRecordService(backupRecordRepo, backupExecutionService, logHub)
|
backupRecordService := service.NewBackupRecordService(backupRecordRepo, backupExecutionService, logHub)
|
||||||
dashboardService := service.NewDashboardService(backupTaskRepo, backupRecordRepo, storageTargetRepo)
|
dashboardService := service.NewDashboardService(backupTaskRepo, backupRecordRepo, storageTargetRepo)
|
||||||
settingsService := service.NewSettingsService(systemConfigRepo)
|
settingsService := service.NewSettingsService(systemConfigRepo)
|
||||||
@@ -100,13 +104,14 @@ func New(ctx context.Context, cfg config.Config, version string) (*Application,
|
|||||||
auditLogRepo := repository.NewAuditLogRepository(db)
|
auditLogRepo := repository.NewAuditLogRepository(db)
|
||||||
auditService := service.NewAuditService(auditLogRepo)
|
auditService := service.NewAuditService(auditLogRepo)
|
||||||
authService.SetAuditService(auditService)
|
authService.SetAuditService(auditService)
|
||||||
|
schedulerService.SetAuditRecorder(auditService)
|
||||||
|
|
||||||
// Database discovery
|
// Database discovery
|
||||||
databaseDiscoveryService := service.NewDatabaseDiscoveryService(backup.NewOSCommandExecutor())
|
databaseDiscoveryService := service.NewDatabaseDiscoveryService(backup.NewOSCommandExecutor())
|
||||||
|
|
||||||
// Cluster: Node management
|
// Cluster: Node management
|
||||||
nodeRepo := repository.NewNodeRepository(db)
|
nodeRepo := repository.NewNodeRepository(db)
|
||||||
nodeService := service.NewNodeService(nodeRepo)
|
nodeService := service.NewNodeService(nodeRepo, version)
|
||||||
if err := nodeService.EnsureLocalNode(ctx); err != nil {
|
if err := nodeService.EnsureLocalNode(ctx); err != nil {
|
||||||
appLogger.Warn("failed to ensure local node", zap.Error(err))
|
appLogger.Warn("failed to ensure local node", zap.Error(err))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,28 @@ import (
|
|||||||
"backupx/server/internal/storage"
|
"backupx/server/internal/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// collectDirPrefixes 从待删除的记录中提取唯一的父目录前缀。
|
||||||
|
func collectDirPrefixes(records []model.BackupRecord) []string {
|
||||||
|
seen := make(map[string]struct{})
|
||||||
|
var prefixes []string
|
||||||
|
for _, record := range records {
|
||||||
|
path := strings.TrimSpace(record.StoragePath)
|
||||||
|
if path == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
idx := strings.LastIndex(path, "/")
|
||||||
|
if idx <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dir := path[:idx]
|
||||||
|
if _, ok := seen[dir]; !ok {
|
||||||
|
seen[dir] = struct{}{}
|
||||||
|
prefixes = append(prefixes, dir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return prefixes
|
||||||
|
}
|
||||||
|
|
||||||
type CleanupResult struct {
|
type CleanupResult struct {
|
||||||
DeletedRecords int
|
DeletedRecords int
|
||||||
DeletedObjects int
|
DeletedObjects int
|
||||||
@@ -54,6 +76,17 @@ func (s *Service) Cleanup(ctx context.Context, task *model.BackupTask, provider
|
|||||||
}
|
}
|
||||||
result.DeletedRecords++
|
result.DeletedRecords++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 清理空目录:收集被删除文件的父目录,尝试移除空目录
|
||||||
|
if dirCleaner, ok := provider.(storage.StorageDirCleaner); ok && result.DeletedObjects > 0 {
|
||||||
|
prefixes := collectDirPrefixes(candidates)
|
||||||
|
for _, prefix := range prefixes {
|
||||||
|
if err := dirCleaner.RemoveEmptyDirs(ctx, prefix); err != nil {
|
||||||
|
result.Warnings = append(result.Warnings, fmt.Sprintf("cleanup empty dirs for %s: %v", prefix, err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ func (r *fakeRecordRepository) Delete(_ context.Context, id uint) error {
|
|||||||
func (r *fakeRecordRepository) ListRecent(context.Context, int) ([]model.BackupRecord, error) {
|
func (r *fakeRecordRepository) ListRecent(context.Context, int) ([]model.BackupRecord, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
func (r *fakeRecordRepository) ListByTask(_ context.Context, _ uint) ([]model.BackupRecord, error) {
|
||||||
|
return r.records, nil
|
||||||
|
}
|
||||||
func (r *fakeRecordRepository) ListSuccessfulByTask(_ context.Context, _ uint) ([]model.BackupRecord, error) {
|
func (r *fakeRecordRepository) ListSuccessfulByTask(_ context.Context, _ uint) ([]model.BackupRecord, error) {
|
||||||
return r.records, nil
|
return r.records, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,7 +130,8 @@ func (h *BackupRecordHandler) Restore(c *gin.Context) {
|
|||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
recordAudit(c, h.auditService, "backup_record", "restore", "backup_record", fmt.Sprintf("%d", id), "", fmt.Sprintf("恢复备份记录 #%d", id))
|
recordAudit(c, h.auditService, "backup_record", "restore", "backup_record", fmt.Sprintf("%d", id), "",
|
||||||
|
fmt.Sprintf("恢复备份记录 (ID: %d)", id))
|
||||||
response.Success(c, gin.H{"restored": true})
|
response.Success(c, gin.H{"restored": true})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,10 +144,29 @@ func (h *BackupRecordHandler) Delete(c *gin.Context) {
|
|||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
recordAudit(c, h.auditService, "backup_record", "delete", "backup_record", fmt.Sprintf("%d", id), "", fmt.Sprintf("删除备份记录 #%d", id))
|
recordAudit(c, h.auditService, "backup_record", "delete", "backup_record", fmt.Sprintf("%d", id), "",
|
||||||
|
fmt.Sprintf("删除备份记录 (ID: %d)", id))
|
||||||
response.Success(c, gin.H{"deleted": true})
|
response.Success(c, gin.H{"deleted": true})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *BackupRecordHandler) BatchDelete(c *gin.Context) {
|
||||||
|
var input struct {
|
||||||
|
IDs []uint `json:"ids" binding:"required,min=1"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&input); err != nil {
|
||||||
|
response.Error(c, apperror.BadRequest("BACKUP_RECORD_BATCH_INVALID", "批量删除参数不合法", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
deleted := 0
|
||||||
|
for _, id := range input.IDs {
|
||||||
|
if err := h.service.Delete(c.Request.Context(), id); err == nil {
|
||||||
|
deleted++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
recordAudit(c, h.auditService, "backup_record", "batch_delete", "backup_record", "", "", fmt.Sprintf("批量删除 %d 条备份记录", deleted))
|
||||||
|
response.Success(c, gin.H{"deleted": deleted})
|
||||||
|
}
|
||||||
|
|
||||||
func buildRecordFilter(c *gin.Context) (service.BackupRecordListInput, error) {
|
func buildRecordFilter(c *gin.Context) (service.BackupRecordListInput, error) {
|
||||||
var filter service.BackupRecordListInput
|
var filter service.BackupRecordListInput
|
||||||
if taskIDValue := strings.TrimSpace(c.Query("taskId")); taskIDValue != "" {
|
if taskIDValue := strings.TrimSpace(c.Query("taskId")); taskIDValue != "" {
|
||||||
|
|||||||
@@ -14,6 +14,19 @@ type BackupTaskHandler struct {
|
|||||||
auditService *service.AuditService
|
auditService *service.AuditService
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// describeTaskInput 提取审计日志中通用的调度和存储目标描述。
|
||||||
|
func describeTaskInput(input service.BackupTaskUpsertInput) (cronDesc string, storageCount int) {
|
||||||
|
cronDesc = "仅手动执行"
|
||||||
|
if input.CronExpr != "" {
|
||||||
|
cronDesc = input.CronExpr
|
||||||
|
}
|
||||||
|
storageCount = len(input.StorageTargetIDs)
|
||||||
|
if storageCount == 0 && input.StorageTargetID > 0 {
|
||||||
|
storageCount = 1
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func NewBackupTaskHandler(taskService *service.BackupTaskService, auditService *service.AuditService) *BackupTaskHandler {
|
func NewBackupTaskHandler(taskService *service.BackupTaskService, auditService *service.AuditService) *BackupTaskHandler {
|
||||||
return &BackupTaskHandler{service: taskService, auditService: auditService}
|
return &BackupTaskHandler{service: taskService, auditService: auditService}
|
||||||
}
|
}
|
||||||
@@ -51,7 +64,9 @@ func (h *BackupTaskHandler) Create(c *gin.Context) {
|
|||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
recordAudit(c, h.auditService, "backup_task", "create", "backup_task", fmt.Sprintf("%d", item.ID), item.Name, fmt.Sprintf("类型: %s", input.Type))
|
cronDesc, storageCount := describeTaskInput(input)
|
||||||
|
recordAudit(c, h.auditService, "backup_task", "create", "backup_task", fmt.Sprintf("%d", item.ID), item.Name,
|
||||||
|
fmt.Sprintf("创建备份任务「%s」,类型: %s, 调度: %s, 存储: %d 个目标", item.Name, input.Type, cronDesc, storageCount))
|
||||||
response.Success(c, item)
|
response.Success(c, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +85,9 @@ func (h *BackupTaskHandler) Update(c *gin.Context) {
|
|||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
recordAudit(c, h.auditService, "backup_task", "update", "backup_task", fmt.Sprintf("%d", item.ID), item.Name, fmt.Sprintf("类型: %s, Cron: %s", input.Type, input.CronExpr))
|
updCronDesc, updStorageCount := describeTaskInput(input)
|
||||||
|
recordAudit(c, h.auditService, "backup_task", "update", "backup_task", fmt.Sprintf("%d", item.ID), item.Name,
|
||||||
|
fmt.Sprintf("更新备份任务「%s」,类型: %s, 调度: %s, 存储: %d 个目标", item.Name, input.Type, updCronDesc, updStorageCount))
|
||||||
response.Success(c, item)
|
response.Success(c, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,11 +96,13 @@ func (h *BackupTaskHandler) Delete(c *gin.Context) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := h.service.Delete(c.Request.Context(), id); err != nil {
|
result, err := h.service.Delete(c.Request.Context(), id)
|
||||||
|
if err != nil {
|
||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
recordAudit(c, h.auditService, "backup_task", "delete", "backup_task", fmt.Sprintf("%d", id), "", fmt.Sprintf("删除备份任务 #%d", id))
|
recordAudit(c, h.auditService, "backup_task", "delete", "backup_task", fmt.Sprintf("%d", id), result.TaskName,
|
||||||
|
fmt.Sprintf("删除备份任务「%s」(ID: %d),关联记录 %d 条,已清理远端文件 %d 个", result.TaskName, id, result.RecordCount, result.CleanedFiles))
|
||||||
response.Success(c, gin.H{"deleted": true})
|
response.Success(c, gin.H{"deleted": true})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,9 +131,12 @@ func (h *BackupTaskHandler) Toggle(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
action := "enable"
|
action := "enable"
|
||||||
|
actionLabel := "启用"
|
||||||
if !enabled {
|
if !enabled {
|
||||||
action = "disable"
|
action = "disable"
|
||||||
|
actionLabel = "停用"
|
||||||
}
|
}
|
||||||
recordAudit(c, h.auditService, "backup_task", action, "backup_task", fmt.Sprintf("%d", id), item.Name, fmt.Sprintf("%s 备份任务", action))
|
recordAudit(c, h.auditService, "backup_task", action, "backup_task", fmt.Sprintf("%d", id), item.Name,
|
||||||
|
fmt.Sprintf("%s备份任务「%s」", actionLabel, item.Name))
|
||||||
response.Success(c, item)
|
response.Success(c, item)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
stdhttp "net/http"
|
stdhttp "net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -10,11 +11,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type NodeHandler struct {
|
type NodeHandler struct {
|
||||||
service *service.NodeService
|
service *service.NodeService
|
||||||
|
auditService *service.AuditService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeHandler(service *service.NodeService) *NodeHandler {
|
func NewNodeHandler(service *service.NodeService, auditService *service.AuditService) *NodeHandler {
|
||||||
return &NodeHandler{service: service}
|
return &NodeHandler{service: service, auditService: auditService}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *NodeHandler) List(c *gin.Context) {
|
func (h *NodeHandler) List(c *gin.Context) {
|
||||||
@@ -51,6 +53,8 @@ func (h *NodeHandler) Create(c *gin.Context) {
|
|||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
recordAudit(c, h.auditService, "node", "create", "node", "", input.Name,
|
||||||
|
fmt.Sprintf("创建远程节点「%s」", input.Name))
|
||||||
response.Success(c, gin.H{"token": token})
|
response.Success(c, gin.H{"token": token})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,6 +68,8 @@ func (h *NodeHandler) Delete(c *gin.Context) {
|
|||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
recordAudit(c, h.auditService, "node", "delete", "node", fmt.Sprintf("%d", id), "",
|
||||||
|
fmt.Sprintf("删除节点 (ID: %d)", id))
|
||||||
response.Success(c, nil)
|
response.Success(c, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,18 +88,41 @@ func (h *NodeHandler) ListDirectory(c *gin.Context) {
|
|||||||
response.Success(c, entries)
|
response.Success(c, entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *NodeHandler) Update(c *gin.Context) {
|
||||||
|
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
response.Error(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var input service.NodeUpdateInput
|
||||||
|
if err := c.ShouldBindJSON(&input); err != nil {
|
||||||
|
c.JSON(stdhttp.StatusBadRequest, gin.H{"code": "INVALID_INPUT", "message": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
item, err := h.service.Update(c.Request.Context(), uint(id), input)
|
||||||
|
if err != nil {
|
||||||
|
response.Error(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
recordAudit(c, h.auditService, "node", "update", "node", fmt.Sprintf("%d", id), item.Name,
|
||||||
|
fmt.Sprintf("更新节点「%s」(ID: %d)", item.Name, id))
|
||||||
|
response.Success(c, item)
|
||||||
|
}
|
||||||
|
|
||||||
func (h *NodeHandler) Heartbeat(c *gin.Context) {
|
func (h *NodeHandler) Heartbeat(c *gin.Context) {
|
||||||
var input struct {
|
var input struct {
|
||||||
Token string `json:"token" binding:"required"`
|
Token string `json:"token" binding:"required"`
|
||||||
Hostname string `json:"hostname"`
|
Hostname string `json:"hostname"`
|
||||||
IPAddress string `json:"ipAddress"`
|
IPAddress string `json:"ipAddress"`
|
||||||
AgentVersion string `json:"agentVersion"`
|
AgentVersion string `json:"agentVersion"`
|
||||||
|
OS string `json:"os"`
|
||||||
|
Arch string `json:"arch"`
|
||||||
}
|
}
|
||||||
if err := c.ShouldBindJSON(&input); err != nil {
|
if err := c.ShouldBindJSON(&input); err != nil {
|
||||||
c.JSON(stdhttp.StatusBadRequest, gin.H{"code": "INVALID_INPUT", "message": err.Error()})
|
c.JSON(stdhttp.StatusBadRequest, gin.H{"code": "INVALID_INPUT", "message": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := h.service.Heartbeat(c.Request.Context(), input.Token, input.Hostname, input.IPAddress, input.AgentVersion); err != nil {
|
if err := h.service.Heartbeat(c.Request.Context(), input.Token, input.Hostname, input.IPAddress, input.AgentVersion, input.OS, input.Arch); err != nil {
|
||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ func NewRouter(deps RouterDependencies) *gin.Engine {
|
|||||||
system := api.Group("/system")
|
system := api.Group("/system")
|
||||||
system.Use(AuthMiddleware(deps.JWTManager))
|
system.Use(AuthMiddleware(deps.JWTManager))
|
||||||
system.GET("/info", systemHandler.Info)
|
system.GET("/info", systemHandler.Info)
|
||||||
|
system.GET("/update-check", systemHandler.CheckUpdate)
|
||||||
|
|
||||||
storageTargets := api.Group("/storage-targets")
|
storageTargets := api.Group("/storage-targets")
|
||||||
storageTargets.Use(AuthMiddleware(deps.JWTManager))
|
storageTargets.Use(AuthMiddleware(deps.JWTManager))
|
||||||
@@ -106,6 +107,7 @@ func NewRouter(deps RouterDependencies) *gin.Engine {
|
|||||||
backupRecords.GET("/:id/logs/stream", backupRecordHandler.StreamLogs)
|
backupRecords.GET("/:id/logs/stream", backupRecordHandler.StreamLogs)
|
||||||
backupRecords.GET("/:id/download", backupRecordHandler.Download)
|
backupRecords.GET("/:id/download", backupRecordHandler.Download)
|
||||||
backupRecords.POST("/:id/restore", backupRecordHandler.Restore)
|
backupRecords.POST("/:id/restore", backupRecordHandler.Restore)
|
||||||
|
backupRecords.POST("/batch-delete", backupRecordHandler.BatchDelete)
|
||||||
backupRecords.DELETE("/:id", backupRecordHandler.Delete)
|
backupRecords.DELETE("/:id", backupRecordHandler.Delete)
|
||||||
dashboard := api.Group("/dashboard")
|
dashboard := api.Group("/dashboard")
|
||||||
dashboard.Use(AuthMiddleware(deps.JWTManager))
|
dashboard.Use(AuthMiddleware(deps.JWTManager))
|
||||||
@@ -138,12 +140,13 @@ func NewRouter(deps RouterDependencies) *gin.Engine {
|
|||||||
database.POST("/discover", databaseHandler.Discover)
|
database.POST("/discover", databaseHandler.Discover)
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeHandler := NewNodeHandler(deps.NodeService)
|
nodeHandler := NewNodeHandler(deps.NodeService, deps.AuditService)
|
||||||
nodes := api.Group("/nodes")
|
nodes := api.Group("/nodes")
|
||||||
nodes.Use(AuthMiddleware(deps.JWTManager))
|
nodes.Use(AuthMiddleware(deps.JWTManager))
|
||||||
nodes.GET("", nodeHandler.List)
|
nodes.GET("", nodeHandler.List)
|
||||||
nodes.GET("/:id", nodeHandler.Get)
|
nodes.GET("/:id", nodeHandler.Get)
|
||||||
nodes.POST("", nodeHandler.Create)
|
nodes.POST("", nodeHandler.Create)
|
||||||
|
nodes.PUT("/:id", nodeHandler.Update)
|
||||||
nodes.DELETE("/:id", nodeHandler.Delete)
|
nodes.DELETE("/:id", nodeHandler.Delete)
|
||||||
nodes.GET("/:id/fs/list", nodeHandler.ListDirectory)
|
nodes.GET("/:id/fs/list", nodeHandler.ListDirectory)
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,8 @@ func (h *StorageTargetHandler) Create(c *gin.Context) {
|
|||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
recordAudit(c, h.auditService, "storage_target", "create", "storage_target", fmt.Sprintf("%d", item.ID), item.Name, fmt.Sprintf("类型: %s", input.Type))
|
recordAudit(c, h.auditService, "storage_target", "create", "storage_target", fmt.Sprintf("%d", item.ID), item.Name,
|
||||||
|
fmt.Sprintf("创建存储目标「%s」,类型: %s", item.Name, input.Type))
|
||||||
response.Success(c, item)
|
response.Success(c, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +85,8 @@ func (h *StorageTargetHandler) Update(c *gin.Context) {
|
|||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
recordAudit(c, h.auditService, "storage_target", "update", "storage_target", fmt.Sprintf("%d", item.ID), item.Name, fmt.Sprintf("类型: %s", input.Type))
|
recordAudit(c, h.auditService, "storage_target", "update", "storage_target", fmt.Sprintf("%d", item.ID), item.Name,
|
||||||
|
fmt.Sprintf("更新存储目标「%s」,类型: %s", item.Name, input.Type))
|
||||||
response.Success(c, item)
|
response.Success(c, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +99,8 @@ func (h *StorageTargetHandler) Delete(c *gin.Context) {
|
|||||||
response.Error(c, err)
|
response.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
recordAudit(c, h.auditService, "storage_target", "delete", "storage_target", fmt.Sprintf("%d", id), "", fmt.Sprintf("删除存储目标 #%d", id))
|
recordAudit(c, h.auditService, "storage_target", "delete", "storage_target", fmt.Sprintf("%d", id), "",
|
||||||
|
fmt.Sprintf("删除存储目标 (ID: %d)", id))
|
||||||
response.Success(c, gin.H{"deleted": true})
|
response.Success(c, gin.H{"deleted": true})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,3 +17,17 @@ func NewSystemHandler(systemService *service.SystemService) *SystemHandler {
|
|||||||
func (h *SystemHandler) Info(c *gin.Context) {
|
func (h *SystemHandler) Info(c *gin.Context) {
|
||||||
response.Success(c, h.systemService.GetInfo(c.Request.Context()))
|
response.Success(c, h.systemService.GetInfo(c.Request.Context()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *SystemHandler) CheckUpdate(c *gin.Context) {
|
||||||
|
result, err := h.systemService.CheckUpdate(c.Request.Context())
|
||||||
|
if err != nil {
|
||||||
|
// 即使检查失败也返回当前版本信息
|
||||||
|
response.Success(c, gin.H{
|
||||||
|
"currentVersion": result.CurrentVersion,
|
||||||
|
"hasUpdate": false,
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.Success(c, result)
|
||||||
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ type BackupRecordRepository interface {
|
|||||||
Update(context.Context, *model.BackupRecord) error
|
Update(context.Context, *model.BackupRecord) error
|
||||||
Delete(context.Context, uint) error
|
Delete(context.Context, uint) error
|
||||||
ListRecent(context.Context, int) ([]model.BackupRecord, error)
|
ListRecent(context.Context, int) ([]model.BackupRecord, error)
|
||||||
|
ListByTask(context.Context, uint) ([]model.BackupRecord, error)
|
||||||
ListSuccessfulByTask(context.Context, uint) ([]model.BackupRecord, error)
|
ListSuccessfulByTask(context.Context, uint) ([]model.BackupRecord, error)
|
||||||
Count(context.Context) (int64, error)
|
Count(context.Context) (int64, error)
|
||||||
CountSince(context.Context, time.Time) (int64, error)
|
CountSince(context.Context, time.Time) (int64, error)
|
||||||
@@ -115,6 +116,14 @@ func (r *GormBackupRecordRepository) ListRecent(ctx context.Context, limit int)
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *GormBackupRecordRepository) ListByTask(ctx context.Context, taskID uint) ([]model.BackupRecord, error) {
|
||||||
|
var items []model.BackupRecord
|
||||||
|
if err := r.db.WithContext(ctx).Where("task_id = ?", taskID).Order("id desc").Find(&items).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *GormBackupRecordRepository) ListSuccessfulByTask(ctx context.Context, taskID uint) ([]model.BackupRecord, error) {
|
func (r *GormBackupRecordRepository) ListSuccessfulByTask(ctx context.Context, taskID uint) ([]model.BackupRecord, error) {
|
||||||
var items []model.BackupRecord
|
var items []model.BackupRecord
|
||||||
if err := r.db.WithContext(ctx).Where("task_id = ? AND status = ?", taskID, "success").Order("completed_at desc, id desc").Find(&items).Error; err != nil {
|
if err := r.db.WithContext(ctx).Where("task_id = ? AND status = ?", taskID, "success").Order("completed_at desc, id desc").Find(&items).Error; err != nil {
|
||||||
|
|||||||
@@ -17,12 +17,18 @@ type TaskRunner interface {
|
|||||||
RunTaskByID(context.Context, uint) (*servicepkg.BackupRecordDetail, error)
|
RunTaskByID(context.Context, uint) (*servicepkg.BackupRecordDetail, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AuditRecorder 记录审计日志(可选依赖)
|
||||||
|
type AuditRecorder interface {
|
||||||
|
Record(servicepkg.AuditEntry)
|
||||||
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
cron *cron.Cron
|
cron *cron.Cron
|
||||||
tasks repository.BackupTaskRepository
|
tasks repository.BackupTaskRepository
|
||||||
runner TaskRunner
|
runner TaskRunner
|
||||||
logger *zap.Logger
|
logger *zap.Logger
|
||||||
|
audit AuditRecorder
|
||||||
entries map[uint]cron.EntryID
|
entries map[uint]cron.EntryID
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,6 +37,8 @@ func NewService(tasks repository.BackupTaskRepository, runner TaskRunner, logger
|
|||||||
return &Service{cron: cron.New(cron.WithParser(parser), cron.WithLocation(time.UTC)), tasks: tasks, runner: runner, logger: logger, entries: make(map[uint]cron.EntryID)}
|
return &Service{cron: cron.New(cron.WithParser(parser), cron.WithLocation(time.UTC)), tasks: tasks, runner: runner, logger: logger, entries: make(map[uint]cron.EntryID)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetAuditRecorder(audit AuditRecorder) { s.audit = audit }
|
||||||
|
|
||||||
func (s *Service) Start(ctx context.Context) error {
|
func (s *Service) Start(ctx context.Context) error {
|
||||||
if err := s.Reload(ctx); err != nil {
|
if err := s.Reload(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -96,9 +104,19 @@ func (s *Service) syncTaskLocked(task *model.BackupTask) error {
|
|||||||
if !task.Enabled || task.CronExpr == "" {
|
if !task.Enabled || task.CronExpr == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
taskID := task.ID
|
||||||
|
taskName := task.Name
|
||||||
entryID, err := s.cron.AddFunc(task.CronExpr, func() {
|
entryID, err := s.cron.AddFunc(task.CronExpr, func() {
|
||||||
if _, runErr := s.runner.RunTaskByID(context.Background(), task.ID); runErr != nil && s.logger != nil {
|
// 自动调度任务记录审计日志
|
||||||
s.logger.Warn("scheduled backup run failed", zap.Uint("task_id", task.ID), zap.Error(runErr))
|
if s.audit != nil {
|
||||||
|
s.audit.Record(servicepkg.AuditEntry{
|
||||||
|
Username: "system", Category: "backup_task", Action: "scheduled_run",
|
||||||
|
TargetType: "backup_task", TargetID: fmt.Sprintf("%d", taskID),
|
||||||
|
TargetName: taskName, Detail: fmt.Sprintf("定时调度触发备份任务: %s (cron: %s)", taskName, task.CronExpr),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if _, runErr := s.runner.RunTaskByID(context.Background(), taskID); runErr != nil && s.logger != nil {
|
||||||
|
s.logger.Warn("scheduled backup run failed", zap.Uint("task_id", taskID), zap.Error(runErr))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -363,33 +363,46 @@ func (s *BackupExecutionService) executeTask(ctx context.Context, task *model.Ba
|
|||||||
logger.Warnf("存储目标 %s 创建客户端失败:%v", targetName, resolveErr)
|
logger.Warnf("存储目标 %s 创建客户端失败:%v", targetName, resolveErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
artifact, openErr := os.Open(finalPath)
|
|
||||||
if openErr != nil {
|
|
||||||
uploadResults[index] = StorageUploadResultItem{StorageTargetID: targetID, StorageTargetName: targetName, Status: "failed", Error: openErr.Error()}
|
|
||||||
logger.Warnf("存储目标 %s 打开备份文件失败:%v", targetName, openErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer artifact.Close()
|
|
||||||
logger.Infof("开始上传备份到存储目标:%s", targetName)
|
logger.Infof("开始上传备份到存储目标:%s", targetName)
|
||||||
// hashingReader: 上传过程中同步计算字节数 + SHA-256,单次读取零额外 I/O
|
// 上传级重试:最多 3 次,指数退避(10s, 30s, 90s)
|
||||||
hr := newHashingReader(artifact)
|
maxAttempts := 3
|
||||||
// progressReader: 包装 hashingReader,通过 LogHub 推送实时上传进度
|
var lastUploadErr error
|
||||||
pr := newProgressReader(hr, fileSize, func(bytesRead int64, speedBps float64) {
|
var hr *hashingReader
|
||||||
percent := float64(0)
|
for attempt := 1; attempt <= maxAttempts; attempt++ {
|
||||||
if fileSize > 0 {
|
if attempt > 1 {
|
||||||
percent = float64(bytesRead) / float64(fileSize) * 100
|
backoff := time.Duration(attempt*attempt) * 10 * time.Second
|
||||||
|
logger.Warnf("存储目标 %s 第 %d 次重试(等待 %v):%v", targetName, attempt, backoff, lastUploadErr)
|
||||||
|
time.Sleep(backoff)
|
||||||
}
|
}
|
||||||
s.logHub.AppendProgress(recordID, backup.ProgressInfo{
|
artifact, openErr := os.Open(finalPath)
|
||||||
BytesSent: bytesRead,
|
if openErr != nil {
|
||||||
TotalBytes: fileSize,
|
uploadResults[index] = StorageUploadResultItem{StorageTargetID: targetID, StorageTargetName: targetName, Status: "failed", Error: openErr.Error()}
|
||||||
Percent: percent,
|
logger.Warnf("存储目标 %s 打开备份文件失败:%v", targetName, openErr)
|
||||||
SpeedBps: speedBps,
|
return
|
||||||
TargetName: targetName,
|
}
|
||||||
|
hr = newHashingReader(artifact)
|
||||||
|
pr := newProgressReader(hr, fileSize, func(bytesRead int64, speedBps float64) {
|
||||||
|
percent := float64(0)
|
||||||
|
if fileSize > 0 {
|
||||||
|
percent = float64(bytesRead) / float64(fileSize) * 100
|
||||||
|
}
|
||||||
|
s.logHub.AppendProgress(recordID, backup.ProgressInfo{
|
||||||
|
BytesSent: bytesRead,
|
||||||
|
TotalBytes: fileSize,
|
||||||
|
Percent: percent,
|
||||||
|
SpeedBps: speedBps,
|
||||||
|
TargetName: targetName,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
lastUploadErr = provider.Upload(ctx, storagePath, pr, fileSize, map[string]string{"taskId": fmt.Sprintf("%d", task.ID), "recordId": fmt.Sprintf("%d", recordID)})
|
||||||
if uploadErr := provider.Upload(ctx, storagePath, pr, fileSize, map[string]string{"taskId": fmt.Sprintf("%d", task.ID), "recordId": fmt.Sprintf("%d", recordID)}); uploadErr != nil {
|
artifact.Close()
|
||||||
uploadResults[index] = StorageUploadResultItem{StorageTargetID: targetID, StorageTargetName: targetName, Status: "failed", Error: uploadErr.Error()}
|
if lastUploadErr == nil {
|
||||||
logger.Warnf("存储目标 %s 上传失败:%v", targetName, uploadErr)
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if lastUploadErr != nil {
|
||||||
|
uploadResults[index] = StorageUploadResultItem{StorageTargetID: targetID, StorageTargetName: targetName, Status: "failed", Error: lastUploadErr.Error()}
|
||||||
|
logger.Warnf("存储目标 %s 上传失败(已重试 %d 次):%v", targetName, maxAttempts, lastUploadErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 完整性校验:对比实际传输字节数
|
// 完整性校验:对比实际传输字节数
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"backupx/server/internal/apperror"
|
"backupx/server/internal/apperror"
|
||||||
"backupx/server/internal/model"
|
"backupx/server/internal/model"
|
||||||
"backupx/server/internal/repository"
|
"backupx/server/internal/repository"
|
||||||
|
"backupx/server/internal/storage"
|
||||||
"backupx/server/internal/storage/codec"
|
"backupx/server/internal/storage/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -81,10 +82,12 @@ type BackupTaskScheduler interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type BackupTaskService struct {
|
type BackupTaskService struct {
|
||||||
tasks repository.BackupTaskRepository
|
tasks repository.BackupTaskRepository
|
||||||
targets repository.StorageTargetRepository
|
targets repository.StorageTargetRepository
|
||||||
cipher *codec.ConfigCipher
|
records repository.BackupRecordRepository
|
||||||
scheduler BackupTaskScheduler
|
storageRegistry *storage.Registry
|
||||||
|
cipher *codec.ConfigCipher
|
||||||
|
scheduler BackupTaskScheduler
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBackupTaskService(
|
func NewBackupTaskService(
|
||||||
@@ -95,6 +98,12 @@ func NewBackupTaskService(
|
|||||||
return &BackupTaskService{tasks: tasks, targets: targets, cipher: cipher}
|
return &BackupTaskService{tasks: tasks, targets: targets, cipher: cipher}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetRecordsAndStorage 注入备份记录仓库和存储注册表,用于任务删除时清理远端文件。
|
||||||
|
func (s *BackupTaskService) SetRecordsAndStorage(records repository.BackupRecordRepository, registry *storage.Registry) {
|
||||||
|
s.records = records
|
||||||
|
s.storageRegistry = registry
|
||||||
|
}
|
||||||
|
|
||||||
func (s *BackupTaskService) SetScheduler(scheduler BackupTaskScheduler) {
|
func (s *BackupTaskService) SetScheduler(scheduler BackupTaskScheduler) {
|
||||||
s.scheduler = scheduler
|
s.scheduler = scheduler
|
||||||
}
|
}
|
||||||
@@ -185,26 +194,80 @@ func (s *BackupTaskService) Update(ctx context.Context, id uint, input BackupTas
|
|||||||
return s.Get(ctx, item.ID)
|
return s.Get(ctx, item.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *BackupTaskService) Delete(ctx context.Context, id uint) error {
|
// DeleteResult 描述任务删除的结果信息,用于审计日志。
|
||||||
|
type DeleteResult struct {
|
||||||
|
TaskName string
|
||||||
|
RecordCount int
|
||||||
|
CleanedFiles int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *BackupTaskService) Delete(ctx context.Context, id uint) (*DeleteResult, error) {
|
||||||
existing, err := s.tasks.FindByID(ctx, id)
|
existing, err := s.tasks.FindByID(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return apperror.Internal("BACKUP_TASK_GET_FAILED", "无法获取备份任务详情", err)
|
return nil, apperror.Internal("BACKUP_TASK_GET_FAILED", "无法获取备份任务详情", err)
|
||||||
}
|
}
|
||||||
if existing == nil {
|
if existing == nil {
|
||||||
return apperror.New(http.StatusNotFound, "BACKUP_TASK_NOT_FOUND", "备份任务不存在", fmt.Errorf("backup task %d not found", id))
|
return nil, apperror.New(http.StatusNotFound, "BACKUP_TASK_NOT_FOUND", "备份任务不存在", fmt.Errorf("backup task %d not found", id))
|
||||||
}
|
|
||||||
if s.scheduler != nil {
|
|
||||||
if err := s.scheduler.RemoveTask(ctx, id); err != nil {
|
|
||||||
return apperror.Internal("BACKUP_TASK_SCHEDULE_FAILED", "无法移除备份任务调度", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := s.tasks.Delete(ctx, id); err != nil {
|
|
||||||
return apperror.Internal("BACKUP_TASK_DELETE_FAILED", "无法删除备份任务", err)
|
|
||||||
}
|
}
|
||||||
if s.scheduler != nil {
|
if s.scheduler != nil {
|
||||||
_ = s.scheduler.RemoveTask(ctx, id)
|
_ = s.scheduler.RemoveTask(ctx, id)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
// 清理远端存储文件(尽力而为,不阻止删除)
|
||||||
|
result := &DeleteResult{TaskName: existing.Name}
|
||||||
|
result.RecordCount, result.CleanedFiles = s.cleanupRemoteFiles(ctx, id)
|
||||||
|
|
||||||
|
if err := s.tasks.Delete(ctx, id); err != nil {
|
||||||
|
return nil, apperror.Internal("BACKUP_TASK_DELETE_FAILED", "无法删除备份任务", err)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanupRemoteFiles 尽力删除任务相关的远端备份文件,返回记录数和成功删除的文件数。
|
||||||
|
func (s *BackupTaskService) cleanupRemoteFiles(ctx context.Context, taskID uint) (recordCount int, cleanedFiles int) {
|
||||||
|
if s.records == nil || s.storageRegistry == nil {
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
records, err := s.records.ListByTask(ctx, taskID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
recordCount = len(records)
|
||||||
|
// 缓存 provider 避免同一存储目标重复创建连接
|
||||||
|
providerCache := make(map[uint]storage.StorageProvider)
|
||||||
|
for _, record := range records {
|
||||||
|
if strings.TrimSpace(record.StoragePath) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
provider, ok := providerCache[record.StorageTargetID]
|
||||||
|
if !ok {
|
||||||
|
provider, err = s.resolveStorageProvider(ctx, record.StorageTargetID)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
providerCache[record.StorageTargetID] = provider
|
||||||
|
}
|
||||||
|
if err := provider.Delete(ctx, record.StoragePath); err == nil {
|
||||||
|
cleanedFiles++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return recordCount, cleanedFiles
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *BackupTaskService) resolveStorageProvider(ctx context.Context, targetID uint) (storage.StorageProvider, error) {
|
||||||
|
target, err := s.targets.FindByID(ctx, targetID)
|
||||||
|
if err != nil || target == nil {
|
||||||
|
return nil, fmt.Errorf("target %d not found", targetID)
|
||||||
|
}
|
||||||
|
configMap := map[string]any{}
|
||||||
|
if err := s.cipher.DecryptJSON(target.ConfigCiphertext, &configMap); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
provider, err := s.storageRegistry.Create(ctx, target.Type, configMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return provider, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *BackupTaskService) Toggle(ctx context.Context, id uint, enabled bool) (*BackupTaskSummary, error) {
|
func (s *BackupTaskService) Toggle(ctx context.Context, id uint, enabled bool) (*BackupTaskSummary, error) {
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"backupx/server/internal/apperror"
|
"backupx/server/internal/apperror"
|
||||||
@@ -37,13 +39,19 @@ type NodeCreateInput struct {
|
|||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NodeService manages the cluster nodes.
|
// NodeUpdateInput 是编辑节点的输入。
|
||||||
type NodeService struct {
|
type NodeUpdateInput struct {
|
||||||
repo repository.NodeRepository
|
Name string `json:"name" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeService(repo repository.NodeRepository) *NodeService {
|
// NodeService manages the cluster nodes.
|
||||||
return &NodeService{repo: repo}
|
type NodeService struct {
|
||||||
|
repo repository.NodeRepository
|
||||||
|
version string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNodeService(repo repository.NodeRepository, version string) *NodeService {
|
||||||
|
return &NodeService{repo: repo, version: version}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnsureLocalNode creates the default "local" node if it does not exist.
|
// EnsureLocalNode creates the default "local" node if it does not exist.
|
||||||
@@ -57,6 +65,8 @@ func (s *NodeService) EnsureLocalNode(ctx context.Context) error {
|
|||||||
existing.LastSeen = time.Now().UTC()
|
existing.LastSeen = time.Now().UTC()
|
||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
existing.Hostname = hostname
|
existing.Hostname = hostname
|
||||||
|
existing.IPAddress = detectLocalIP()
|
||||||
|
existing.AgentVer = s.version
|
||||||
existing.OS = runtime.GOOS
|
existing.OS = runtime.GOOS
|
||||||
existing.Arch = runtime.GOARCH
|
existing.Arch = runtime.GOARCH
|
||||||
return s.repo.Update(ctx, existing)
|
return s.repo.Update(ctx, existing)
|
||||||
@@ -64,14 +74,16 @@ func (s *NodeService) EnsureLocalNode(ctx context.Context) error {
|
|||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
token, _ := generateToken()
|
token, _ := generateToken()
|
||||||
node := &model.Node{
|
node := &model.Node{
|
||||||
Name: "本机 (Local)",
|
Name: "本机 (Local)",
|
||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
Token: token,
|
IPAddress: detectLocalIP(),
|
||||||
Status: model.NodeStatusOnline,
|
Token: token,
|
||||||
IsLocal: true,
|
Status: model.NodeStatusOnline,
|
||||||
OS: runtime.GOOS,
|
IsLocal: true,
|
||||||
Arch: runtime.GOARCH,
|
OS: runtime.GOOS,
|
||||||
LastSeen: time.Now().UTC(),
|
Arch: runtime.GOARCH,
|
||||||
|
AgentVer: s.version,
|
||||||
|
LastSeen: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
return s.repo.Create(ctx, node)
|
return s.repo.Create(ctx, node)
|
||||||
}
|
}
|
||||||
@@ -199,7 +211,7 @@ func (s *NodeService) ListDirectory(ctx context.Context, nodeID uint, path strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Heartbeat updates the node status when an agent reports in.
|
// Heartbeat updates the node status when an agent reports in.
|
||||||
func (s *NodeService) Heartbeat(ctx context.Context, token string, hostname string, ip string, agentVer string) error {
|
func (s *NodeService) Heartbeat(ctx context.Context, token string, hostname string, ip string, agentVer string, osName string, archName string) error {
|
||||||
node, err := s.repo.FindByToken(ctx, token)
|
node, err := s.repo.FindByToken(ctx, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -211,12 +223,36 @@ func (s *NodeService) Heartbeat(ctx context.Context, token string, hostname stri
|
|||||||
node.Hostname = hostname
|
node.Hostname = hostname
|
||||||
node.IPAddress = ip
|
node.IPAddress = ip
|
||||||
node.AgentVer = agentVer
|
node.AgentVer = agentVer
|
||||||
node.OS = runtime.GOOS
|
if strings.TrimSpace(osName) != "" {
|
||||||
node.Arch = runtime.GOARCH
|
node.OS = osName
|
||||||
|
} else {
|
||||||
|
node.OS = runtime.GOOS
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(archName) != "" {
|
||||||
|
node.Arch = archName
|
||||||
|
} else {
|
||||||
|
node.Arch = runtime.GOARCH
|
||||||
|
}
|
||||||
node.LastSeen = time.Now().UTC()
|
node.LastSeen = time.Now().UTC()
|
||||||
return s.repo.Update(ctx, node)
|
return s.repo.Update(ctx, node)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update 编辑节点名称。
|
||||||
|
func (s *NodeService) Update(ctx context.Context, id uint, input NodeUpdateInput) (*NodeSummary, error) {
|
||||||
|
node, err := s.repo.FindByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if node == nil {
|
||||||
|
return nil, apperror.New(http.StatusNotFound, "NODE_NOT_FOUND", "节点不存在", nil)
|
||||||
|
}
|
||||||
|
node.Name = strings.TrimSpace(input.Name)
|
||||||
|
if err := s.repo.Update(ctx, node); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return s.Get(ctx, id)
|
||||||
|
}
|
||||||
|
|
||||||
// DirEntry represents a file or directory in a node's file system.
|
// DirEntry represents a file or directory in a node's file system.
|
||||||
type DirEntry struct {
|
type DirEntry struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -225,6 +261,22 @@ type DirEntry struct {
|
|||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// detectLocalIP 获取本机第一个非回环 IPv4 地址。
|
||||||
|
func detectLocalIP() string {
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
|
||||||
|
if ipNet.IP.To4() != nil {
|
||||||
|
return ipNet.IP.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func generateToken() (string, error) {
|
func generateToken() (string, error) {
|
||||||
b := make([]byte, 32)
|
b := make([]byte, 32)
|
||||||
if _, err := rand.Read(b); err != nil {
|
if _, err := rand.Read(b); err != nil {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import (
|
|||||||
|
|
||||||
type StorageTargetUpsertInput struct {
|
type StorageTargetUpsertInput struct {
|
||||||
Name string `json:"name" binding:"required,min=1,max=128"`
|
Name string `json:"name" binding:"required,min=1,max=128"`
|
||||||
Type string `json:"type" binding:"required,oneof=local_disk google_drive s3 webdav"`
|
Type string `json:"type" binding:"required,min=1"`
|
||||||
Description string `json:"description" binding:"max=255"`
|
Description string `json:"description" binding:"max=255"`
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
Config map[string]any `json:"config" binding:"required"`
|
Config map[string]any `json:"config" binding:"required"`
|
||||||
|
|||||||
@@ -2,7 +2,12 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -30,6 +35,82 @@ func NewSystemService(cfg config.Config, version string, startedAt time.Time) *S
|
|||||||
return &SystemService{cfg: cfg, version: version, startedAt: startedAt}
|
return &SystemService{cfg: cfg, version: version, startedAt: startedAt}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateCheckResult 描述版本更新检查结果。
|
||||||
|
type UpdateCheckResult struct {
|
||||||
|
CurrentVersion string `json:"currentVersion"`
|
||||||
|
LatestVersion string `json:"latestVersion"`
|
||||||
|
HasUpdate bool `json:"hasUpdate"`
|
||||||
|
ReleaseURL string `json:"releaseUrl,omitempty"`
|
||||||
|
ReleaseNotes string `json:"releaseNotes,omitempty"`
|
||||||
|
PublishedAt string `json:"publishedAt,omitempty"`
|
||||||
|
DownloadURL string `json:"downloadUrl,omitempty"`
|
||||||
|
DockerImage string `json:"dockerImage,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const githubRepoAPI = "https://api.github.com/repos/Awuqing/BackupX/releases/latest"
|
||||||
|
|
||||||
|
// CheckUpdate 从 GitHub Releases 检查是否有新版本。
|
||||||
|
func (s *SystemService) CheckUpdate(ctx context.Context) (*UpdateCheckResult, error) {
|
||||||
|
result := &UpdateCheckResult{
|
||||||
|
CurrentVersion: s.version,
|
||||||
|
DockerImage: "awuqing/backupx",
|
||||||
|
}
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, githubRepoAPI, nil)
|
||||||
|
if err != nil {
|
||||||
|
return result, fmt.Errorf("create request: %w", err)
|
||||||
|
}
|
||||||
|
req.Header.Set("Accept", "application/vnd.github.v3+json")
|
||||||
|
req.Header.Set("User-Agent", "BackupX/"+s.version)
|
||||||
|
|
||||||
|
client := &http.Client{Timeout: 15 * time.Second}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return result, fmt.Errorf("fetch latest release: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return result, fmt.Errorf("github api returned %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
var release struct {
|
||||||
|
TagName string `json:"tag_name"`
|
||||||
|
HTMLURL string `json:"html_url"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
Published string `json:"published_at"`
|
||||||
|
Assets []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
BrowserDownloadURL string `json:"browser_download_url"`
|
||||||
|
} `json:"assets"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
|
||||||
|
return result, fmt.Errorf("decode release: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result.LatestVersion = release.TagName
|
||||||
|
result.ReleaseURL = release.HTMLURL
|
||||||
|
result.ReleaseNotes = release.Body
|
||||||
|
result.PublishedAt = release.Published
|
||||||
|
|
||||||
|
// 比较版本号(去 v 前缀后字符串比较)
|
||||||
|
current := strings.TrimPrefix(s.version, "v")
|
||||||
|
latest := strings.TrimPrefix(release.TagName, "v")
|
||||||
|
result.HasUpdate = latest > current && current != "dev"
|
||||||
|
|
||||||
|
// 匹配当前平台的下载链接
|
||||||
|
goos := runtime.GOOS
|
||||||
|
goarch := runtime.GOARCH
|
||||||
|
suffix := fmt.Sprintf("%s-%s.tar.gz", goos, goarch)
|
||||||
|
for _, asset := range release.Assets {
|
||||||
|
if strings.HasSuffix(asset.Name, suffix) {
|
||||||
|
result.DownloadURL = asset.BrowserDownloadURL
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SystemService) GetInfo(_ context.Context) *SystemInfo {
|
func (s *SystemService) GetInfo(_ context.Context) *SystemInfo {
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
info := &SystemInfo{
|
info := &SystemInfo{
|
||||||
@@ -51,3 +132,4 @@ func (s *SystemService) GetInfo(_ context.Context) *SystemInfo {
|
|||||||
}
|
}
|
||||||
return info
|
return info
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -434,3 +434,75 @@ type BackendOption struct {
|
|||||||
Required bool `json:"required"`
|
Required bool `json:"required"`
|
||||||
IsPassword bool `json:"isPassword"`
|
IsPassword bool `json:"isPassword"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// 通用 BackendFactory — 为任意 rclone 后端自动生成独立 Factory
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// GenericBackendFactory 为单个 rclone 后端创建独立的 ProviderFactory。
|
||||||
|
// 用户存储目标的 type 直接是后端名(如 "sftp"),与 "s3"、"ftp" 完全平级。
|
||||||
|
type GenericBackendFactory struct {
|
||||||
|
backendType string
|
||||||
|
sensitive []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBackendFactory 为指定 rclone 后端创建一个 Factory。
|
||||||
|
func NewBackendFactory(backendType string) GenericBackendFactory {
|
||||||
|
var sensitive []string
|
||||||
|
for _, ri := range fs.Registry {
|
||||||
|
if ri.Name == backendType {
|
||||||
|
for _, opt := range ri.Options {
|
||||||
|
if opt.IsPassword {
|
||||||
|
sensitive = append(sensitive, opt.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return GenericBackendFactory{backendType: backendType, sensitive: sensitive}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f GenericBackendFactory) Type() storage.ProviderType { return storage.ProviderType(f.backendType) }
|
||||||
|
func (f GenericBackendFactory) SensitiveFields() []string { return f.sensitive }
|
||||||
|
|
||||||
|
func (f GenericBackendFactory) New(ctx context.Context, rawConfig map[string]any) (storage.StorageProvider, error) {
|
||||||
|
root, _ := rawConfig["root"].(string)
|
||||||
|
root = strings.TrimSpace(root)
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString(":")
|
||||||
|
b.WriteString(f.backendType)
|
||||||
|
for key, val := range rawConfig {
|
||||||
|
if key == "root" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
strVal := fmt.Sprintf("%v", val)
|
||||||
|
if strings.TrimSpace(strVal) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
b.WriteString(",")
|
||||||
|
b.WriteString(key)
|
||||||
|
b.WriteString("=")
|
||||||
|
b.WriteString(quoteParam(strVal))
|
||||||
|
}
|
||||||
|
b.WriteString(":")
|
||||||
|
b.WriteString(root)
|
||||||
|
|
||||||
|
return newFs(ctx, storage.ProviderType(f.backendType), b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterAllBackends 将所有 rclone 后端注册为独立 Factory 到 Registry。
|
||||||
|
// 已存在的内置类型(s3, ftp 等)不会被覆盖。
|
||||||
|
func RegisterAllBackends(registry *storage.Registry) {
|
||||||
|
builtinTypes := map[string]bool{
|
||||||
|
"local_disk": true, "s3": true, "webdav": true, "google_drive": true,
|
||||||
|
"ftp": true, "aliyun_oss": true, "tencent_cos": true, "qiniu_kodo": true,
|
||||||
|
"rclone": true, "local": true,
|
||||||
|
}
|
||||||
|
for _, info := range ListBackends() {
|
||||||
|
if builtinTypes[info.Name] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
registry.Register(NewBackendFactory(info.Name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -26,8 +27,12 @@ func newProvider(providerType storage.ProviderType, rfs fs.Fs) *Provider {
|
|||||||
|
|
||||||
func (p *Provider) Type() storage.ProviderType { return p.providerType }
|
func (p *Provider) Type() storage.ProviderType { return p.providerType }
|
||||||
|
|
||||||
// TestConnection 通过列出根目录验证连通性。
|
// TestConnection 验证连通性。对本地磁盘会先确保目录存在。
|
||||||
func (p *Provider) TestConnection(ctx context.Context) error {
|
func (p *Provider) TestConnection(ctx context.Context) error {
|
||||||
|
// 确保根目录存在(本地磁盘等后端需要预创建)
|
||||||
|
if err := p.rfs.Mkdir(ctx, ""); err != nil {
|
||||||
|
return fmt.Errorf("rclone test connection (mkdir): %w", err)
|
||||||
|
}
|
||||||
_, err := p.rfs.List(ctx, "")
|
_, err := p.rfs.List(ctx, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("rclone test connection: %w", err)
|
return fmt.Errorf("rclone test connection: %w", err)
|
||||||
@@ -120,6 +125,36 @@ func (p *Provider) About(ctx context.Context) (*storage.StorageUsageInfo, error)
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveEmptyDirs 递归删除 prefix 下的空目录,从最深层开始。
|
||||||
|
// 非空目录删除会失败(安全忽略),仅清理真正的空目录。
|
||||||
|
func (p *Provider) RemoveEmptyDirs(ctx context.Context, prefix string) error {
|
||||||
|
var dirs []string
|
||||||
|
err := walk.ListR(ctx, p.rfs, prefix, true, -1, walk.ListDirs, func(entries fs.DirEntries) error {
|
||||||
|
for _, entry := range entries {
|
||||||
|
if _, ok := entry.(fs.Directory); ok {
|
||||||
|
dirs = append(dirs, entry.Remote())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
// 列目录失败(比如目录不存在)静默返回
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// 按路径长度倒序(深目录优先删除),同长度保持稳定顺序
|
||||||
|
sort.SliceStable(dirs, func(i, j int) bool {
|
||||||
|
return len(dirs[i]) > len(dirs[j])
|
||||||
|
})
|
||||||
|
for _, dir := range dirs {
|
||||||
|
_ = p.rfs.Rmdir(ctx, dir)
|
||||||
|
}
|
||||||
|
// 尝试清理 prefix 本身
|
||||||
|
if prefix != "" {
|
||||||
|
_ = p.rfs.Rmdir(ctx, prefix)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// pathDir 返回 objectKey 的目录部分(正斜杠分隔)。
|
// pathDir 返回 objectKey 的目录部分(正斜杠分隔)。
|
||||||
func pathDir(objectKey string) string {
|
func pathDir(objectKey string) string {
|
||||||
idx := strings.LastIndex(objectKey, "/")
|
idx := strings.LastIndex(objectKey, "/")
|
||||||
|
|||||||
@@ -145,3 +145,10 @@ type FTPConfig struct {
|
|||||||
UseTLS bool `json:"useTLS"`
|
UseTLS bool `json:"useTLS"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StorageDirCleaner 是可选能力接口,支持清理空目录。
|
||||||
|
// 主要用于本地磁盘等文件系统类存储,对象存储通常不需要。
|
||||||
|
// 通过 type assertion 检测 provider 是否实现该接口。
|
||||||
|
type StorageDirCleaner interface {
|
||||||
|
RemoveEmptyDirs(ctx context.Context, prefix string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,196 +1,327 @@
|
|||||||
import { Input, Space, Switch, Tabs, Typography, Radio, Checkbox, Select } from '@arco-design/web-react'
|
import { Button, Divider, Input, Select, Space, Switch, Typography } from '@arco-design/web-react'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useMemo, useState } from 'react'
|
||||||
|
|
||||||
export interface CronInputProps {
|
export interface CronInputProps {
|
||||||
value?: string
|
value?: string
|
||||||
onChange?: (value: string) => void
|
onChange?: (value: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_CRON = '* * * * *'
|
const DEFAULT_CRON = '0 2 * * *'
|
||||||
|
|
||||||
type CronPart = 'minute' | 'hour' | 'day' | 'month' | 'week'
|
// 常用预设
|
||||||
|
const PRESETS = [
|
||||||
interface CronState {
|
{ label: '每天 02:00', value: '0 2 * * *' },
|
||||||
minute: string
|
{ label: '每天 00:00', value: '0 0 * * *' },
|
||||||
hour: string
|
{ label: '每 6 小时', value: '0 */6 * * *' },
|
||||||
day: string
|
{ label: '每 12 小时', value: '0 */12 * * *' },
|
||||||
month: string
|
{ label: '每周日 03:00', value: '0 3 * * 0' },
|
||||||
week: string
|
{ label: '每月 1 日 02:00', value: '0 2 1 * *' },
|
||||||
}
|
{ label: '每 30 分钟', value: '*/30 * * * *' },
|
||||||
|
{ label: '每小时整点', value: '0 * * * *' },
|
||||||
function parseCron(expr: string): CronState {
|
|
||||||
const parts = (expr || DEFAULT_CRON).trim().split(/\s+/)
|
|
||||||
return {
|
|
||||||
minute: parts[0] || '*',
|
|
||||||
hour: parts[1] || '*',
|
|
||||||
day: parts[2] || '*',
|
|
||||||
month: parts[3] || '*',
|
|
||||||
week: parts[4] || '*',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function stringifyCron(state: CronState): string {
|
|
||||||
return `${state.minute} ${state.hour} ${state.day} ${state.month} ${state.week}`
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateOptions(min: number, max: number) {
|
|
||||||
return Array.from({ length: max - min + 1 }, (_, i) => ({
|
|
||||||
label: String(i + min),
|
|
||||||
value: String(i + min),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
const MINUTES_OPTIONS = generateOptions(0, 59)
|
|
||||||
const HOURS_OPTIONS = generateOptions(0, 23)
|
|
||||||
const DAYS_OPTIONS = generateOptions(1, 31)
|
|
||||||
const MONTHS_OPTIONS = generateOptions(1, 12)
|
|
||||||
const WEEKS_OPTIONS = [
|
|
||||||
{ label: '星期日', value: '0' },
|
|
||||||
{ label: '星期一', value: '1' },
|
|
||||||
{ label: '星期二', value: '2' },
|
|
||||||
{ label: '星期三', value: '3' },
|
|
||||||
{ label: '星期四', value: '4' },
|
|
||||||
{ label: '星期五', value: '5' },
|
|
||||||
{ label: '星期六', value: '6' },
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
const HOUR_OPTIONS = Array.from({ length: 24 }, (_, i) => ({
|
||||||
|
label: `${String(i).padStart(2, '0')} 时`,
|
||||||
|
value: String(i),
|
||||||
|
}))
|
||||||
|
|
||||||
|
const MINUTE_OPTIONS = Array.from({ length: 12 }, (_, i) => ({
|
||||||
|
label: `${String(i * 5).padStart(2, '0')} 分`,
|
||||||
|
value: String(i * 5),
|
||||||
|
}))
|
||||||
|
|
||||||
|
const WEEKDAY_OPTIONS = [
|
||||||
|
{ label: '周一', value: '1' },
|
||||||
|
{ label: '周二', value: '2' },
|
||||||
|
{ label: '周三', value: '3' },
|
||||||
|
{ label: '周四', value: '4' },
|
||||||
|
{ label: '周五', value: '5' },
|
||||||
|
{ label: '周六', value: '6' },
|
||||||
|
{ label: '周日', value: '0' },
|
||||||
|
]
|
||||||
|
|
||||||
|
const DAY_OPTIONS = Array.from({ length: 31 }, (_, i) => ({
|
||||||
|
label: `${i + 1} 日`,
|
||||||
|
value: String(i + 1),
|
||||||
|
}))
|
||||||
|
|
||||||
|
type ScheduleMode = 'daily' | 'weekly' | 'monthly' | 'interval'
|
||||||
|
|
||||||
|
// 将 cron 表达式转为自然语言中文描述
|
||||||
|
function describeCron(expr: string): string {
|
||||||
|
const parts = expr.trim().split(/\s+/)
|
||||||
|
if (parts.length !== 5) return ''
|
||||||
|
const [minute, hour, day, _month, week] = parts
|
||||||
|
|
||||||
|
// 每 N 分钟
|
||||||
|
if (minute.includes('/') && hour === '*' && day === '*' && week === '*') {
|
||||||
|
return `每 ${minute.split('/')[1]} 分钟执行一次`
|
||||||
|
}
|
||||||
|
// 每 N 小时
|
||||||
|
if (minute !== '*' && hour.includes('/') && day === '*' && week === '*') {
|
||||||
|
return `每 ${hour.split('/')[1]} 小时执行一次(在第 ${minute} 分)`
|
||||||
|
}
|
||||||
|
// 每小时
|
||||||
|
if (minute !== '*' && hour === '*' && day === '*' && week === '*') {
|
||||||
|
return `每小时的第 ${minute} 分执行`
|
||||||
|
}
|
||||||
|
|
||||||
|
const hh = hour.padStart(2, '0')
|
||||||
|
const mm = minute.padStart(2, '0')
|
||||||
|
const time = `${hh}:${mm}`
|
||||||
|
|
||||||
|
// 每周某天
|
||||||
|
if (day === '*' && week !== '*') {
|
||||||
|
const weekNames: Record<string, string> = { '0': '日', '1': '一', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六', '7': '日' }
|
||||||
|
const days = week.split(',').map((w) => `周${weekNames[w] || w}`).join('、')
|
||||||
|
return `每${days} ${time} 执行`
|
||||||
|
}
|
||||||
|
// 每月某日
|
||||||
|
if (day !== '*' && week === '*') {
|
||||||
|
return `每月 ${day} 日 ${time} 执行`
|
||||||
|
}
|
||||||
|
// 每天
|
||||||
|
if (day === '*' && week === '*' && hour !== '*' && !hour.includes('/')) {
|
||||||
|
return `每天 ${time} 执行`
|
||||||
|
}
|
||||||
|
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
export function CronInput({ value, onChange }: CronInputProps) {
|
export function CronInput({ value, onChange }: CronInputProps) {
|
||||||
const [internalValue, setInternalValue] = useState(value || DEFAULT_CRON)
|
const [cronExpr, setCronExpr] = useState(value || DEFAULT_CRON)
|
||||||
const [isAdvanced, setIsAdvanced] = useState(false)
|
const [isAdvanced, setIsAdvanced] = useState(false)
|
||||||
const [state, setState] = useState<CronState>(parseCron(internalValue))
|
const [showCustom, setShowCustom] = useState(false)
|
||||||
|
|
||||||
// Sync prop to internal state
|
// 自定义模式的状态
|
||||||
|
const [mode, setMode] = useState<ScheduleMode>('daily')
|
||||||
|
const [customHour, setCustomHour] = useState('2')
|
||||||
|
const [customMinute, setCustomMinute] = useState('0')
|
||||||
|
const [customWeekdays, setCustomWeekdays] = useState<string[]>(['0'])
|
||||||
|
const [customDay, setCustomDay] = useState('1')
|
||||||
|
const [customInterval, setCustomInterval] = useState('6')
|
||||||
|
|
||||||
|
// 从 prop 同步
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (value !== undefined && value !== internalValue) {
|
if (value !== undefined && value !== cronExpr) {
|
||||||
setInternalValue(value || DEFAULT_CRON)
|
setCronExpr(value || DEFAULT_CRON)
|
||||||
if (!isAdvanced) {
|
|
||||||
setState(parseCron(value || DEFAULT_CRON))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [value, isAdvanced, internalValue])
|
}, [value])
|
||||||
|
|
||||||
const notifyChange = (nextValue: string) => {
|
const description = useMemo(() => describeCron(cronExpr), [cronExpr])
|
||||||
setInternalValue(nextValue)
|
const isPreset = PRESETS.some((p) => p.value === cronExpr)
|
||||||
if (onChange) {
|
|
||||||
onChange(nextValue)
|
const emit = (expr: string) => {
|
||||||
}
|
setCronExpr(expr)
|
||||||
|
onChange?.(expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleStateChange = (part: CronPart, val: string) => {
|
// 从自定义选择器构建 cron
|
||||||
const nextState = { ...state, [part]: val }
|
const buildCustomCron = (
|
||||||
setState(nextState)
|
m: ScheduleMode,
|
||||||
notifyChange(stringifyCron(nextState))
|
h: string,
|
||||||
}
|
min: string,
|
||||||
|
weekdays: string[],
|
||||||
const renderPartTab = (
|
day: string,
|
||||||
part: CronPart,
|
interval: string,
|
||||||
title: string,
|
|
||||||
options: { label: string; value: string }[],
|
|
||||||
allowAnyVal = '*',
|
|
||||||
) => {
|
) => {
|
||||||
const currentVal = state[part]
|
switch (m) {
|
||||||
const isAny = currentVal === allowAnyVal || currentVal === '*' || currentVal === '?'
|
case 'daily':
|
||||||
const isSpecific = !isAny && !currentVal.includes('/') && !currentVal.includes('-')
|
return `${min} ${h} * * *`
|
||||||
|
case 'weekly':
|
||||||
// For simplicity in this visual editor, we only support "every" (*) and "specific values" (1,2,3).
|
return `${min} ${h} * * ${weekdays.sort().join(',') || '0'}`
|
||||||
const type = isAny ? 'any' : 'specific'
|
case 'monthly':
|
||||||
const specificValues = isSpecific ? currentVal.split(',') : []
|
return `${min} ${h} ${day} * *`
|
||||||
|
case 'interval':
|
||||||
|
return `0 */${interval} * * *`
|
||||||
|
default:
|
||||||
|
return DEFAULT_CRON
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
const handleCustomChange = (updates: {
|
||||||
<div style={{ padding: '16px 0' }}>
|
mode?: ScheduleMode
|
||||||
<Radio.Group
|
hour?: string
|
||||||
direction="vertical"
|
minute?: string
|
||||||
value={type}
|
weekdays?: string[]
|
||||||
onChange={(val) => {
|
day?: string
|
||||||
if (val === 'any') {
|
interval?: string
|
||||||
handleStateChange(part, allowAnyVal)
|
}) => {
|
||||||
} else {
|
const m = updates.mode ?? mode
|
||||||
handleStateChange(part, options[0].value) // Default to first valid item
|
const h = updates.hour ?? customHour
|
||||||
}
|
const min = updates.minute ?? customMinute
|
||||||
}}
|
const w = updates.weekdays ?? customWeekdays
|
||||||
>
|
const d = updates.day ?? customDay
|
||||||
<Radio value="any">
|
const iv = updates.interval ?? customInterval
|
||||||
<Typography.Text>通配 ({allowAnyVal}) - 任意{title}</Typography.Text>
|
|
||||||
</Radio>
|
|
||||||
<Radio value="specific">
|
|
||||||
<Typography.Text>指定{title}</Typography.Text>
|
|
||||||
</Radio>
|
|
||||||
</Radio.Group>
|
|
||||||
|
|
||||||
{type === 'specific' && (
|
if (updates.mode !== undefined) setMode(m)
|
||||||
<div style={{ paddingLeft: 24, marginTop: 12 }}>
|
if (updates.hour !== undefined) setCustomHour(h)
|
||||||
<Select
|
if (updates.minute !== undefined) setCustomMinute(min)
|
||||||
mode="multiple"
|
if (updates.weekdays !== undefined) setCustomWeekdays(w)
|
||||||
placeholder={`请选择${title}`}
|
if (updates.day !== undefined) setCustomDay(d)
|
||||||
value={specificValues}
|
if (updates.interval !== undefined) setCustomInterval(iv)
|
||||||
options={options}
|
|
||||||
onChange={(vals: string[]) => {
|
emit(buildCustomCron(m, h, min, w, d, iv))
|
||||||
if (vals.length === 0) {
|
|
||||||
handleStateChange(part, allowAnyVal)
|
|
||||||
} else {
|
|
||||||
// Sort numerically to keep things neat
|
|
||||||
const sorted = [...vals].sort((a, b) => Number(a) - Number(b))
|
|
||||||
handleStateChange(part, sorted.join(','))
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
style={{ width: '100%', maxWidth: 400 }}
|
|
||||||
allowClear
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="cron-input-container">
|
<div>
|
||||||
<div style={{ marginBottom: 12, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
{/* 预设按钮 */}
|
||||||
<Input
|
<Space wrap size="small" style={{ marginBottom: 12 }}>
|
||||||
value={internalValue}
|
{PRESETS.map((preset) => (
|
||||||
onChange={(val) => {
|
<Button
|
||||||
setInternalValue(val)
|
key={preset.value}
|
||||||
if (isAdvanced && onChange) {
|
size="small"
|
||||||
onChange(val)
|
type={cronExpr === preset.value ? 'primary' : 'secondary'}
|
||||||
}
|
onClick={() => {
|
||||||
}}
|
emit(preset.value)
|
||||||
readOnly={!isAdvanced}
|
setShowCustom(false)
|
||||||
style={{ width: 240, fontFamily: 'monospace' }}
|
setIsAdvanced(false)
|
||||||
placeholder="* * * * *"
|
|
||||||
/>
|
|
||||||
<Space>
|
|
||||||
<Typography.Text type="secondary">高级模式 (手动输入)</Typography.Text>
|
|
||||||
<Switch
|
|
||||||
checked={isAdvanced}
|
|
||||||
onChange={(checked) => {
|
|
||||||
setIsAdvanced(checked)
|
|
||||||
if (!checked) {
|
|
||||||
// When switching back to visual, parse the current raw value
|
|
||||||
setState(parseCron(internalValue))
|
|
||||||
notifyChange(stringifyCron(parseCron(internalValue)))
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
/>
|
>
|
||||||
</Space>
|
{preset.label}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
type={!isPreset && !isAdvanced ? 'primary' : 'secondary'}
|
||||||
|
onClick={() => {
|
||||||
|
setShowCustom(true)
|
||||||
|
setIsAdvanced(false)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
自定义...
|
||||||
|
</Button>
|
||||||
|
</Space>
|
||||||
|
|
||||||
|
{/* 中文描述 + cron 表达式 */}
|
||||||
|
<div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 8 }}>
|
||||||
|
<Input
|
||||||
|
value={cronExpr}
|
||||||
|
readOnly={!isAdvanced}
|
||||||
|
style={{ width: 180, fontFamily: 'monospace', fontSize: 13 }}
|
||||||
|
placeholder="0 2 * * *"
|
||||||
|
onChange={(val) => {
|
||||||
|
if (isAdvanced) emit(val)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
{description && (
|
||||||
|
<Typography.Text type="secondary">{description}</Typography.Text>
|
||||||
|
)}
|
||||||
|
<div style={{ marginLeft: 'auto' }}>
|
||||||
|
<Space size="mini">
|
||||||
|
<Typography.Text type="secondary" style={{ fontSize: 12 }}>手动输入</Typography.Text>
|
||||||
|
<Switch
|
||||||
|
size="small"
|
||||||
|
checked={isAdvanced}
|
||||||
|
onChange={(checked) => {
|
||||||
|
setIsAdvanced(checked)
|
||||||
|
setShowCustom(false)
|
||||||
|
if (!checked) {
|
||||||
|
setCronExpr(cronExpr)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Space>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{!isAdvanced && (
|
{/* 自定义选择器 */}
|
||||||
<Tabs type="card-gutter" size="small">
|
{showCustom && !isAdvanced && (
|
||||||
<Tabs.TabPane key="minute" title="分钟">
|
<div style={{ padding: '12px 16px', background: 'var(--color-fill-1)', borderRadius: 6 }}>
|
||||||
{renderPartTab('minute', '分钟', MINUTES_OPTIONS, '*')}
|
<Space size="large" style={{ marginBottom: 12 }}>
|
||||||
</Tabs.TabPane>
|
<Button size="small" type={mode === 'daily' ? 'primary' : 'text'} onClick={() => handleCustomChange({ mode: 'daily' })}>
|
||||||
<Tabs.TabPane key="hour" title="小时">
|
每天
|
||||||
{renderPartTab('hour', '小时', HOURS_OPTIONS, '*')}
|
</Button>
|
||||||
</Tabs.TabPane>
|
<Button size="small" type={mode === 'weekly' ? 'primary' : 'text'} onClick={() => handleCustomChange({ mode: 'weekly' })}>
|
||||||
<Tabs.TabPane key="day" title="日">
|
每周
|
||||||
{renderPartTab('day', '日', DAYS_OPTIONS, '*')}
|
</Button>
|
||||||
</Tabs.TabPane>
|
<Button size="small" type={mode === 'monthly' ? 'primary' : 'text'} onClick={() => handleCustomChange({ mode: 'monthly' })}>
|
||||||
<Tabs.TabPane key="month" title="月">
|
每月
|
||||||
{renderPartTab('month', '月', MONTHS_OPTIONS, '*')}
|
</Button>
|
||||||
</Tabs.TabPane>
|
<Button size="small" type={mode === 'interval' ? 'primary' : 'text'} onClick={() => handleCustomChange({ mode: 'interval' })}>
|
||||||
<Tabs.TabPane key="week" title="周">
|
间隔
|
||||||
{renderPartTab('week', '周', WEEKS_OPTIONS, '*')}
|
</Button>
|
||||||
</Tabs.TabPane>
|
</Space>
|
||||||
</Tabs>
|
|
||||||
|
{mode === 'interval' ? (
|
||||||
|
<Space align="center">
|
||||||
|
<Typography.Text>每</Typography.Text>
|
||||||
|
<Select
|
||||||
|
size="small"
|
||||||
|
value={customInterval}
|
||||||
|
style={{ width: 80 }}
|
||||||
|
options={[
|
||||||
|
{ label: '1', value: '1' },
|
||||||
|
{ label: '2', value: '2' },
|
||||||
|
{ label: '3', value: '3' },
|
||||||
|
{ label: '4', value: '4' },
|
||||||
|
{ label: '6', value: '6' },
|
||||||
|
{ label: '8', value: '8' },
|
||||||
|
{ label: '12', value: '12' },
|
||||||
|
]}
|
||||||
|
onChange={(val) => handleCustomChange({ interval: val })}
|
||||||
|
/>
|
||||||
|
<Typography.Text>小时执行一次</Typography.Text>
|
||||||
|
</Space>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
{mode === 'weekly' && (
|
||||||
|
<div style={{ marginBottom: 8 }}>
|
||||||
|
<Space wrap size="mini">
|
||||||
|
{WEEKDAY_OPTIONS.map((opt) => (
|
||||||
|
<Button
|
||||||
|
key={opt.value}
|
||||||
|
size="mini"
|
||||||
|
type={customWeekdays.includes(opt.value) ? 'primary' : 'secondary'}
|
||||||
|
onClick={() => {
|
||||||
|
const next = customWeekdays.includes(opt.value)
|
||||||
|
? customWeekdays.filter((v) => v !== opt.value)
|
||||||
|
: [...customWeekdays, opt.value]
|
||||||
|
handleCustomChange({ weekdays: next.length > 0 ? next : [opt.value] })
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{opt.label}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
</Space>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{mode === 'monthly' && (
|
||||||
|
<div style={{ marginBottom: 8 }}>
|
||||||
|
<Space align="center">
|
||||||
|
<Typography.Text>每月</Typography.Text>
|
||||||
|
<Select
|
||||||
|
size="small"
|
||||||
|
value={customDay}
|
||||||
|
style={{ width: 90 }}
|
||||||
|
options={DAY_OPTIONS}
|
||||||
|
onChange={(val) => handleCustomChange({ day: val })}
|
||||||
|
/>
|
||||||
|
</Space>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<Space align="center">
|
||||||
|
<Typography.Text>执行时间</Typography.Text>
|
||||||
|
<Select
|
||||||
|
size="small"
|
||||||
|
value={customHour}
|
||||||
|
style={{ width: 90 }}
|
||||||
|
options={HOUR_OPTIONS}
|
||||||
|
onChange={(val) => handleCustomChange({ hour: val })}
|
||||||
|
/>
|
||||||
|
<Typography.Text>:</Typography.Text>
|
||||||
|
<Select
|
||||||
|
size="small"
|
||||||
|
value={customMinute}
|
||||||
|
style={{ width: 90 }}
|
||||||
|
options={MINUTE_OPTIONS}
|
||||||
|
onChange={(val) => handleCustomChange({ minute: val })}
|
||||||
|
/>
|
||||||
|
</Space>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Button, Input, Message, Modal, Space, Spin, Tree, Typography } from '@arco-design/web-react'
|
import { Button, Input, Message, Modal, Space, Spin, Tree, Typography, Empty } from '@arco-design/web-react'
|
||||||
import { IconFolder, IconFile } from '@arco-design/web-react/icon'
|
import { IconFolder, IconFile, IconFolderAdd } from '@arco-design/web-react/icon'
|
||||||
import { useCallback, useState } from 'react'
|
import { useCallback, useState } from 'react'
|
||||||
import { listNodeDirectory } from '../../services/nodes'
|
import { listNodeDirectory } from '../../services/nodes'
|
||||||
import type { DirEntry } from '../../types/nodes'
|
import type { DirEntry } from '../../types/nodes'
|
||||||
@@ -27,7 +27,7 @@ function entriesToTreeNodes(entries: DirEntry[], mode: 'directory' | 'file'): Tr
|
|||||||
.map((entry) => ({
|
.map((entry) => ({
|
||||||
key: entry.path,
|
key: entry.path,
|
||||||
title: entry.name,
|
title: entry.name,
|
||||||
icon: entry.isDir ? <IconFolder /> : <IconFile />,
|
icon: entry.isDir ? <IconFolder style={{ color: 'var(--color-warning-6)' }} /> : <IconFile />,
|
||||||
isLeaf: !entry.isDir,
|
isLeaf: !entry.isDir,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@@ -94,46 +94,83 @@ export function DirectoryPicker({ value, onChange, placeholder, mode = 'director
|
|||||||
setModalVisible(false)
|
setModalVisible(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleInputKeyDown(e: React.KeyboardEvent) {
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
e.preventDefault()
|
||||||
|
const trimmed = value?.trim()
|
||||||
|
if (trimmed) {
|
||||||
|
onChange(trimmed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 没有 nodeId 时退化为普通输入框
|
// 没有 nodeId 时退化为普通输入框
|
||||||
if (nodeId === undefined) {
|
if (nodeId === undefined) {
|
||||||
return <Input value={value} placeholder={placeholder} onChange={onChange} />
|
return <Input value={value} placeholder={placeholder} onChange={onChange} onKeyDown={handleInputKeyDown} />
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Space style={{ width: '100%' }}>
|
<div style={{ display: 'flex', gap: 8, width: '100%' }}>
|
||||||
<Input style={{ flex: 1 }} value={value} placeholder={placeholder} onChange={onChange} />
|
<Input
|
||||||
<Button type="outline" size="small" onClick={handleOpen}>
|
style={{ flex: 1 }}
|
||||||
|
value={value}
|
||||||
|
placeholder={placeholder}
|
||||||
|
onChange={onChange}
|
||||||
|
onKeyDown={handleInputKeyDown}
|
||||||
|
allowClear
|
||||||
|
/>
|
||||||
|
<Button type="outline" size="default" onClick={handleOpen} icon={<IconFolderAdd />}>
|
||||||
浏览
|
浏览
|
||||||
</Button>
|
</Button>
|
||||||
</Space>
|
</div>
|
||||||
|
|
||||||
<Modal
|
<Modal
|
||||||
title={mode === 'directory' ? '选择目录' : '选择文件'}
|
title={mode === 'directory' ? '选择目录' : '选择文件'}
|
||||||
visible={modalVisible}
|
visible={modalVisible}
|
||||||
onCancel={() => setModalVisible(false)}
|
onCancel={() => setModalVisible(false)}
|
||||||
onOk={handleConfirm}
|
onOk={handleConfirm}
|
||||||
okText="选择"
|
okText="确认选择"
|
||||||
cancelText="取消"
|
cancelText="取消"
|
||||||
style={{ width: 560 }}
|
style={{ width: 640 }}
|
||||||
okButtonProps={{ disabled: !selectedPath }}
|
okButtonProps={{ disabled: !selectedPath }}
|
||||||
unmountOnExit
|
unmountOnExit
|
||||||
>
|
>
|
||||||
{selectedPath && (
|
{/* 当前选中路径 */}
|
||||||
<div style={{ padding: '8px 12px', marginBottom: 12, background: 'var(--color-fill-2)', borderRadius: 4 }}>
|
<div style={{
|
||||||
<Typography.Text copyable style={{ fontSize: 13 }}>
|
padding: '10px 14px',
|
||||||
|
marginBottom: 16,
|
||||||
|
background: selectedPath ? 'var(--color-primary-light-1)' : 'var(--color-fill-2)',
|
||||||
|
borderRadius: 6,
|
||||||
|
border: selectedPath ? '1px solid var(--color-primary-light-3)' : '1px solid var(--color-border)',
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
gap: 8,
|
||||||
|
minHeight: 40,
|
||||||
|
}}>
|
||||||
|
<IconFolder style={{ color: selectedPath ? 'var(--color-primary-6)' : 'var(--color-text-4)', fontSize: 16, flexShrink: 0 }} />
|
||||||
|
{selectedPath ? (
|
||||||
|
<Typography.Text copyable style={{ fontSize: 13, fontFamily: 'monospace', wordBreak: 'break-all' }}>
|
||||||
{selectedPath}
|
{selectedPath}
|
||||||
</Typography.Text>
|
</Typography.Text>
|
||||||
</div>
|
) : (
|
||||||
)}
|
<Typography.Text type="secondary" style={{ fontSize: 13 }}>请在下方目录树中选择路径</Typography.Text>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 目录树 */}
|
||||||
{loading ? (
|
{loading ? (
|
||||||
<Spin style={{ display: 'block', textAlign: 'center', padding: 40 }} />
|
<Spin style={{ display: 'block', textAlign: 'center', padding: 48 }} tip="加载目录中..." />
|
||||||
) : treeData.length === 0 ? (
|
) : treeData.length === 0 ? (
|
||||||
<Typography.Text type="secondary" style={{ display: 'block', textAlign: 'center', padding: 40 }}>
|
<Empty style={{ padding: 48 }} description="目录为空" />
|
||||||
目录为空
|
|
||||||
</Typography.Text>
|
|
||||||
) : (
|
) : (
|
||||||
<div style={{ maxHeight: 420, overflow: 'auto', border: '1px solid var(--color-border)', borderRadius: 4, padding: '4px 0' }}>
|
<div style={{
|
||||||
|
maxHeight: 400,
|
||||||
|
overflow: 'auto',
|
||||||
|
border: '1px solid var(--color-border)',
|
||||||
|
borderRadius: 6,
|
||||||
|
padding: '6px 0',
|
||||||
|
}}>
|
||||||
<Tree
|
<Tree
|
||||||
blockNode
|
blockNode
|
||||||
showLine
|
showLine
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { Alert, Button, Divider, Drawer, Input, Select, Space, Switch, Typography } from '@arco-design/web-react'
|
import { Alert, Button, Collapse, Divider, Drawer, Input, Select, Space, Switch, Typography } from '@arco-design/web-react'
|
||||||
import { useEffect, useMemo, useState } from 'react'
|
import { useEffect, useMemo, useState } from 'react'
|
||||||
import { getStorageTargetFieldConfigs, getStorageTargetTypeLabel, storageTargetTypeOptions } from './field-config'
|
import { getStorageTargetFieldConfigs, getStorageTargetTypeLabel, isBuiltinType, buildAllTypeOptions } from './field-config'
|
||||||
import type { StorageConnectionTestResult, StorageTargetDetail, StorageTargetPayload, StorageTargetType } from '../../types/storage-targets'
|
import type { StorageConnectionTestResult, StorageTargetDetail, StorageTargetPayload, StorageTargetType } from '../../types/storage-targets'
|
||||||
import { listRcloneBackends, type RcloneBackendInfo } from '../../services/rclone'
|
import { listRcloneBackends, type RcloneBackendInfo, type RcloneBackendOption } from '../../services/rclone'
|
||||||
|
|
||||||
interface StorageTargetFormDrawerProps {
|
interface StorageTargetFormDrawerProps {
|
||||||
visible: boolean
|
visible: boolean
|
||||||
@@ -16,37 +16,29 @@ interface StorageTargetFormDrawerProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createEmptyDraft(type: StorageTargetType = 'local_disk'): StorageTargetPayload {
|
function createEmptyDraft(type: StorageTargetType = 'local_disk'): StorageTargetPayload {
|
||||||
return {
|
return { name: '', type, description: '', enabled: true, config: {} }
|
||||||
name: '',
|
|
||||||
type,
|
|
||||||
description: '',
|
|
||||||
enabled: true,
|
|
||||||
config: {},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function StorageTargetFormDrawer({
|
export function StorageTargetFormDrawer({
|
||||||
visible,
|
visible, loading, testing, initialValue, onCancel, onSubmit, onTest, onGoogleDriveAuth,
|
||||||
loading,
|
|
||||||
testing,
|
|
||||||
initialValue,
|
|
||||||
onCancel,
|
|
||||||
onSubmit,
|
|
||||||
onTest,
|
|
||||||
onGoogleDriveAuth,
|
|
||||||
}: StorageTargetFormDrawerProps) {
|
}: StorageTargetFormDrawerProps) {
|
||||||
const [draft, setDraft] = useState<StorageTargetPayload>(createEmptyDraft())
|
const [draft, setDraft] = useState<StorageTargetPayload>(createEmptyDraft())
|
||||||
const [error, setError] = useState('')
|
const [error, setError] = useState('')
|
||||||
const [testResult, setTestResult] = useState<StorageConnectionTestResult | null>(null)
|
const [testResult, setTestResult] = useState<StorageConnectionTestResult | null>(null)
|
||||||
|
|
||||||
// rclone 后端列表(API 驱动)
|
|
||||||
const [rcloneBackends, setRcloneBackends] = useState<RcloneBackendInfo[]>([])
|
const [rcloneBackends, setRcloneBackends] = useState<RcloneBackendInfo[]>([])
|
||||||
const [rcloneBackendsLoading, setRcloneBackendsLoading] = useState(false)
|
const [backendsLoaded, setBackendsLoaded] = useState(false)
|
||||||
|
|
||||||
|
// 加载 rclone 后端列表
|
||||||
|
useEffect(() => {
|
||||||
|
if (visible && !backendsLoaded) {
|
||||||
|
listRcloneBackends()
|
||||||
|
.then((data) => { setRcloneBackends(data); setBackendsLoaded(true) })
|
||||||
|
.catch(() => setBackendsLoaded(true))
|
||||||
|
}
|
||||||
|
}, [visible, backendsLoaded])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!visible) {
|
if (!visible) return
|
||||||
return
|
|
||||||
}
|
|
||||||
if (!initialValue) {
|
if (!initialValue) {
|
||||||
setDraft(createEmptyDraft())
|
setDraft(createEmptyDraft())
|
||||||
setError('')
|
setError('')
|
||||||
@@ -64,273 +56,178 @@ export function StorageTargetFormDrawer({
|
|||||||
setTestResult(null)
|
setTestResult(null)
|
||||||
}, [initialValue, visible])
|
}, [initialValue, visible])
|
||||||
|
|
||||||
// 当类型切换到 rclone 时,加载后端列表
|
// 构建分类的类型选项(去重、中文标注)
|
||||||
useEffect(() => {
|
const allTypeOptions = useMemo(() => buildAllTypeOptions(rcloneBackends), [rcloneBackends])
|
||||||
if (draft.type === 'rclone' && rcloneBackends.length === 0 && !rcloneBackendsLoading) {
|
|
||||||
setRcloneBackendsLoading(true)
|
// 按分组聚合,用于 Select 的 OptGroup 渲染
|
||||||
listRcloneBackends()
|
const groupedOptions = useMemo(() => {
|
||||||
.then(setRcloneBackends)
|
const groups: Record<string, { label: string; value: string }[]> = {}
|
||||||
.catch(() => {})
|
for (const opt of allTypeOptions) {
|
||||||
.finally(() => setRcloneBackendsLoading(false))
|
if (!groups[opt.group]) groups[opt.group] = []
|
||||||
|
groups[opt.group].push({ label: opt.label, value: opt.value })
|
||||||
}
|
}
|
||||||
}, [draft.type, rcloneBackends.length, rcloneBackendsLoading])
|
return groups
|
||||||
|
}, [allTypeOptions])
|
||||||
|
|
||||||
const fieldConfigs = useMemo(() => getStorageTargetFieldConfigs(draft.type), [draft.type])
|
// 当前类型是否为非内置(rclone 动态后端)
|
||||||
|
const isDynamicType = !isBuiltinType(draft.type)
|
||||||
|
const staticFields = isBuiltinType(draft.type) ? getStorageTargetFieldConfigs(draft.type) : []
|
||||||
|
|
||||||
// 当前选中的 rclone 后端信息
|
// 当前 rclone 后端的动态字段
|
||||||
const selectedRcloneBackend = useMemo(() => {
|
const dynamicBackend = useMemo(() => {
|
||||||
if (draft.type !== 'rclone') return null
|
if (!isDynamicType) return null
|
||||||
const backendName = draft.config.backend as string
|
return rcloneBackends.find((b) => b.name === draft.type) || null
|
||||||
if (!backendName) return null
|
}, [isDynamicType, draft.type, rcloneBackends])
|
||||||
return rcloneBackends.find((b) => b.name === backendName) || null
|
|
||||||
}, [draft.type, draft.config.backend, rcloneBackends])
|
|
||||||
|
|
||||||
// rclone 后端下拉选项
|
|
||||||
const rcloneBackendOptions = useMemo(() => {
|
|
||||||
return rcloneBackends.map((b) => ({
|
|
||||||
label: `${b.name} — ${b.description}`,
|
|
||||||
value: b.name,
|
|
||||||
}))
|
|
||||||
}, [rcloneBackends])
|
|
||||||
|
|
||||||
function updateConfig(key: string, value: string | boolean) {
|
function updateConfig(key: string, value: string | boolean) {
|
||||||
setDraft((current) => ({
|
setDraft((c) => ({ ...c, config: { ...c.config, [key]: value } }))
|
||||||
...current,
|
|
||||||
config: {
|
|
||||||
...current.config,
|
|
||||||
[key]: value,
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function validate(value: StorageTargetPayload) {
|
function validate(value: StorageTargetPayload) {
|
||||||
if (!value.name.trim()) {
|
if (!value.name.trim()) return '请输入存储目标名称'
|
||||||
return '请输入存储目标名称'
|
if (!value.type.trim()) return '请选择存储类型'
|
||||||
}
|
if (isBuiltinType(value.type)) {
|
||||||
// rclone 类型需要选择后端
|
for (const field of staticFields) {
|
||||||
if (value.type === 'rclone') {
|
if (!field.required || field.type === 'switch') continue
|
||||||
if (!value.config.backend || !(value.config.backend as string).trim()) {
|
const v = value.config[field.key]
|
||||||
return '请选择 Rclone 后端类型'
|
if (typeof v !== 'string' || !v.trim()) return `请填写${field.label}`
|
||||||
}
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
for (const field of fieldConfigs) {
|
|
||||||
if (!field.required) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
const currentValue = value.config[field.key]
|
|
||||||
if (field.type === 'switch') {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if (typeof currentValue !== 'string' || !currentValue.trim()) {
|
|
||||||
return `请填写${field.label}`
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleSubmit() {
|
async function handleSubmit() {
|
||||||
const validationError = validate(draft)
|
const e = validate(draft); if (e) { setError(e); return }
|
||||||
if (validationError) {
|
setError(''); await onSubmit(draft, initialValue?.id)
|
||||||
setError(validationError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
setError('')
|
|
||||||
await onSubmit(draft, initialValue?.id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleTest() {
|
async function handleTest() {
|
||||||
const validationError = validate(draft)
|
const e = validate(draft); if (e) { setError(e); return }
|
||||||
if (validationError) {
|
setError(''); setTestResult(await onTest(draft, initialValue?.id))
|
||||||
setError(validationError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
setError('')
|
|
||||||
const result = await onTest(draft, initialValue?.id)
|
|
||||||
setTestResult(result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleGoogleDriveAuth() {
|
async function handleGoogleDriveAuth() {
|
||||||
const validationError = validate(draft)
|
const e = validate(draft); if (e) { setError(e); return }
|
||||||
if (validationError) {
|
setError(''); await onGoogleDriveAuth(draft, initialValue?.id)
|
||||||
setError(validationError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
setError('')
|
|
||||||
await onGoogleDriveAuth(draft, initialValue?.id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 渲染 rclone 类型的动态配置表单
|
// 渲染静态字段(内置类型)
|
||||||
function renderRcloneFields() {
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<div>
|
|
||||||
<Typography.Text>Rclone 后端类型 *</Typography.Text>
|
|
||||||
<Select
|
|
||||||
showSearch
|
|
||||||
allowClear
|
|
||||||
placeholder="搜索并选择后端(如 sftp, azureblob, dropbox...)"
|
|
||||||
loading={rcloneBackendsLoading}
|
|
||||||
value={(draft.config.backend as string) || undefined}
|
|
||||||
options={rcloneBackendOptions}
|
|
||||||
filterOption={(inputValue, option) => {
|
|
||||||
const label = (option?.props?.children ?? option?.props?.label ?? '') as string
|
|
||||||
return label.toLowerCase().includes(inputValue.toLowerCase())
|
|
||||||
}}
|
|
||||||
onChange={(value) => {
|
|
||||||
// 切换后端时清空旧配置,保留 backend 和 root
|
|
||||||
const root = draft.config.root || ''
|
|
||||||
setDraft((current) => ({
|
|
||||||
...current,
|
|
||||||
config: { backend: value || '', root },
|
|
||||||
}))
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<Typography.Paragraph type="secondary" style={{ marginBottom: 0, marginTop: 4 }}>
|
|
||||||
支持 SFTP、Azure Blob、Dropbox、OneDrive、B2、SMB 等 70+ 存储后端
|
|
||||||
</Typography.Paragraph>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<Typography.Text>远端路径</Typography.Text>
|
|
||||||
<Input
|
|
||||||
value={(draft.config.root as string) || ''}
|
|
||||||
placeholder="/backups 或 bucket-name"
|
|
||||||
onChange={(value) => updateConfig('root', value)}
|
|
||||||
/>
|
|
||||||
<Typography.Paragraph type="secondary" style={{ marginBottom: 0, marginTop: 4 }}>
|
|
||||||
远端存储的根路径、桶名或挂载点,留空使用根目录
|
|
||||||
</Typography.Paragraph>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{selectedRcloneBackend && selectedRcloneBackend.options.length > 0 && (
|
|
||||||
<>
|
|
||||||
<Divider orientation="left" style={{ margin: '8px 0' }}>
|
|
||||||
{selectedRcloneBackend.name} 配置
|
|
||||||
</Divider>
|
|
||||||
{selectedRcloneBackend.options.map((opt) => (
|
|
||||||
<div key={opt.key}>
|
|
||||||
<Typography.Text>
|
|
||||||
{opt.key}
|
|
||||||
{opt.required ? ' *' : ''}
|
|
||||||
</Typography.Text>
|
|
||||||
{opt.isPassword ? (
|
|
||||||
<Input.Password
|
|
||||||
value={(draft.config[opt.key] as string) || ''}
|
|
||||||
placeholder={opt.label}
|
|
||||||
onChange={(value) => updateConfig(opt.key, value)}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<Input
|
|
||||||
value={(draft.config[opt.key] as string) || ''}
|
|
||||||
placeholder={opt.label}
|
|
||||||
onChange={(value) => updateConfig(opt.key, value)}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{opt.label && (
|
|
||||||
<Typography.Paragraph
|
|
||||||
type="secondary"
|
|
||||||
style={{ marginBottom: 0, marginTop: 2, fontSize: 12, lineHeight: '18px' }}
|
|
||||||
ellipsis={{ rows: 2, expandable: true }}
|
|
||||||
>
|
|
||||||
{opt.label}
|
|
||||||
</Typography.Paragraph>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 渲染常规类型的静态字段
|
|
||||||
function renderStaticFields() {
|
function renderStaticFields() {
|
||||||
return fieldConfigs.map((field) => {
|
return staticFields.map((field) => {
|
||||||
const value = draft.config[field.key]
|
const value = draft.config[field.key]
|
||||||
const normalizedValue = typeof value === 'boolean' ? value : typeof value === 'string' ? value : field.type === 'switch' ? false : ''
|
const normalized = typeof value === 'boolean' ? value : typeof value === 'string' ? value : field.type === 'switch' ? false : ''
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div key={field.key}>
|
<div key={field.key}>
|
||||||
<Typography.Text>
|
<Typography.Text>{field.label}{field.required ? ' *' : ''}</Typography.Text>
|
||||||
{field.label}
|
|
||||||
{field.required ? ' *' : ''}
|
|
||||||
</Typography.Text>
|
|
||||||
{field.type === 'switch' ? (
|
{field.type === 'switch' ? (
|
||||||
<Space align="center" size="medium">
|
<Space align="center" size="medium">
|
||||||
<Switch checked={Boolean(normalizedValue)} onChange={(checked) => updateConfig(field.key, checked)} />
|
<Switch checked={Boolean(normalized)} onChange={(v) => updateConfig(field.key, v)} />
|
||||||
{field.description ? <Typography.Text type="secondary">{field.description}</Typography.Text> : null}
|
{field.description && <Typography.Text type="secondary">{field.description}</Typography.Text>}
|
||||||
</Space>
|
</Space>
|
||||||
) : field.type === 'password' ? (
|
) : field.type === 'password' ? (
|
||||||
<Input.Password
|
<Input.Password value={String(normalized)} placeholder={field.placeholder} onChange={(v) => updateConfig(field.key, v)} />
|
||||||
value={String(normalizedValue)}
|
|
||||||
placeholder={field.placeholder}
|
|
||||||
onChange={(nextValue) => updateConfig(field.key, nextValue)}
|
|
||||||
/>
|
|
||||||
) : (
|
) : (
|
||||||
<Input value={String(normalizedValue)} placeholder={field.placeholder} onChange={(nextValue) => updateConfig(field.key, nextValue)} />
|
<Input value={String(normalized)} placeholder={field.placeholder} onChange={(v) => updateConfig(field.key, v)} />
|
||||||
|
)}
|
||||||
|
{field.description && field.type !== 'switch' && (
|
||||||
|
<Typography.Paragraph type="secondary" style={{ marginBottom: 0, marginTop: 4 }}>{field.description}</Typography.Paragraph>
|
||||||
|
)}
|
||||||
|
{initialValue?.maskedFields?.includes(field.key) && !draft.config[field.key] && (
|
||||||
|
<Typography.Paragraph type="secondary" style={{ marginBottom: 0, marginTop: 4 }}>已存在敏感配置,留空则保持不变。</Typography.Paragraph>
|
||||||
)}
|
)}
|
||||||
{field.description && field.type !== 'switch' ? (
|
|
||||||
<Typography.Paragraph type="secondary" style={{ marginBottom: 0, marginTop: 4 }}>
|
|
||||||
{field.description}
|
|
||||||
</Typography.Paragraph>
|
|
||||||
) : null}
|
|
||||||
{initialValue?.maskedFields?.includes(field.key) && !draft.config[field.key] ? (
|
|
||||||
<Typography.Paragraph type="secondary" style={{ marginBottom: 0, marginTop: 4 }}>
|
|
||||||
已存在敏感配置,留空则保持不变。
|
|
||||||
</Typography.Paragraph>
|
|
||||||
) : null}
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 渲染单个动态字段
|
||||||
|
function renderDynamicOption(opt: RcloneBackendOption) {
|
||||||
|
return (
|
||||||
|
<div key={opt.key}>
|
||||||
|
<Typography.Text>{opt.key}{opt.required ? ' *' : ''}</Typography.Text>
|
||||||
|
{opt.isPassword ? (
|
||||||
|
<Input.Password value={(draft.config[opt.key] as string) || ''} placeholder={opt.label} onChange={(v) => updateConfig(opt.key, v)} />
|
||||||
|
) : (
|
||||||
|
<Input value={(draft.config[opt.key] as string) || ''} placeholder={opt.label} onChange={(v) => updateConfig(opt.key, v)} />
|
||||||
|
)}
|
||||||
|
{opt.label && (
|
||||||
|
<Typography.Paragraph type="secondary" style={{ marginBottom: 0, marginTop: 2, fontSize: 12 }} ellipsis={{ rows: 2, expandable: true }}>{opt.label}</Typography.Paragraph>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 渲染动态字段(rclone 后端)— 必填优先,可选折叠
|
||||||
|
function renderDynamicFields() {
|
||||||
|
const requiredOptions = dynamicBackend?.options.filter((opt) => opt.required) ?? []
|
||||||
|
const optionalOptions = dynamicBackend?.options.filter((opt) => !opt.required) ?? []
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div>
|
||||||
|
<Typography.Text>远端路径</Typography.Text>
|
||||||
|
<Input value={(draft.config.root as string) || ''} placeholder="如 /backups 或 bucket 名" onChange={(v) => updateConfig('root', v)} />
|
||||||
|
<Typography.Paragraph type="secondary" style={{ marginBottom: 0, marginTop: 4 }}>远端根路径、桶名或挂载点,留空使用根目录</Typography.Paragraph>
|
||||||
|
</div>
|
||||||
|
{requiredOptions.map(renderDynamicOption)}
|
||||||
|
{optionalOptions.length > 0 && (
|
||||||
|
<Collapse bordered={false} style={{ background: 'transparent' }}>
|
||||||
|
<Collapse.Item
|
||||||
|
header={<Typography.Text type="secondary">高级配置({optionalOptions.length} 个可选项)</Typography.Text>}
|
||||||
|
name="advanced"
|
||||||
|
>
|
||||||
|
<Space direction="vertical" size="large" style={{ width: '100%' }}>
|
||||||
|
{optionalOptions.map(renderDynamicOption)}
|
||||||
|
</Space>
|
||||||
|
</Collapse.Item>
|
||||||
|
</Collapse>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Drawer
|
<Drawer width={560} title={initialValue ? '编辑存储目标' : '新建存储目标'} visible={visible} onCancel={onCancel} unmountOnExit={false}>
|
||||||
width={560}
|
|
||||||
title={initialValue ? '编辑存储目标' : '新建存储目标'}
|
|
||||||
visible={visible}
|
|
||||||
onCancel={onCancel}
|
|
||||||
unmountOnExit={false}
|
|
||||||
>
|
|
||||||
<Space direction="vertical" size="large" style={{ width: '100%' }}>
|
<Space direction="vertical" size="large" style={{ width: '100%' }}>
|
||||||
{error ? <Alert type="error" content={error} /> : <Alert type="info" content="存储目标提供备份文件的最终去向,请确保服务端网络连通性并通过测试。" />}
|
{error ? <Alert type="error" content={error} /> : <Alert type="info" content="存储目标提供备份文件的最终去向,请确保服务端网络连通性并通过测试。" />}
|
||||||
{testResult ? <Alert type={testResult.success ? 'success' : 'warning'} content={testResult.message} /> : null}
|
{testResult && <Alert type={testResult.success ? 'success' : 'warning'} content={testResult.message} />}
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<Typography.Text>名称</Typography.Text>
|
<Typography.Text>名称</Typography.Text>
|
||||||
<Input value={draft.name} placeholder="例如:生产环境 MinIO" onChange={(value) => setDraft((current) => ({ ...current, name: value }))} />
|
<Input value={draft.name} placeholder="例如:生产环境 MinIO" onChange={(v) => setDraft((c) => ({ ...c, name: v }))} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<Typography.Text>类型</Typography.Text>
|
<Typography.Text>存储类型</Typography.Text>
|
||||||
<Select
|
<Select
|
||||||
value={draft.type}
|
showSearch
|
||||||
options={storageTargetTypeOptions as unknown as { label: string; value: string }[]}
|
value={draft.type || undefined}
|
||||||
|
placeholder="搜索存储类型..."
|
||||||
|
filterOption={(input, option) => {
|
||||||
|
const label = String(option?.props?.children ?? option?.props?.label ?? '')
|
||||||
|
return label.toLowerCase().includes(input.toLowerCase())
|
||||||
|
}}
|
||||||
onChange={(value) => {
|
onChange={(value) => {
|
||||||
const nextType = value as StorageTargetType
|
setDraft((c) => ({ ...c, type: value as string, config: {} }))
|
||||||
setDraft((current) => ({
|
|
||||||
...current,
|
|
||||||
type: nextType,
|
|
||||||
config: {},
|
|
||||||
}))
|
|
||||||
setTestResult(null)
|
setTestResult(null)
|
||||||
}}
|
}}
|
||||||
/>
|
>
|
||||||
|
{Object.entries(groupedOptions).map(([group, options]) => (
|
||||||
|
<Select.OptGroup key={group} label={group}>
|
||||||
|
{options.map((opt) => (
|
||||||
|
<Select.Option key={opt.value} value={opt.value}>{opt.label}</Select.Option>
|
||||||
|
))}
|
||||||
|
</Select.OptGroup>
|
||||||
|
))}
|
||||||
|
</Select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<Typography.Text>描述</Typography.Text>
|
<Typography.Text>描述</Typography.Text>
|
||||||
<Input.TextArea
|
<Input.TextArea value={draft.description} placeholder="可选描述" onChange={(v) => setDraft((c) => ({ ...c, description: v }))} />
|
||||||
value={draft.description}
|
|
||||||
placeholder="可选描述,例如备份上传到 NAS 或 Google Drive"
|
|
||||||
onChange={(value) => setDraft((current) => ({ ...current, description: value }))}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Space align="center" size="medium">
|
<Space align="center" size="medium">
|
||||||
<Typography.Text>启用</Typography.Text>
|
<Typography.Text>启用</Typography.Text>
|
||||||
<Switch checked={draft.enabled} onChange={(checked) => setDraft((current) => ({ ...current, enabled: checked }))} />
|
<Switch checked={draft.enabled} onChange={(v) => setDraft((c) => ({ ...c, enabled: v }))} />
|
||||||
</Space>
|
</Space>
|
||||||
|
|
||||||
<Divider orientation="left">环境配置</Divider>
|
<Divider orientation="left">环境配置</Divider>
|
||||||
@@ -340,22 +237,18 @@ export function StorageTargetFormDrawer({
|
|||||||
{getStorageTargetTypeLabel(draft.type)}
|
{getStorageTargetTypeLabel(draft.type)}
|
||||||
</Typography.Title>
|
</Typography.Title>
|
||||||
<Space direction="vertical" size="large" style={{ width: '100%' }}>
|
<Space direction="vertical" size="large" style={{ width: '100%' }}>
|
||||||
{draft.type === 'rclone' ? renderRcloneFields() : renderStaticFields()}
|
{isDynamicType ? renderDynamicFields() : renderStaticFields()}
|
||||||
</Space>
|
</Space>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Space>
|
<Space>
|
||||||
<Button loading={testing} onClick={handleTest}>
|
<Button loading={testing} onClick={handleTest}>测试连接</Button>
|
||||||
测试连接
|
{draft.type === 'google_drive' && (
|
||||||
</Button>
|
|
||||||
{draft.type === 'google_drive' ? (
|
|
||||||
<Button type="outline" onClick={handleGoogleDriveAuth}>
|
<Button type="outline" onClick={handleGoogleDriveAuth}>
|
||||||
{initialValue ? '重新授权 Google Drive' : '发起 Google Drive 授权'}
|
{initialValue ? '重新授权 Google Drive' : '发起 Google Drive 授权'}
|
||||||
</Button>
|
</Button>
|
||||||
) : null}
|
)}
|
||||||
<Button type="primary" loading={loading} onClick={handleSubmit}>
|
<Button type="primary" loading={loading} onClick={handleSubmit}>保存</Button>
|
||||||
保存
|
|
||||||
</Button>
|
|
||||||
</Space>
|
</Space>
|
||||||
</Space>
|
</Space>
|
||||||
</Drawer>
|
</Drawer>
|
||||||
|
|||||||
@@ -1,305 +1,174 @@
|
|||||||
import type { StorageTargetFieldConfig, StorageTargetType } from '../../types/storage-targets'
|
import type { StorageTargetFieldConfig, StorageTargetType } from '../../types/storage-targets'
|
||||||
|
|
||||||
const FIELD_CONFIG_MAP: Record<StorageTargetType, StorageTargetFieldConfig[]> = {
|
// ---------------------------------------------------------------------------
|
||||||
|
// 内置类型的静态字段配置
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const BUILTIN_FIELD_CONFIG: Record<string, StorageTargetFieldConfig[]> = {
|
||||||
local_disk: [
|
local_disk: [
|
||||||
{
|
{ key: 'basePath', label: '基础目录', type: 'input', required: true, placeholder: '/data/backups', description: 'BackupX 将在该目录下创建和管理备份文件。' },
|
||||||
key: 'basePath',
|
|
||||||
label: '基础目录',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
placeholder: '/data/backups',
|
|
||||||
description: 'BackupX 将在该目录下创建和管理备份文件。',
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
s3: [
|
s3: [
|
||||||
{
|
{ key: 'endpoint', label: 'Endpoint', type: 'input', required: true, placeholder: 'https://s3.amazonaws.com' },
|
||||||
key: 'endpoint',
|
{ key: 'region', label: '区域', type: 'input', required: true, placeholder: 'ap-east-1' },
|
||||||
label: 'Endpoint',
|
{ key: 'bucket', label: 'Bucket', type: 'input', required: true, placeholder: 'backupx-prod' },
|
||||||
type: 'input',
|
{ key: 'accessKeyId', label: 'Access Key ID', type: 'input', required: true, sensitive: true, placeholder: 'AKIA...' },
|
||||||
required: true,
|
{ key: 'secretAccessKey', label: 'Secret Access Key', type: 'password', required: true, sensitive: true },
|
||||||
placeholder: 'https://s3.amazonaws.com',
|
{ key: 'forcePathStyle', label: '强制 Path Style', type: 'switch', description: 'MinIO 等兼容存储需要开启。' },
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'region',
|
|
||||||
label: '区域',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
placeholder: 'ap-east-1',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'bucket',
|
|
||||||
label: 'Bucket',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
placeholder: 'backupx-prod',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'accessKeyId',
|
|
||||||
label: 'Access Key ID',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: 'AKIA...',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'secretAccessKey',
|
|
||||||
label: 'Secret Access Key',
|
|
||||||
type: 'password',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: '输入新的 Secret Access Key',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'forcePathStyle',
|
|
||||||
label: '强制 Path Style',
|
|
||||||
type: 'switch',
|
|
||||||
description: 'MinIO 或部分兼容对象存储通常需要开启。',
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
webdav: [
|
webdav: [
|
||||||
{
|
{ key: 'endpoint', label: 'WebDAV 地址', type: 'input', required: true, placeholder: 'https://dav.example.com/...' },
|
||||||
key: 'endpoint',
|
{ key: 'username', label: '用户名', type: 'input', required: true },
|
||||||
label: 'WebDAV 地址',
|
{ key: 'password', label: '密码', type: 'password', required: true, sensitive: true },
|
||||||
type: 'input',
|
{ key: 'basePath', label: '基础目录', type: 'input', placeholder: '/backupx' },
|
||||||
required: true,
|
|
||||||
placeholder: 'https://dav.example.com/remote.php/dav/files/admin',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'username',
|
|
||||||
label: '用户名',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
placeholder: 'admin',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'password',
|
|
||||||
label: '密码',
|
|
||||||
type: 'password',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: '输入新的 WebDAV 密码',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'basePath',
|
|
||||||
label: '基础目录',
|
|
||||||
type: 'input',
|
|
||||||
placeholder: '/backupx',
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
google_drive: [
|
google_drive: [
|
||||||
{
|
{ key: 'clientId', label: 'Client ID', type: 'input', required: true, sensitive: true },
|
||||||
key: 'clientId',
|
{ key: 'clientSecret', label: 'Client Secret', type: 'password', required: true, sensitive: true },
|
||||||
label: 'Client ID',
|
{ key: 'folderId', label: '目标文件夹 ID', type: 'input', placeholder: '留空则使用根目录' },
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: 'Google OAuth Client ID',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'clientSecret',
|
|
||||||
label: 'Client Secret',
|
|
||||||
type: 'password',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: '输入新的 Google Client Secret',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'folderId',
|
|
||||||
label: '目标文件夹 ID',
|
|
||||||
type: 'input',
|
|
||||||
placeholder: '留空则使用根目录',
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
aliyun_oss: [
|
aliyun_oss: [
|
||||||
{
|
{ key: 'region', label: '区域', type: 'input', required: true, placeholder: 'cn-hangzhou' },
|
||||||
key: 'region',
|
{ key: 'bucket', label: 'Bucket', type: 'input', required: true },
|
||||||
label: '区域 (Region)',
|
{ key: 'accessKeyId', label: 'AccessKey ID', type: 'input', required: true, sensitive: true },
|
||||||
type: 'input',
|
{ key: 'secretAccessKey', label: 'AccessKey Secret', type: 'password', required: true, sensitive: true },
|
||||||
required: true,
|
{ key: 'internalNetwork', label: '使用内网', type: 'switch' },
|
||||||
placeholder: 'cn-hangzhou',
|
|
||||||
description: '如 cn-hangzhou, cn-shanghai, cn-beijing, cn-shenzhen 等。系统会自动组装 Endpoint。',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'bucket',
|
|
||||||
label: 'Bucket',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
placeholder: 'my-backup-bucket',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'accessKeyId',
|
|
||||||
label: 'AccessKey ID',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: 'LTAI...',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'secretAccessKey',
|
|
||||||
label: 'AccessKey Secret',
|
|
||||||
type: 'password',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: '输入新的 AccessKey Secret',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'internalNetwork',
|
|
||||||
label: '使用内网 Endpoint',
|
|
||||||
type: 'switch',
|
|
||||||
description: '同一区域的 ECS 实例可启用内网传输,节省流量费用。',
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
tencent_cos: [
|
tencent_cos: [
|
||||||
{
|
{ key: 'region', label: '区域', type: 'input', required: true, placeholder: 'ap-guangzhou' },
|
||||||
key: 'region',
|
{ key: 'bucket', label: 'Bucket', type: 'input', required: true, placeholder: 'backup-1250000000' },
|
||||||
label: '区域 (Region)',
|
{ key: 'accessKeyId', label: 'SecretId', type: 'input', required: true, sensitive: true },
|
||||||
type: 'input',
|
{ key: 'secretAccessKey', label: 'SecretKey', type: 'password', required: true, sensitive: true },
|
||||||
required: true,
|
|
||||||
placeholder: 'ap-guangzhou',
|
|
||||||
description: '如 ap-guangzhou, ap-shanghai, ap-beijing, ap-chengdu 等。系统会自动组装 Endpoint。',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'bucket',
|
|
||||||
label: 'Bucket',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
placeholder: 'backup-1250000000',
|
|
||||||
description: '格式为 BucketName-APPID,如 backup-1250000000。',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'accessKeyId',
|
|
||||||
label: 'SecretId',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: 'AKIDxxxxxxxx',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'secretAccessKey',
|
|
||||||
label: 'SecretKey',
|
|
||||||
type: 'password',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: '输入新的 SecretKey',
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
qiniu_kodo: [
|
qiniu_kodo: [
|
||||||
{
|
{ key: 'region', label: '区域', type: 'input', required: true, placeholder: 'z0' },
|
||||||
key: 'region',
|
{ key: 'bucket', label: 'Bucket', type: 'input', required: true },
|
||||||
label: '区域 (Region)',
|
{ key: 'accessKeyId', label: 'AccessKey', type: 'input', required: true, sensitive: true },
|
||||||
type: 'input',
|
{ key: 'secretAccessKey', label: 'SecretKey', type: 'password', required: true, sensitive: true },
|
||||||
required: true,
|
|
||||||
placeholder: 'z0',
|
|
||||||
description: '支持 z0(华东), cn-east-2(华东-浙江2), z1(华北), z2(华南), na0(北美), as0(东南亚)。',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'bucket',
|
|
||||||
label: 'Bucket',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
placeholder: 'my-backup',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'accessKeyId',
|
|
||||||
label: 'AccessKey',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: '七牛云 AccessKey',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'secretAccessKey',
|
|
||||||
label: 'SecretKey',
|
|
||||||
type: 'password',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: '输入新的 SecretKey',
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
rclone: [], // 动态表单,字段从 API 获取(见 StorageTargetFormDrawer)
|
|
||||||
ftp: [
|
ftp: [
|
||||||
{
|
{ key: 'host', label: '主机地址', type: 'input', required: true, placeholder: 'ftp.example.com' },
|
||||||
key: 'host',
|
{ key: 'port', label: '端口', type: 'input', placeholder: '21' },
|
||||||
label: '主机地址',
|
{ key: 'username', label: '用户名', type: 'input', required: true },
|
||||||
type: 'input',
|
{ key: 'password', label: '密码', type: 'password', required: true, sensitive: true },
|
||||||
required: true,
|
{ key: 'basePath', label: '基础目录', type: 'input', placeholder: '/backups' },
|
||||||
placeholder: 'ftp.example.com',
|
{ key: 'useTLS', label: 'TLS (FTPS)', type: 'switch' },
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'port',
|
|
||||||
label: '端口',
|
|
||||||
type: 'input',
|
|
||||||
placeholder: '21',
|
|
||||||
description: '默认 FTP 端口为 21。',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'username',
|
|
||||||
label: '用户名',
|
|
||||||
type: 'input',
|
|
||||||
required: true,
|
|
||||||
placeholder: 'backup_user',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'password',
|
|
||||||
label: '密码',
|
|
||||||
type: 'password',
|
|
||||||
required: true,
|
|
||||||
sensitive: true,
|
|
||||||
placeholder: '输入新的 FTP 密码',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'basePath',
|
|
||||||
label: '基础目录',
|
|
||||||
type: 'input',
|
|
||||||
placeholder: '/backups',
|
|
||||||
description: 'FTP 服务器上的目标目录,留空使用根目录。',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 'useTLS',
|
|
||||||
label: '使用 TLS (FTPS)',
|
|
||||||
type: 'switch',
|
|
||||||
description: '启用 Explicit TLS 加密连接。',
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getStorageTargetFieldConfigs(type: StorageTargetType) {
|
const BUILTIN_TYPES = new Set(Object.keys(BUILTIN_FIELD_CONFIG))
|
||||||
return FIELD_CONFIG_MAP[type]
|
|
||||||
|
export function isBuiltinType(type: StorageTargetType): boolean {
|
||||||
|
return BUILTIN_TYPES.has(type)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getStorageTargetTypeLabel(type: StorageTargetType) {
|
export function getStorageTargetFieldConfigs(type: StorageTargetType): StorageTargetFieldConfig[] {
|
||||||
switch (type) {
|
return BUILTIN_FIELD_CONFIG[type] ?? []
|
||||||
case 'local_disk':
|
}
|
||||||
return '本地磁盘'
|
|
||||||
case 'google_drive':
|
// ---------------------------------------------------------------------------
|
||||||
return 'Google Drive'
|
// 存储类型完整列表(分类、中文标注、去重)
|
||||||
case 's3':
|
// ---------------------------------------------------------------------------
|
||||||
return 'S3 Compatible'
|
|
||||||
case 'webdav':
|
export interface TypeOption {
|
||||||
return 'WebDAV'
|
label: string
|
||||||
case 'aliyun_oss':
|
value: string
|
||||||
return '阿里云 OSS'
|
group: string
|
||||||
case 'tencent_cos':
|
}
|
||||||
return '腾讯云 COS'
|
|
||||||
case 'qiniu_kodo':
|
// rclone 后端中不适合做存储目标的(工具类/代理类/只读类)
|
||||||
return '七牛云 Kodo'
|
const EXCLUDED_BACKENDS = new Set([
|
||||||
case 'ftp':
|
'alias', 'cache', 'http', 'archive', 'memory', 'tardigrade', // tardigrade = storj 别名
|
||||||
return 'FTP'
|
'union', 'crypt', 'chunker', 'compress', 'hasher', 'combine',
|
||||||
case 'rclone':
|
'local', // 用内置 local_disk 替代
|
||||||
return 'Rclone (70+ 后端)'
|
'drive', // 用内置 google_drive 替代(避免和 rclone 的 drive 重复)
|
||||||
default:
|
])
|
||||||
return type
|
|
||||||
|
// 内置类型(带中文标签的定制化类型,优先展示)
|
||||||
|
const BUILTIN_OPTIONS: TypeOption[] = [
|
||||||
|
{ label: '本地磁盘', value: 'local_disk', group: '常用' },
|
||||||
|
{ label: 'S3 兼容存储(AWS / MinIO / 阿里云 / 腾讯云等)', value: 's3', group: '常用' },
|
||||||
|
{ label: '阿里云 OSS', value: 'aliyun_oss', group: '常用' },
|
||||||
|
{ label: '腾讯云 COS', value: 'tencent_cos', group: '常用' },
|
||||||
|
{ label: '七牛云 Kodo', value: 'qiniu_kodo', group: '常用' },
|
||||||
|
{ label: 'Google Drive', value: 'google_drive', group: '常用' },
|
||||||
|
{ label: 'WebDAV(Nextcloud / 坚果云等)', value: 'webdav', group: '常用' },
|
||||||
|
{ label: 'FTP / FTPS', value: 'ftp', group: '常用' },
|
||||||
|
]
|
||||||
|
|
||||||
|
// rclone 后端的中文标注(仅标注常见的,其余用原始描述)
|
||||||
|
const RCLONE_LABELS: Record<string, { label: string; group: string }> = {
|
||||||
|
sftp: { label: 'SFTP(SSH 文件传输)', group: '文件传输' },
|
||||||
|
smb: { label: 'SMB / CIFS(Windows 共享)', group: '文件传输' },
|
||||||
|
azureblob: { label: 'Azure Blob 存储', group: '云存储' },
|
||||||
|
azurefiles: { label: 'Azure Files 存储', group: '云存储' },
|
||||||
|
'google cloud storage': { label: 'Google Cloud Storage(GCS)', group: '云存储' },
|
||||||
|
b2: { label: 'Backblaze B2', group: '云存储' },
|
||||||
|
swift: { label: 'OpenStack Swift', group: '云存储' },
|
||||||
|
dropbox: { label: 'Dropbox', group: '网盘' },
|
||||||
|
onedrive: { label: 'Microsoft OneDrive', group: '网盘' },
|
||||||
|
box: { label: 'Box', group: '网盘' },
|
||||||
|
pcloud: { label: 'pCloud', group: '网盘' },
|
||||||
|
mega: { label: 'MEGA', group: '网盘' },
|
||||||
|
'google photos': { label: 'Google Photos', group: '网盘' },
|
||||||
|
yandex: { label: 'Yandex Disk', group: '网盘' },
|
||||||
|
pikpak: { label: 'PikPak', group: '网盘' },
|
||||||
|
iclouddrive: { label: 'iCloud Drive', group: '网盘' },
|
||||||
|
jottacloud: { label: 'Jottacloud', group: '网盘' },
|
||||||
|
hidrive: { label: 'HiDrive', group: '网盘' },
|
||||||
|
protondrive: { label: 'Proton Drive', group: '网盘' },
|
||||||
|
mailru: { label: 'Mail.ru Cloud', group: '网盘' },
|
||||||
|
sugarsync: { label: 'SugarSync', group: '网盘' },
|
||||||
|
putio: { label: 'Put.io', group: '网盘' },
|
||||||
|
zoho: { label: 'Zoho WorkDrive', group: '网盘' },
|
||||||
|
internxt: { label: 'Internxt Drive', group: '网盘' },
|
||||||
|
seafile: { label: 'Seafile', group: '自建存储' },
|
||||||
|
storj: { label: 'Storj 去中心化存储', group: '云存储' },
|
||||||
|
hdfs: { label: 'Hadoop HDFS', group: '企业存储' },
|
||||||
|
oracleobjectstorage: { label: 'Oracle 对象存储', group: '云存储' },
|
||||||
|
qingstor: { label: '青云 QingStor', group: '云存储' },
|
||||||
|
sharefile: { label: 'Citrix ShareFile', group: '企业存储' },
|
||||||
|
filefabric: { label: 'Enterprise File Fabric', group: '企业存储' },
|
||||||
|
netstorage: { label: 'Akamai NetStorage', group: '企业存储' },
|
||||||
|
sia: { label: 'Sia 去中心化存储', group: '云存储' },
|
||||||
|
koofr: { label: 'Koofr / Digi Storage', group: '网盘' },
|
||||||
|
opendrive: { label: 'OpenDrive', group: '网盘' },
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 构建完整类型选项列表(内置 + rclone,去重+分类) */
|
||||||
|
export function buildAllTypeOptions(rcloneBackends: { name: string; description: string }[]): TypeOption[] {
|
||||||
|
const result = [...BUILTIN_OPTIONS]
|
||||||
|
const existingValues = new Set(BUILTIN_OPTIONS.map((o) => o.value))
|
||||||
|
|
||||||
|
for (const backend of rcloneBackends) {
|
||||||
|
if (EXCLUDED_BACKENDS.has(backend.name) || existingValues.has(backend.name)) continue
|
||||||
|
// 也排除和内置类型实际是同一后端的(如 rclone 的 s3, ftp, webdav 已被内置覆盖)
|
||||||
|
existingValues.add(backend.name)
|
||||||
|
|
||||||
|
const meta = RCLONE_LABELS[backend.name]
|
||||||
|
result.push({
|
||||||
|
label: meta?.label ?? `${backend.name} — ${backend.description}`,
|
||||||
|
value: backend.name,
|
||||||
|
group: meta?.group ?? '其他',
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
export const storageTargetTypeOptions = [
|
// ---------------------------------------------------------------------------
|
||||||
{ label: '本地磁盘', value: 'local_disk' },
|
// 类型标签
|
||||||
{ label: '阿里云 OSS', value: 'aliyun_oss' },
|
// ---------------------------------------------------------------------------
|
||||||
{ label: '腾讯云 COS', value: 'tencent_cos' },
|
|
||||||
{ label: '七牛云 Kodo', value: 'qiniu_kodo' },
|
const TYPE_LABELS: Record<string, string> = {
|
||||||
{ label: 'S3 Compatible', value: 's3' },
|
local_disk: '本地磁盘', google_drive: 'Google Drive', s3: 'S3 Compatible',
|
||||||
{ label: 'Google Drive', value: 'google_drive' },
|
webdav: 'WebDAV', aliyun_oss: '阿里云 OSS', tencent_cos: '腾讯云 COS',
|
||||||
{ label: 'WebDAV', value: 'webdav' },
|
qiniu_kodo: '七牛云 Kodo', ftp: 'FTP',
|
||||||
{ label: 'FTP', value: 'ftp' },
|
sftp: 'SFTP', smb: 'SMB', azureblob: 'Azure Blob', dropbox: 'Dropbox',
|
||||||
{ label: 'Rclone (70+ 后端)', value: 'rclone' },
|
onedrive: 'OneDrive', b2: 'Backblaze B2', mega: 'MEGA', pcloud: 'pCloud',
|
||||||
] as const
|
box: 'Box', swift: 'Swift', pikpak: 'PikPak',
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getStorageTargetTypeLabel(type: StorageTargetType): string {
|
||||||
|
return TYPE_LABELS[type] || type.toUpperCase()
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import {
|
|||||||
Table, Button, Space, Tag, Typography, PageHeader, Modal, Input, Message, Badge, Popconfirm, Card, Descriptions, Empty
|
Table, Button, Space, Tag, Typography, PageHeader, Modal, Input, Message, Badge, Popconfirm, Card, Descriptions, Empty
|
||||||
} from '@arco-design/web-react'
|
} from '@arco-design/web-react'
|
||||||
import {
|
import {
|
||||||
IconPlus, IconDelete, IconDesktop, IconCloudDownload
|
IconPlus, IconDelete, IconDesktop, IconCloudDownload, IconEdit
|
||||||
} from '@arco-design/web-react/icon'
|
} from '@arco-design/web-react/icon'
|
||||||
import type { NodeSummary } from '../../types/nodes'
|
import type { NodeSummary } from '../../types/nodes'
|
||||||
import { listNodes, createNode, deleteNode } from '../../services/nodes'
|
import { listNodes, createNode, deleteNode, updateNode } from '../../services/nodes'
|
||||||
|
|
||||||
const { Title, Text } = Typography
|
const { Title, Text } = Typography
|
||||||
|
|
||||||
@@ -17,6 +17,11 @@ export default function NodesPage() {
|
|||||||
const [newNodeName, setNewNodeName] = useState('')
|
const [newNodeName, setNewNodeName] = useState('')
|
||||||
const [newToken, setNewToken] = useState('')
|
const [newToken, setNewToken] = useState('')
|
||||||
|
|
||||||
|
// 编辑状态
|
||||||
|
const [editVisible, setEditVisible] = useState(false)
|
||||||
|
const [editNode, setEditNode] = useState<NodeSummary | null>(null)
|
||||||
|
const [editName, setEditName] = useState('')
|
||||||
|
|
||||||
const fetchNodes = useCallback(async () => {
|
const fetchNodes = useCallback(async () => {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
try {
|
try {
|
||||||
@@ -56,6 +61,21 @@ export default function NodesPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleEdit = async () => {
|
||||||
|
if (!editNode || !editName.trim()) {
|
||||||
|
Message.warning('请输入节点名称')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await updateNode(editNode.id, { name: editName.trim() })
|
||||||
|
Message.success('节点更新成功')
|
||||||
|
setEditVisible(false)
|
||||||
|
fetchNodes()
|
||||||
|
} catch {
|
||||||
|
Message.error('更新节点失败')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const columns = [
|
const columns = [
|
||||||
{
|
{
|
||||||
title: '节点名称',
|
title: '节点名称',
|
||||||
@@ -110,15 +130,22 @@ export default function NodesPage() {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: '操作',
|
title: '操作',
|
||||||
width: 80,
|
width: 120,
|
||||||
render: (_: unknown, record: NodeSummary) => {
|
render: (_: unknown, record: NodeSummary) => (
|
||||||
if (record.isLocal) return <Text type="secondary">-</Text>
|
<Space>
|
||||||
return (
|
<Button
|
||||||
<Popconfirm title="确定删除该节点?" onOk={() => handleDelete(record.id)}>
|
type="text"
|
||||||
<Button type="text" status="danger" icon={<IconDelete />} size="small" />
|
icon={<IconEdit />}
|
||||||
</Popconfirm>
|
size="small"
|
||||||
)
|
onClick={() => { setEditNode(record); setEditName(record.name); setEditVisible(true) }}
|
||||||
},
|
/>
|
||||||
|
{!record.isLocal && (
|
||||||
|
<Popconfirm title="确定删除该节点?" onOk={() => handleDelete(record.id)}>
|
||||||
|
<Button type="text" status="danger" icon={<IconDelete />} size="small" />
|
||||||
|
</Popconfirm>
|
||||||
|
)}
|
||||||
|
</Space>
|
||||||
|
),
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -145,6 +172,7 @@ export default function NodesPage() {
|
|||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
{/* 添加节点弹窗 */}
|
||||||
<Modal
|
<Modal
|
||||||
title="添加远程节点"
|
title="添加远程节点"
|
||||||
visible={createVisible}
|
visible={createVisible}
|
||||||
@@ -175,6 +203,25 @@ export default function NodesPage() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
|
{/* 编辑节点弹窗 */}
|
||||||
|
<Modal
|
||||||
|
title="编辑节点"
|
||||||
|
visible={editVisible}
|
||||||
|
onCancel={() => setEditVisible(false)}
|
||||||
|
onOk={handleEdit}
|
||||||
|
okText="保存"
|
||||||
|
cancelText="取消"
|
||||||
|
>
|
||||||
|
<div style={{ marginBottom: 8 }}>
|
||||||
|
<Text type="secondary">节点名称</Text>
|
||||||
|
</div>
|
||||||
|
<Input
|
||||||
|
placeholder="输入节点名称"
|
||||||
|
value={editName}
|
||||||
|
onChange={setEditName}
|
||||||
|
/>
|
||||||
|
</Modal>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,88 +1,130 @@
|
|||||||
import { Card, Descriptions, Grid, PageHeader, Space, Typography } from '@arco-design/web-react'
|
import { Badge, Button, Card, Descriptions, Grid, Link, Message, PageHeader, Space, Tag, Typography } from '@arco-design/web-react'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { fetchSystemInfo, type SystemInfo } from '../../services/system'
|
import { fetchSystemInfo, checkUpdate, type SystemInfo, type UpdateCheckResult } from '../../services/system'
|
||||||
import { resolveErrorMessage } from '../../utils/error'
|
import { resolveErrorMessage } from '../../utils/error'
|
||||||
import { formatDuration } from '../../utils/format'
|
import { formatDuration } from '../../utils/format'
|
||||||
|
|
||||||
const { Row, Col } = Grid
|
const { Row, Col } = Grid
|
||||||
|
|
||||||
const deploySteps = [
|
function formatBytes(bytes: number | undefined): string {
|
||||||
'1. 构建前端:cd web && npm run build',
|
if (!bytes || bytes <= 0) return '-'
|
||||||
'2. 编译后端:cd server && go build -o backupx ./cmd/backupx',
|
const units = ['B', 'KB', 'MB', 'GB', 'TB']
|
||||||
'3. 部署静态资源与二进制,并按 deploy/ 目录提供的配置接入 Nginx 与 systemd',
|
let i = 0
|
||||||
'4. 首次启动后访问 Web 控制台,完成管理员初始化与存储目标配置',
|
let size = bytes
|
||||||
]
|
while (size >= 1024 && i < units.length - 1) {
|
||||||
|
size /= 1024
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return `${size.toFixed(1)} ${units[i]}`
|
||||||
|
}
|
||||||
|
|
||||||
export function SettingsPage() {
|
export function SettingsPage() {
|
||||||
const [info, setInfo] = useState<SystemInfo | null>(null)
|
const [info, setInfo] = useState<SystemInfo | null>(null)
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const [error, setError] = useState('')
|
const [error, setError] = useState('')
|
||||||
|
const [updateResult, setUpdateResult] = useState<UpdateCheckResult | null>(null)
|
||||||
|
const [checking, setChecking] = useState(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let active = true
|
let active = true
|
||||||
void (async () => {
|
void (async () => {
|
||||||
try {
|
try {
|
||||||
const result = await fetchSystemInfo()
|
const result = await fetchSystemInfo()
|
||||||
if (active) {
|
if (active) { setInfo(result); setError('') }
|
||||||
setInfo(result)
|
|
||||||
setError('')
|
|
||||||
}
|
|
||||||
} catch (loadError) {
|
} catch (loadError) {
|
||||||
if (active) {
|
if (active) setError(resolveErrorMessage(loadError, '加载系统信息失败'))
|
||||||
setError(resolveErrorMessage(loadError, '加载系统设置失败'))
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
if (active) {
|
if (active) setLoading(false)
|
||||||
setLoading(false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
return () => {
|
return () => { active = false }
|
||||||
active = false
|
|
||||||
}
|
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
async function handleCheckUpdate() {
|
||||||
|
setChecking(true)
|
||||||
|
try {
|
||||||
|
const result = await checkUpdate()
|
||||||
|
setUpdateResult(result)
|
||||||
|
} catch (e) {
|
||||||
|
setUpdateResult({ currentVersion: info?.version || '-', latestVersion: '-', hasUpdate: false, error: resolveErrorMessage(e, '检查更新失败') })
|
||||||
|
} finally {
|
||||||
|
setChecking(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Space direction="vertical" size="large" style={{ width: '100%' }}>
|
<Space direction="vertical" size="large" style={{ width: '100%' }}>
|
||||||
<PageHeader
|
<PageHeader style={{ paddingBottom: 16 }} title="系统设置" subTitle="运行信息、磁盘状态与版本更新">
|
||||||
style={{ paddingBottom: 16 }}
|
|
||||||
title="系统设置"
|
|
||||||
subTitle="展示当前运行信息、部署入口和交付所需的基础操作说明"
|
|
||||||
>
|
|
||||||
{error ? <Typography.Text type="error">{error}</Typography.Text> : null}
|
{error ? <Typography.Text type="error">{error}</Typography.Text> : null}
|
||||||
</PageHeader>
|
</PageHeader>
|
||||||
|
|
||||||
<Row gutter={16}>
|
<Row gutter={16}>
|
||||||
<Col span={12}>
|
<Col span={12}>
|
||||||
<Card loading={loading} title="运行信息">
|
<Card loading={loading} title="运行信息">
|
||||||
<Descriptions
|
<Descriptions column={1} border data={[
|
||||||
column={1}
|
{ label: '版本', value: <Space>{info?.version ?? '-'}<Button size="mini" type="text" loading={checking} onClick={handleCheckUpdate}>检查更新</Button></Space> },
|
||||||
border
|
{ label: '运行模式', value: info?.mode === 'release' ? <Tag color="green">生产</Tag> : <Tag color="orange">{info?.mode ?? '-'}</Tag> },
|
||||||
data={[
|
{ label: '运行时长', value: formatDuration(info?.uptimeSeconds) },
|
||||||
{ label: '版本', value: info?.version ?? '-' },
|
{ label: '启动时间', value: info?.startedAt ?? '-' },
|
||||||
{ label: '运行模式', value: info?.mode ?? '-' },
|
{ label: '数据库路径', value: <Typography.Text copyable>{info?.databasePath ?? '-'}</Typography.Text> },
|
||||||
{ label: '运行时长', value: formatDuration(info?.uptimeSeconds) },
|
]} />
|
||||||
{ label: '启动时间', value: info?.startedAt ?? '-' },
|
|
||||||
{ label: '数据库路径', value: info?.databasePath ?? '-' },
|
|
||||||
]}
|
|
||||||
/>
|
|
||||||
</Card>
|
</Card>
|
||||||
</Col>
|
</Col>
|
||||||
<Col span={12}>
|
<Col span={12}>
|
||||||
<Card title="部署资产">
|
<Card loading={loading} title="磁盘状态">
|
||||||
<Space direction="vertical" size="medium" style={{ width: '100%' }}>
|
<Descriptions column={1} border data={[
|
||||||
<Typography.Text>`deploy/nginx.conf`:静态资源托管与 `/api` 反向代理示例。</Typography.Text>
|
{ label: '总空间', value: formatBytes(info?.diskTotal) },
|
||||||
<Typography.Text>`deploy/backupx.service`:systemd 服务单元,负责守护 API 进程。</Typography.Text>
|
{ label: '已用空间', value: formatBytes(info?.diskUsed) },
|
||||||
<Typography.Text>`deploy/install.sh`:一键安装示例脚本,用于创建目录、复制文件并启动服务。</Typography.Text>
|
{ label: '可用空间', value: formatBytes(info?.diskFree) },
|
||||||
<Typography.Text>`README.md`:包含完整部署与使用文档。</Typography.Text>
|
{ label: '使用率', value: info?.diskTotal ? `${((info.diskUsed / info.diskTotal) * 100).toFixed(1)}%` : '-' },
|
||||||
</Space>
|
]} />
|
||||||
</Card>
|
</Card>
|
||||||
</Col>
|
</Col>
|
||||||
</Row>
|
</Row>
|
||||||
|
|
||||||
<Card title="部署步骤">
|
{/* 更新检查结果 */}
|
||||||
<div className="code-block">{deploySteps.join('\n')}</div>
|
{updateResult && (
|
||||||
</Card>
|
<Card title="版本更新">
|
||||||
|
{updateResult.error ? (
|
||||||
|
<Typography.Text type="warning">{updateResult.error}</Typography.Text>
|
||||||
|
) : updateResult.hasUpdate ? (
|
||||||
|
<Space direction="vertical" size="medium" style={{ width: '100%' }}>
|
||||||
|
<Space>
|
||||||
|
<Badge status="processing" />
|
||||||
|
<Typography.Text style={{ fontWeight: 600 }}>
|
||||||
|
有新版本可用:{updateResult.latestVersion}
|
||||||
|
</Typography.Text>
|
||||||
|
<Typography.Text type="secondary">(当前:{updateResult.currentVersion})</Typography.Text>
|
||||||
|
</Space>
|
||||||
|
{updateResult.publishedAt && (
|
||||||
|
<Typography.Text type="secondary">发布时间:{new Date(updateResult.publishedAt).toLocaleString()}</Typography.Text>
|
||||||
|
)}
|
||||||
|
{updateResult.releaseNotes && (
|
||||||
|
<Card size="small" title="更新说明" style={{ maxHeight: 200, overflow: 'auto' }}>
|
||||||
|
<Typography.Paragraph style={{ whiteSpace: 'pre-wrap', marginBottom: 0 }}>{updateResult.releaseNotes}</Typography.Paragraph>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
<Space>
|
||||||
|
{updateResult.downloadUrl && (
|
||||||
|
<Link href={updateResult.downloadUrl} target="_blank">
|
||||||
|
<Button type="outline">下载二进制包</Button>
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
{updateResult.releaseUrl && (
|
||||||
|
<Link href={updateResult.releaseUrl} target="_blank">
|
||||||
|
<Button type="text">Release 详情</Button>
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
</Space>
|
||||||
|
</Space>
|
||||||
|
) : (
|
||||||
|
<Space>
|
||||||
|
<Badge status="success" />
|
||||||
|
<Typography.Text>当前已是最新版本 ({updateResult.currentVersion})</Typography.Text>
|
||||||
|
</Space>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
</Space>
|
</Space>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ export async function createNode(name: string) {
|
|||||||
return unwrapApiEnvelope(response.data)
|
return unwrapApiEnvelope(response.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function updateNode(id: number, data: { name: string }) {
|
||||||
|
const response = await http.put<ApiEnvelope<NodeSummary>>(`/nodes/${id}`, data)
|
||||||
|
return unwrapApiEnvelope(response.data)
|
||||||
|
}
|
||||||
|
|
||||||
export async function deleteNode(id: number) {
|
export async function deleteNode(id: number) {
|
||||||
const response = await http.delete<ApiEnvelope<null>>(`/nodes/${id}`)
|
const response = await http.delete<ApiEnvelope<null>>(`/nodes/${id}`)
|
||||||
return unwrapApiEnvelope(response.data)
|
return unwrapApiEnvelope(response.data)
|
||||||
|
|||||||
@@ -14,6 +14,6 @@ export interface RcloneBackendInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function listRcloneBackends(): Promise<RcloneBackendInfo[]> {
|
export async function listRcloneBackends(): Promise<RcloneBackendInfo[]> {
|
||||||
const { data } = await http.get<{ data: RcloneBackendInfo[] }>('/api/storage-targets/rclone/backends')
|
const { data } = await http.get<{ data: RcloneBackendInfo[] }>('/storage-targets/rclone/backends')
|
||||||
return data.data
|
return data.data
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,11 +11,28 @@ export interface SystemInfo {
|
|||||||
diskUsed: number
|
diskUsed: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface UpdateCheckResult {
|
||||||
|
currentVersion: string
|
||||||
|
latestVersion: string
|
||||||
|
hasUpdate: boolean
|
||||||
|
releaseUrl?: string
|
||||||
|
releaseNotes?: string
|
||||||
|
publishedAt?: string
|
||||||
|
downloadUrl?: string
|
||||||
|
dockerImage?: string
|
||||||
|
error?: string
|
||||||
|
}
|
||||||
|
|
||||||
export async function fetchSystemInfo() {
|
export async function fetchSystemInfo() {
|
||||||
const response = await http.get<{ code: string; message: string; data: SystemInfo }>('/system/info')
|
const response = await http.get<{ code: string; message: string; data: SystemInfo }>('/system/info')
|
||||||
return response.data.data
|
return response.data.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function checkUpdate() {
|
||||||
|
const response = await http.get<{ code: string; message: string; data: UpdateCheckResult }>('/system/update-check')
|
||||||
|
return response.data.data
|
||||||
|
}
|
||||||
|
|
||||||
export async function fetchSettings() {
|
export async function fetchSettings() {
|
||||||
const response = await http.get<{ code: string; message: string; data: Record<string, string> }>('/settings')
|
const response = await http.get<{ code: string; message: string; data: Record<string, string> }>('/settings')
|
||||||
return response.data.data
|
return response.data.data
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
export type StorageTargetType = 'local_disk' | 'google_drive' | 's3' | 'webdav' | 'aliyun_oss' | 'tencent_cos' | 'qiniu_kodo' | 'ftp' | 'rclone'
|
// 内置类型 + 全部 rclone 后端名(sftp, azureblob, dropbox 等)
|
||||||
|
export type StorageTargetType = string
|
||||||
export type StorageTestStatus = 'unknown' | 'success' | 'failed'
|
export type StorageTestStatus = 'unknown' | 'success' | 'failed'
|
||||||
export type StorageFieldType = 'input' | 'password' | 'switch'
|
export type StorageFieldType = 'input' | 'password' | 'switch'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user