新增:应用配置管理模块,优化文件系统功能
- 新增 ConfigAPI 和 ConfigService 实现配置管理 - 新增 SettingsPanel 和 UpdateNotification 组件 - 文件系统模块化重构,提升代码质量 - 提取公共函数,优化代码结构 - 版本号更新至 0.2.0
This commit is contained in:
137
internal/api/config_api.go
Normal file
137
internal/api/config_api.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"u-desk/internal/service"
|
||||
)
|
||||
|
||||
// ConfigAPI 配置 API
|
||||
type ConfigAPI struct {
|
||||
configService *service.ConfigService
|
||||
}
|
||||
|
||||
// NewConfigAPI 创建配置 API 实例
|
||||
func NewConfigAPI() (*ConfigAPI, error) {
|
||||
configService, err := service.NewConfigService()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ConfigAPI{
|
||||
configService: configService,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetAppConfigResponse 获取应用配置响应
|
||||
type GetAppConfigResponse struct {
|
||||
Tabs []AppTabDefinition `json:"tabs"`
|
||||
VisibleTabs []string `json:"visibleTabs"`
|
||||
DefaultTab string `json:"defaultTab"`
|
||||
}
|
||||
|
||||
// AppTabDefinition 应用 Tab 定义(前端格式)
|
||||
type AppTabDefinition struct {
|
||||
Key string `json:"key"`
|
||||
Title string `json:"title"`
|
||||
Visible bool `json:"visible"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
// SaveAppConfigRequest 保存应用配置请求(前端格式)
|
||||
type SaveAppConfigRequest struct {
|
||||
Tabs []AppTabDefinition `json:"tabs"`
|
||||
VisibleTabs []string `json:"visibleTabs"`
|
||||
DefaultTab string `json:"defaultTab"`
|
||||
}
|
||||
|
||||
// GetAppConfig 获取应用配置
|
||||
func (api *ConfigAPI) GetAppConfig() (map[string]interface{}, error) {
|
||||
tabConfig, err := api.configService.GetTabConfig()
|
||||
if err != nil {
|
||||
return map[string]interface{}{
|
||||
"success": false,
|
||||
"message": fmt.Sprintf("获取配置失败: %v", err),
|
||||
}, err
|
||||
}
|
||||
|
||||
// 转换为前端格式
|
||||
tabs := make([]AppTabDefinition, len(tabConfig.AvailableTabs))
|
||||
visibleTabSet := make(map[string]bool)
|
||||
for _, key := range tabConfig.VisibleTabs {
|
||||
visibleTabSet[key] = true
|
||||
}
|
||||
|
||||
for i, tab := range tabConfig.AvailableTabs {
|
||||
tabs[i] = AppTabDefinition{
|
||||
Key: tab.Key,
|
||||
Title: tab.Title,
|
||||
Visible: visibleTabSet[tab.Key],
|
||||
Enabled: tab.Enabled,
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"success": true,
|
||||
"data": GetAppConfigResponse{
|
||||
Tabs: tabs,
|
||||
VisibleTabs: tabConfig.VisibleTabs,
|
||||
DefaultTab: tabConfig.DefaultTab,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SaveAppConfig 保存应用配置
|
||||
func (api *ConfigAPI) SaveAppConfig(req SaveAppConfigRequest) (map[string]interface{}, error) {
|
||||
// 验证:至少保留一个可见 Tab
|
||||
if len(req.VisibleTabs) < 1 {
|
||||
return map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "至少需要保留一个可见的 Tab",
|
||||
}, fmt.Errorf("至少需要保留一个可见的 Tab")
|
||||
}
|
||||
|
||||
// 验证:默认 Tab 必须在可见列表中
|
||||
defaultTabExists := false
|
||||
for _, key := range req.VisibleTabs {
|
||||
if key == req.DefaultTab {
|
||||
defaultTabExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !defaultTabExists {
|
||||
return map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "默认 Tab 必须在可见列表中",
|
||||
}, fmt.Errorf("默认 Tab 必须在可见列表中")
|
||||
}
|
||||
|
||||
// 转换为服务层格式
|
||||
availableTabs := make([]service.TabDefinition, len(req.Tabs))
|
||||
for i, tab := range req.Tabs {
|
||||
availableTabs[i] = service.TabDefinition{
|
||||
Key: tab.Key,
|
||||
Title: tab.Title,
|
||||
Enabled: tab.Enabled,
|
||||
}
|
||||
}
|
||||
|
||||
tabConfig := &service.TabConfig{
|
||||
AvailableTabs: availableTabs,
|
||||
VisibleTabs: req.VisibleTabs,
|
||||
DefaultTab: req.DefaultTab,
|
||||
}
|
||||
|
||||
// 保存配置
|
||||
if err := api.configService.SaveTabConfig(tabConfig); err != nil {
|
||||
return map[string]interface{}{
|
||||
"success": false,
|
||||
"message": fmt.Sprintf("保存配置失败: %v", err),
|
||||
}, err
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "配置保存成功",
|
||||
"data": nil,
|
||||
}, nil
|
||||
}
|
||||
17
internal/common/constants.go
Normal file
17
internal/common/constants.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package common
|
||||
|
||||
// Default visible tabs configuration
|
||||
const (
|
||||
// TabDatabase 数据库管理 Tab
|
||||
TabDatabase = "db-cli"
|
||||
// TabFileSystem 文件系统 Tab
|
||||
TabFileSystem = "file-system"
|
||||
// TabDevice 设备测试 Tab
|
||||
TabDevice = "device"
|
||||
)
|
||||
|
||||
// DefaultVisibleTabs 默认可见的 Tabs
|
||||
var DefaultVisibleTabs = []string{TabDatabase, TabFileSystem, TabDevice}
|
||||
|
||||
// DefaultTab 默认打开的 Tab
|
||||
const DefaultTab = TabDatabase
|
||||
45
internal/common/path.go
Normal file
45
internal/common/path.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
const (
|
||||
// AppName 应用名称
|
||||
AppName = "u-desk"
|
||||
)
|
||||
|
||||
// GetUserDataDir 获取用户数据目录
|
||||
// 跨平台支持:Windows、macOS、Linux
|
||||
func GetUserDataDir() string {
|
||||
var basePath string
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
// Windows: %LOCALAPPDATA% 或 %APPDATA%
|
||||
basePath = os.Getenv("LOCALAPPDATA")
|
||||
if basePath == "" {
|
||||
basePath = os.Getenv("APPDATA")
|
||||
}
|
||||
case "darwin":
|
||||
// macOS: ~/Library/Application Support
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
basePath = filepath.Join(homeDir, "Library", "Application Support")
|
||||
}
|
||||
default:
|
||||
// Linux: ~/.config
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
basePath = filepath.Join(homeDir, ".config")
|
||||
}
|
||||
}
|
||||
|
||||
if basePath == "" {
|
||||
basePath = "."
|
||||
}
|
||||
|
||||
return filepath.Join(basePath, AppName)
|
||||
}
|
||||
@@ -4,6 +4,17 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// InterfaceSliceToStringSlice 将 []interface{} 安全转换为 []string
|
||||
func InterfaceSliceToStringSlice(slice []interface{}) []string {
|
||||
result := make([]string, 0, len(slice))
|
||||
for _, v := range slice {
|
||||
if str, ok := v.(string); ok && str != "" {
|
||||
result = append(result, str)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// FormatBytes 格式化字节大小为人类可读格式
|
||||
// 例如: 1024 → "1.00 KB", 1048576 → "1.00 MB"
|
||||
func FormatBytes(bytes uint64) string {
|
||||
@@ -18,3 +29,28 @@ func FormatBytes(bytes uint64) string {
|
||||
}
|
||||
return fmt.Sprintf("%.2f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
// Contains 检查切片是否包含元素
|
||||
func Contains[T comparable](slice []T, item T) bool {
|
||||
for _, s := range slice {
|
||||
if s == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Difference 返回在 a 中但不在 b 中的元素
|
||||
func Difference[T comparable](a, b []T) []T {
|
||||
mb := make(map[T]struct{}, len(b))
|
||||
for _, x := range b {
|
||||
mb[x] = struct{}{}
|
||||
}
|
||||
var diff []T
|
||||
for _, x := range a {
|
||||
if _, found := mb[x]; !found {
|
||||
diff = append(diff, x)
|
||||
}
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package filesystem
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
@@ -128,3 +129,15 @@ func GetStackTrace(skip int) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// DeleteRestrictionWarning 删除限制警告
|
||||
// 用于在删除受限文件时提供详细的警告信息
|
||||
type DeleteRestrictionWarning struct {
|
||||
Path string
|
||||
Details string
|
||||
Info os.FileInfo
|
||||
}
|
||||
|
||||
func (w *DeleteRestrictionWarning) Error() string {
|
||||
return fmt.Sprintf("删除限制警告: %s\n%s", w.Path, w.Details)
|
||||
}
|
||||
|
||||
@@ -2,246 +2,101 @@ package filesystem
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 在包级别存储审计日志记录器
|
||||
var auditLogger *AuditLogger
|
||||
// ========== 向后兼容的全局函数包装器 ==========
|
||||
// 这些函数提供向后兼容性,内部委托给 FileSystemService
|
||||
// 新代码应该使用 FileSystemService 而不是这些全局函数
|
||||
|
||||
// InitAudit 初始化文件系统模块(包括审计日志)
|
||||
func InitAudit(logDir string) error {
|
||||
logger, err := NewAuditLogger(logDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
auditLogger = logger
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloseAudit 关闭审计日志
|
||||
func CloseAudit() error {
|
||||
if auditLogger != nil {
|
||||
return auditLogger.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// formatBytes 格式化字节大小为人类可读格式
|
||||
func formatBytes(bytes int64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := bytes / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.2f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
// ReadFile 读取文件内容
|
||||
// ReadFile 读取文件内容(向后兼容包装器)
|
||||
func ReadFile(path string) (string, error) {
|
||||
if !isSafePath(path) {
|
||||
return "", fmt.Errorf("路径不安全")
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("读取文件失败: %v", err)
|
||||
return "", fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
return service.ReadFile(path)
|
||||
}
|
||||
|
||||
// WriteFile 写入文件
|
||||
// WriteFile 写入文件(向后兼容包装器)
|
||||
func WriteFile(path, content string) error {
|
||||
if !isSafePath(path) {
|
||||
return fmt.Errorf("路径不安全")
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("创建目录失败: %v", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
return fmt.Errorf("写入文件失败: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return service.WriteFile(path, content)
|
||||
}
|
||||
|
||||
// ListDir 列出目录内容
|
||||
// ListDir 列出目录内容(向后兼容包装器)
|
||||
func ListDir(path string) ([]map[string]interface{}, error) {
|
||||
if !isSafePath(path) {
|
||||
return nil, fmt.Errorf("路径不安全")
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(path)
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取目录失败: %v", err)
|
||||
return nil, fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
result := []map[string]interface{}{}
|
||||
for _, entry := range entries {
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(path, entry.Name())
|
||||
result = append(result, map[string]interface{}{
|
||||
"name": entry.Name(),
|
||||
"path": fullPath,
|
||||
"is_dir": entry.IsDir(),
|
||||
"size": info.Size(),
|
||||
"mod_time": info.ModTime().Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return service.ListDir(path)
|
||||
}
|
||||
|
||||
// CreateDir 创建目录
|
||||
// CreateDir 创建目录(向后兼容包装器)
|
||||
func CreateDir(path string) error {
|
||||
if !isSafePath(path) {
|
||||
return fmt.Errorf("路径不安全")
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(path, 0755); err != nil {
|
||||
return fmt.Errorf("创建目录失败: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return service.CreateDir(path)
|
||||
}
|
||||
|
||||
// CreateFile 创建空文件
|
||||
// CreateFile 创建空文件(向后兼容包装器)
|
||||
func CreateFile(path string) error {
|
||||
if !isSafePath(path) {
|
||||
return fmt.Errorf("路径不安全")
|
||||
}
|
||||
|
||||
// 检查文件是否已存在
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return fmt.Errorf("文件已存在")
|
||||
}
|
||||
|
||||
// 创建文件(如果父目录不存在,会自动创建)
|
||||
file, err := os.Create(path)
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建文件失败: %v", err)
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
file.Close()
|
||||
|
||||
return nil
|
||||
return service.CreateFile(path)
|
||||
}
|
||||
|
||||
// DeletePath 删除文件或目录
|
||||
// 优化:使用配置驱动的安全检查,支持确认机制
|
||||
// DeletePath 删除文件或目录(向后兼容包装器)
|
||||
func DeletePath(path string) error {
|
||||
// 使用默认配置
|
||||
return DeletePathWithConfig(path, DefaultConfig())
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
return service.DeletePath(path)
|
||||
}
|
||||
|
||||
// DeletePathWithConfig 使用指定配置删除文件或目录
|
||||
// 支持配置化的安全策略和确认机制
|
||||
// DeletePathWithConfig 使用指定配置删除文件或目录(向后兼容包装器)
|
||||
func DeletePathWithConfig(path string, config *Config) error {
|
||||
// 1. 路径安全检查
|
||||
validator := NewPathValidator(config)
|
||||
if err := validator.Validate(path); err != nil && err.IsError {
|
||||
return fmt.Errorf("路径验证失败: %w", err)
|
||||
}
|
||||
|
||||
// 2. 获取文件信息
|
||||
info, err := os.Stat(path)
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return fmt.Errorf("文件或目录不存在")
|
||||
}
|
||||
return fmt.Errorf("获取文件信息失败: %v", err)
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
// 3. 检查删除限制(配置驱动)
|
||||
exceeds, details, checkErr := CheckDeleteRestrictions(path, info, config)
|
||||
if checkErr != nil {
|
||||
return checkErr
|
||||
}
|
||||
// 临时替换服务的配置
|
||||
originalConfig := service.config
|
||||
service.config = config
|
||||
defer func() { service.config = originalConfig }()
|
||||
|
||||
if exceeds {
|
||||
// 根据配置决定是拒绝还是需要确认
|
||||
if config.Security.DeleteRestrictions.RequireConfirm {
|
||||
// TODO: 这里应该触发前端确认对话框
|
||||
// 目前暂时返回警告信息,由前端处理
|
||||
return &DeleteRestrictionWarning{
|
||||
Path: path,
|
||||
Details: details,
|
||||
Info: info,
|
||||
}
|
||||
}
|
||||
// 不需要确认,直接拒绝
|
||||
return fmt.Errorf("删除限制: %s", details)
|
||||
}
|
||||
|
||||
// 4. 执行删除操作
|
||||
if info.IsDir() {
|
||||
if err := os.RemoveAll(path); err != nil {
|
||||
return fmt.Errorf("删除目录失败: %v", err)
|
||||
}
|
||||
} else {
|
||||
if err := os.Remove(path); err != nil {
|
||||
return fmt.Errorf("删除文件失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return service.DeletePath(path)
|
||||
}
|
||||
|
||||
// DeleteRestrictionWarning 删除限制警告
|
||||
// 用于前端显示确认对话框
|
||||
type DeleteRestrictionWarning struct {
|
||||
Path string
|
||||
Details string
|
||||
Info os.FileInfo
|
||||
}
|
||||
|
||||
func (w *DeleteRestrictionWarning) Error() string {
|
||||
return fmt.Sprintf("删除限制警告: %s\n%s", w.Path, w.Details)
|
||||
}
|
||||
|
||||
// GetFileInfo 获取文件信息
|
||||
// GetFileInfo 获取文件信息(向后兼容包装器)
|
||||
func GetFileInfo(path string) (map[string]interface{}, error) {
|
||||
if !isSafePath(path) {
|
||||
return nil, fmt.Errorf("路径不安全")
|
||||
}
|
||||
|
||||
info, err := os.Stat(path)
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("文件或目录不存在")
|
||||
}
|
||||
return nil, fmt.Errorf("获取文件信息失败: %v", err)
|
||||
return nil, fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"name": info.Name(),
|
||||
"path": path,
|
||||
"size": info.Size(),
|
||||
"size_str": formatBytes(info.Size()),
|
||||
"is_dir": info.IsDir(),
|
||||
"mod_time": info.ModTime().Format("2006-01-02 15:04:05"),
|
||||
"mode": info.Mode().String(),
|
||||
}, nil
|
||||
return service.GetFileInfo(path)
|
||||
}
|
||||
|
||||
// OpenPath 打开文件或目录(使用系统默认程序)
|
||||
// 这是一个核心工具函数,保留为独立函数
|
||||
func OpenPath(path string) error {
|
||||
if !isSafePath(path) {
|
||||
return fmt.Errorf("路径不安全")
|
||||
// 使用 path.validator 进行验证
|
||||
validator := NewPathValidator(DefaultConfig())
|
||||
if err := validator.Validate(path); err != nil && err.IsError {
|
||||
return fmt.Errorf("路径不安全: %w", err)
|
||||
}
|
||||
|
||||
path = filepath.Clean(path)
|
||||
@@ -276,3 +131,28 @@ func OpenPath(path string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RenamePath 重命名文件或目录(向后兼容包装器)
|
||||
func RenamePath(oldPath, newPath string) error {
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
return service.RenamePath(oldPath, newPath)
|
||||
}
|
||||
|
||||
// ========== 辅助函数 ==========
|
||||
|
||||
// formatBytes 格式化字节大小为人类可读格式
|
||||
func formatBytes(bytes int64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := bytes / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.2f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"u-desk/internal/common"
|
||||
)
|
||||
|
||||
// FileSystemService 文件系统服务
|
||||
@@ -77,30 +79,22 @@ func (s *FileSystemService) initializeComponents() error {
|
||||
|
||||
// initAuditLogger 初始化审计日志
|
||||
func (s *FileSystemService) initAuditLogger() error {
|
||||
// 获取日志目录
|
||||
userDataDir := getUserDataDir()
|
||||
logDir := filepath.Join(userDataDir, "logs")
|
||||
|
||||
logDir := filepath.Join(common.GetUserDataDir(), "logs")
|
||||
logger, err := NewAuditLogger(logDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.auditLogger = logger
|
||||
return nil
|
||||
}
|
||||
|
||||
// initRecycleBin 初始化回收站
|
||||
func (s *FileSystemService) initRecycleBin() error {
|
||||
// 获取回收站目录
|
||||
userDataDir := getUserDataDir()
|
||||
recycleBinPath := filepath.Join(userDataDir, "recycle_bin")
|
||||
|
||||
recycleBinPath := filepath.Join(common.GetUserDataDir(), "recycle_bin")
|
||||
bin, err := NewRecycleBin(recycleBinPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.recycleBin = bin
|
||||
return nil
|
||||
}
|
||||
@@ -125,11 +119,7 @@ func (s *FileSystemService) ReadFile(path string) (string, error) {
|
||||
return "", fmt.Errorf("读取文件失败: %v", err)
|
||||
}
|
||||
|
||||
// 记录审计日志
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.LogRead(path, int64(len(data)), nil)
|
||||
}
|
||||
|
||||
s.logRead(path, int64(len(data)), nil)
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
@@ -154,18 +144,11 @@ func (s *FileSystemService) WriteFile(path, content string) error {
|
||||
// 写入文件
|
||||
data := []byte(content)
|
||||
if err := os.WriteFile(path, data, DefaultFilePermissions); err != nil {
|
||||
// 记录审计日志
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.LogWrite(path, int64(len(data)), err)
|
||||
}
|
||||
s.logWrite(path, int64(len(data)), err)
|
||||
return fmt.Errorf("写入文件失败: %v", err)
|
||||
}
|
||||
|
||||
// 记录审计日志
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.LogWrite(path, int64(len(data)), nil)
|
||||
}
|
||||
|
||||
s.logWrite(path, int64(len(data)), nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -247,10 +230,7 @@ func (s *FileSystemService) DeletePathWithContext(ctx context.Context, path stri
|
||||
deleteErr = os.Remove(path)
|
||||
}
|
||||
|
||||
// 记录审计日志
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.LogDelete(path, info.IsDir(), info.Size(), deleteErr)
|
||||
}
|
||||
s.logDelete(path, info.IsDir(), info.Size(), deleteErr)
|
||||
|
||||
if deleteErr != nil {
|
||||
return fmt.Errorf("删除失败: %v", deleteErr)
|
||||
@@ -301,16 +281,13 @@ func (s *FileSystemService) ListDir(path string) ([]map[string]interface{}, erro
|
||||
})
|
||||
}
|
||||
|
||||
// 记录审计日志
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.Log(AuditLogEntry{
|
||||
Timestamp: getCurrentTimestamp(),
|
||||
Operation: OperationList,
|
||||
Path: path,
|
||||
IsDirectory: true,
|
||||
Success: true,
|
||||
})
|
||||
}
|
||||
s.logAudit(AuditLogEntry{
|
||||
Timestamp: getCurrentTimestamp(),
|
||||
Operation: OperationList,
|
||||
Path: path,
|
||||
IsDirectory: true,
|
||||
Success: true,
|
||||
})
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -325,16 +302,13 @@ func (s *FileSystemService) CreateDir(path string) error {
|
||||
return fmt.Errorf("创建目录失败: %v", err)
|
||||
}
|
||||
|
||||
// 记录审计日志
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.Log(AuditLogEntry{
|
||||
Timestamp: getCurrentTimestamp(),
|
||||
Operation: OperationCreate,
|
||||
Path: path,
|
||||
IsDirectory: true,
|
||||
Success: true,
|
||||
})
|
||||
}
|
||||
s.logAudit(AuditLogEntry{
|
||||
Timestamp: getCurrentTimestamp(),
|
||||
Operation: OperationCreate,
|
||||
Path: path,
|
||||
IsDirectory: true,
|
||||
Success: true,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -356,16 +330,13 @@ func (s *FileSystemService) CreateFile(path string) error {
|
||||
}
|
||||
file.Close()
|
||||
|
||||
// 记录审计日志
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.Log(AuditLogEntry{
|
||||
Timestamp: getCurrentTimestamp(),
|
||||
Operation: OperationCreate,
|
||||
Path: path,
|
||||
IsDirectory: false,
|
||||
Success: true,
|
||||
})
|
||||
}
|
||||
s.logAudit(AuditLogEntry{
|
||||
Timestamp: getCurrentTimestamp(),
|
||||
Operation: OperationCreate,
|
||||
Path: path,
|
||||
IsDirectory: false,
|
||||
Success: true,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -409,6 +380,34 @@ func (s *FileSystemService) OpenPath(path string) error {
|
||||
return OpenPath(path)
|
||||
}
|
||||
|
||||
// RenamePath 重命名文件或目录
|
||||
func (s *FileSystemService) RenamePath(oldPath, newPath string) error {
|
||||
// 验证旧路径
|
||||
if err := s.validatePath(oldPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 验证新路径
|
||||
if err := s.validatePath(newPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 执行重命名
|
||||
if err := os.Rename(oldPath, newPath); err != nil {
|
||||
return fmt.Errorf("重命名失败: %v", err)
|
||||
}
|
||||
|
||||
s.logAudit(AuditLogEntry{
|
||||
Timestamp: getCurrentTimestamp(),
|
||||
Operation: OperationRename,
|
||||
Path: newPath,
|
||||
OldPath: oldPath,
|
||||
Success: true,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ========== ZIP操作接口 ==========
|
||||
|
||||
// ListZip 列出ZIP文件内容
|
||||
@@ -416,16 +415,31 @@ func (s *FileSystemService) ListZip(zipPath string) ([]map[string]interface{}, e
|
||||
return ListZipContents(zipPath)
|
||||
}
|
||||
|
||||
// ListZipContents 列出ZIP文件内容(别名,保持向后兼容)
|
||||
func (s *FileSystemService) ListZipContents(zipPath string) ([]map[string]interface{}, error) {
|
||||
return ListZipContents(zipPath)
|
||||
}
|
||||
|
||||
// ExtractZipFile 从ZIP提取文件内容
|
||||
func (s *FileSystemService) ExtractZipFile(zipPath, filePath string) (string, error) {
|
||||
return ExtractFileFromZip(zipPath, filePath)
|
||||
}
|
||||
|
||||
// ExtractFileFromZip 从ZIP提取文件内容(别名,保持向后兼容)
|
||||
func (s *FileSystemService) ExtractFileFromZip(zipPath, filePath string) (string, error) {
|
||||
return ExtractFileFromZip(zipPath, filePath)
|
||||
}
|
||||
|
||||
// ExtractZipFileToTemp 从ZIP提取文件到临时目录
|
||||
func (s *FileSystemService) ExtractZipFileToTemp(zipPath, filePath string) (string, error) {
|
||||
return ExtractFileFromZipToTemp(zipPath, filePath)
|
||||
}
|
||||
|
||||
// ExtractFileFromZipToTemp 从ZIP提取文件到临时目录(别名,保持向后兼容)
|
||||
func (s *FileSystemService) ExtractFileFromZipToTemp(zipPath, filePath string) (string, error) {
|
||||
return ExtractFileFromZipToTemp(zipPath, filePath)
|
||||
}
|
||||
|
||||
// GetZipFileInfo 获取ZIP文件信息
|
||||
func (s *FileSystemService) GetZipFileInfo(zipPath, filePath string) (map[string]interface{}, error) {
|
||||
return GetZipFileInfo(zipPath, filePath)
|
||||
@@ -433,31 +447,6 @@ func (s *FileSystemService) GetZipFileInfo(zipPath, filePath string) (map[string
|
||||
|
||||
// ========== 辅助函数 ==========
|
||||
|
||||
// getUserDataDir 获取用户数据目录
|
||||
func getUserDataDir() string {
|
||||
var basePath string
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
basePath = os.Getenv("LOCALAPPDATA")
|
||||
if basePath == "" {
|
||||
basePath = os.Getenv("APPDATA")
|
||||
}
|
||||
case "darwin":
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
basePath = filepath.Join(homeDir, "Library", "Application Support")
|
||||
default:
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
basePath = filepath.Join(homeDir, ".config")
|
||||
}
|
||||
|
||||
if basePath == "" {
|
||||
basePath = "."
|
||||
}
|
||||
|
||||
return filepath.Join(basePath, "u-desk")
|
||||
}
|
||||
|
||||
// getCurrentTimestamp 获取当前时间戳
|
||||
func getCurrentTimestamp() time.Time {
|
||||
return time.Now()
|
||||
@@ -465,9 +454,7 @@ func getCurrentTimestamp() time.Time {
|
||||
|
||||
// isInRecycleBin 检查路径是否在回收站中
|
||||
func isInRecycleBin(path string) bool {
|
||||
// 简化版本:检查路径是否包含回收站目录名
|
||||
userDataDir := getUserDataDir()
|
||||
recycleBinPath := filepath.Join(userDataDir, "recycle_bin")
|
||||
recycleBinPath := filepath.Join(common.GetUserDataDir(), "recycle_bin")
|
||||
return filepath.HasPrefix(filepath.Clean(path), filepath.Clean(recycleBinPath))
|
||||
}
|
||||
|
||||
@@ -497,6 +484,163 @@ func (s *FileSystemService) GetRecycleBin() *RecycleBin {
|
||||
return s.recycleBin
|
||||
}
|
||||
|
||||
// ========== 审计日志接口 ==========
|
||||
|
||||
// logAudit 安全记录审计日志(自动处理 nil 检查)
|
||||
func (s *FileSystemService) logAudit(entry AuditLogEntry) {
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.Log(entry)
|
||||
}
|
||||
}
|
||||
|
||||
// logRead 记录读取操作审计日志
|
||||
func (s *FileSystemService) logRead(path string, size int64, err error) {
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.LogRead(path, size, err)
|
||||
}
|
||||
}
|
||||
|
||||
// logWrite 记录写入操作审计日志
|
||||
func (s *FileSystemService) logWrite(path string, size int64, err error) {
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.LogWrite(path, size, err)
|
||||
}
|
||||
}
|
||||
|
||||
// logDelete 记录删除操作审计日志
|
||||
func (s *FileSystemService) logDelete(path string, isDir bool, size int64, err error) {
|
||||
if s.auditLogger != nil {
|
||||
s.auditLogger.LogDelete(path, isDir, size, err)
|
||||
}
|
||||
}
|
||||
|
||||
// GetAuditLogs 获取审计日志
|
||||
func (s *FileSystemService) GetAuditLogs(limit int) ([]map[string]interface{}, error) {
|
||||
if s.auditLogger == nil {
|
||||
return []map[string]interface{}{}, nil
|
||||
}
|
||||
|
||||
logDir := filepath.Join(common.GetUserDataDir(), "logs")
|
||||
entries, err := GetRecentLogs(logDir, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]map[string]interface{}, len(entries))
|
||||
for i, entry := range entries {
|
||||
result[i] = map[string]interface{}{
|
||||
"timestamp": entry.Timestamp.Format("2006-01-02 15:04:05"),
|
||||
"operation": entry.Operation,
|
||||
"path": entry.Path,
|
||||
"size": entry.Size,
|
||||
"is_directory": entry.IsDirectory,
|
||||
"success": entry.Success,
|
||||
"error": entry.Error,
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ========== 回收站接口 ==========
|
||||
|
||||
// GetRecycleBinEntries 获取回收站条目
|
||||
func (s *FileSystemService) GetRecycleBinEntries() ([]map[string]interface{}, error) {
|
||||
if s.recycleBin == nil {
|
||||
return []map[string]interface{}{}, nil
|
||||
}
|
||||
|
||||
entries := s.recycleBin.ListEntries()
|
||||
result := make([]map[string]interface{}, len(entries))
|
||||
|
||||
for i, entry := range entries {
|
||||
result[i] = map[string]interface{}{
|
||||
"original_path": entry.OriginalPath,
|
||||
"deleted_path": entry.DeletedPath,
|
||||
"deleted_time": entry.DeletedTime.Format("2006-01-02 15:04:05"),
|
||||
"size": entry.Size,
|
||||
"is_directory": entry.IsDirectory,
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// RestoreFromRecycleBin 从回收站恢复文件
|
||||
func (s *FileSystemService) RestoreFromRecycleBin(recyclePath string) error {
|
||||
if s.recycleBin == nil {
|
||||
return fmt.Errorf("回收站未初始化")
|
||||
}
|
||||
return s.recycleBin.RestoreFromRecycleBin(recyclePath)
|
||||
}
|
||||
|
||||
// DeletePermanently 永久删除回收站中的文件
|
||||
func (s *FileSystemService) DeletePermanently(recyclePath string) error {
|
||||
if s.recycleBin == nil {
|
||||
return fmt.Errorf("回收站未初始化")
|
||||
}
|
||||
return s.recycleBin.DeletePermanently(recyclePath)
|
||||
}
|
||||
|
||||
// EmptyRecycleBin 清空回收站
|
||||
func (s *FileSystemService) EmptyRecycleBin() error {
|
||||
if s.recycleBin == nil {
|
||||
return fmt.Errorf("回收站未初始化")
|
||||
}
|
||||
return s.recycleBin.Empty()
|
||||
}
|
||||
|
||||
// ResolveShortcut 解析快捷方式(.lnk)文件,返回目标路径
|
||||
func (s *FileSystemService) ResolveShortcut(lnkPath string) (targetPath string, err error) {
|
||||
// 验证路径
|
||||
if err := s.validatePath(lnkPath); err != nil {
|
||||
return "", fmt.Errorf("路径验证失败: %w", err)
|
||||
}
|
||||
|
||||
// 检查文件扩展名
|
||||
if filepath.Ext(lnkPath) != ".lnk" {
|
||||
return "", fmt.Errorf("不是快捷方式文件")
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if _, err := os.Stat(lnkPath); os.IsNotExist(err) {
|
||||
return "", fmt.Errorf("快捷方式文件不存在")
|
||||
}
|
||||
|
||||
// 使用 Windows PowerShell 解析 lnk 文件
|
||||
// 这种方法更可靠,不需要依赖第三方库
|
||||
if runtime.GOOS == "windows" {
|
||||
// 创建 PowerShell 脚本
|
||||
psScript := fmt.Sprintf(
|
||||
"$shell = New-Object -ComObject WScript.Shell; "+
|
||||
"$shortcut = $shell.CreateShortcut('%s'); "+
|
||||
"$shortcut.TargetPath",
|
||||
lnkPath,
|
||||
)
|
||||
|
||||
// 执行 PowerShell 命令
|
||||
cmd := exec.Command("powershell", "-NoProfile", "-NonInteractive", "-Command", psScript)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("解析快捷方式失败: %w", err)
|
||||
}
|
||||
|
||||
// 去除空白字符
|
||||
targetPath = string(output)
|
||||
targetPath = filepath.Clean(targetPath)
|
||||
|
||||
// 如果目标路径为空,返回错误
|
||||
if targetPath == "" || targetPath == "." {
|
||||
return "", fmt.Errorf("快捷方式目标路径为空")
|
||||
}
|
||||
|
||||
return targetPath, nil
|
||||
}
|
||||
|
||||
// 非 Windows 系统暂不支持
|
||||
return "", fmt.Errorf("当前系统不支持快捷方式解析")
|
||||
}
|
||||
|
||||
// Close 关闭服务,释放资源
|
||||
func (s *FileSystemService) Close(ctx context.Context) error {
|
||||
s.mu.Lock()
|
||||
|
||||
@@ -17,6 +17,9 @@ type FileService interface {
|
||||
GetInfo(path string) (map[string]interface{}, error)
|
||||
Open(path string) error
|
||||
|
||||
// 快捷方式
|
||||
ResolveShortcut(lnkPath string) (targetPath string, err error)
|
||||
|
||||
// 配置
|
||||
GetConfig() *Config
|
||||
Close(ctx context.Context) error
|
||||
|
||||
118
internal/service/config_service.go
Normal file
118
internal/service/config_service.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"u-desk/internal/storage"
|
||||
"u-desk/internal/storage/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ConfigService 配置服务
|
||||
type ConfigService struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewConfigService 创建配置服务实例
|
||||
func NewConfigService() (*ConfigService, error) {
|
||||
db, err := storage.InitFast()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("数据库初始化失败: %w", err)
|
||||
}
|
||||
|
||||
return &ConfigService{db: db}, nil
|
||||
}
|
||||
|
||||
// TabDefinition Tab 定义
|
||||
type TabDefinition struct {
|
||||
Key string `json:"key"`
|
||||
Title string `json:"title"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
// TabConfig Tab 配置
|
||||
type TabConfig struct {
|
||||
AvailableTabs []TabDefinition `json:"available_tabs"`
|
||||
VisibleTabs []string `json:"visible_tabs"`
|
||||
DefaultTab string `json:"default_tab"`
|
||||
}
|
||||
|
||||
// 默认 Tab 配置
|
||||
var defaultTabConfig = TabConfig{
|
||||
AvailableTabs: []TabDefinition{
|
||||
{Key: "db-cli", Title: "数据库", Enabled: true},
|
||||
{Key: "file-system", Title: "文件管理", Enabled: true},
|
||||
{Key: "device", Title: "设备调用测试", Enabled: true},
|
||||
},
|
||||
VisibleTabs: []string{"db-cli", "file-system", "device"},
|
||||
DefaultTab: "db-cli",
|
||||
}
|
||||
|
||||
const (
|
||||
tabConfigKey = "tab_config"
|
||||
)
|
||||
|
||||
// GetTabConfig 获取 Tab 配置
|
||||
func (s *ConfigService) GetTabConfig() (*TabConfig, error) {
|
||||
var config models.AppConfig
|
||||
|
||||
// 查询配置
|
||||
err := s.db.Where("`key` = ?", tabConfigKey).First(&config).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
// 不存在配置,返回默认配置
|
||||
return &defaultTabConfig, nil
|
||||
}
|
||||
return nil, fmt.Errorf("查询配置失败: %w", err)
|
||||
}
|
||||
|
||||
// 解析 JSON
|
||||
var tabConfig TabConfig
|
||||
if err := json.Unmarshal([]byte(config.Value), &tabConfig); err != nil {
|
||||
// 解析失败,返回默认配置
|
||||
return &defaultTabConfig, nil
|
||||
}
|
||||
|
||||
// 验证配置完整性
|
||||
if len(tabConfig.AvailableTabs) == 0 || len(tabConfig.VisibleTabs) == 0 {
|
||||
return &defaultTabConfig, nil
|
||||
}
|
||||
|
||||
return &tabConfig, nil
|
||||
}
|
||||
|
||||
// SaveTabConfig 保存 Tab 配置
|
||||
func (s *ConfigService) SaveTabConfig(config *TabConfig) error {
|
||||
// 序列化为 JSON
|
||||
jsonData, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("序列化配置失败: %w", err)
|
||||
}
|
||||
|
||||
// 查询是否存在配置
|
||||
var existingConfig models.AppConfig
|
||||
err = s.db.Where("`key` = ?", tabConfigKey).First(&existingConfig).Error
|
||||
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
// 不存在,创建新配置
|
||||
newConfig := models.AppConfig{
|
||||
Key: tabConfigKey,
|
||||
Value: string(jsonData),
|
||||
Description: "Tab 显示和排序配置",
|
||||
}
|
||||
if err := s.db.Create(&newConfig).Error; err != nil {
|
||||
return fmt.Errorf("创建配置失败: %w", err)
|
||||
}
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("查询配置失败: %w", err)
|
||||
} else {
|
||||
// 存在,更新配置
|
||||
existingConfig.Value = string(jsonData)
|
||||
if err := s.db.Save(&existingConfig).Error; err != nil {
|
||||
return fmt.Errorf("更新配置失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -27,6 +28,7 @@ type RemoteVersionInfo struct {
|
||||
Changelog string `json:"changelog"`
|
||||
ForceUpdate bool `json:"force_update"`
|
||||
ReleaseDate string `json:"release_date"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
}
|
||||
|
||||
// UpdateCheckResult 更新检查结果
|
||||
@@ -38,6 +40,7 @@ type UpdateCheckResult struct {
|
||||
Changelog string `json:"changelog"`
|
||||
ForceUpdate bool `json:"force_update"`
|
||||
ReleaseDate string `json:"release_date"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
}
|
||||
|
||||
// InstallResult 安装结果
|
||||
@@ -81,8 +84,13 @@ func (s *UpdateService) CheckUpdate() (*UpdateCheckResult, error) {
|
||||
return nil, fmt.Errorf("获取远程版本信息失败: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("[更新检查] 远程版本信息: 版本=%s, 下载地址=%s, 强制更新=%v",
|
||||
remoteInfo.Version, remoteInfo.DownloadURL, remoteInfo.ForceUpdate)
|
||||
log.Printf("[更新检查] 远程版本信息: 版本=%s, 下载地址=%s, 强制更新=%v, 更新日志长度=%d",
|
||||
remoteInfo.Version, remoteInfo.DownloadURL, remoteInfo.ForceUpdate, len(remoteInfo.Changelog))
|
||||
if remoteInfo.Changelog != "" {
|
||||
log.Printf("[更新检查] 更新日志内容: %s", remoteInfo.Changelog)
|
||||
} else {
|
||||
log.Printf("[更新检查] 警告: 远程接口未返回更新日志")
|
||||
}
|
||||
|
||||
// 解析远程版本号
|
||||
remoteVersion, err := ParseVersion(remoteInfo.Version)
|
||||
@@ -106,6 +114,7 @@ func (s *UpdateService) CheckUpdate() (*UpdateCheckResult, error) {
|
||||
Changelog: remoteInfo.Changelog,
|
||||
ForceUpdate: remoteInfo.ForceUpdate,
|
||||
ReleaseDate: remoteInfo.ReleaseDate,
|
||||
FileSize: remoteInfo.FileSize,
|
||||
}
|
||||
|
||||
log.Printf("[更新检查] 检查完成: 有更新=%v", hasUpdate)
|
||||
@@ -138,13 +147,24 @@ func (s *UpdateService) fetchRemoteVersionInfo() (*RemoteVersionInfo, error) {
|
||||
|
||||
log.Printf("[远程版本] 请求远程版本信息: %s", s.checkURL)
|
||||
|
||||
// 添加时间戳参数防止缓存
|
||||
timestamp := time.Now().UnixMilli() // 使用毫秒级时间戳
|
||||
var requestURL string
|
||||
if strings.Contains(s.checkURL, "?") {
|
||||
requestURL = fmt.Sprintf("%s&t=%d", s.checkURL, timestamp)
|
||||
} else {
|
||||
requestURL = fmt.Sprintf("%s?t=%d", s.checkURL, timestamp)
|
||||
}
|
||||
|
||||
log.Printf("[远程版本] 实际请求URL: %s", requestURL)
|
||||
|
||||
// 创建 HTTP 客户端,设置超时
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
resp, err := client.Get(s.checkURL)
|
||||
resp, err := client.Get(requestURL)
|
||||
if err != nil {
|
||||
log.Printf("[远程版本] 网络请求失败: %v", err)
|
||||
return nil, fmt.Errorf("网络请求失败: %v", err)
|
||||
|
||||
@@ -44,9 +44,9 @@ func LoadUpdateConfig() (*UpdateConfig, error) {
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
return &UpdateConfig{
|
||||
CurrentVersion: GetCurrentVersion(),
|
||||
LastCheckTime: time.Time{},
|
||||
LastCheckTime: time.Time{}, // 启动时会立即检查
|
||||
AutoCheckEnabled: true,
|
||||
CheckIntervalMinutes: 1,
|
||||
CheckIntervalMinutes: 5, // 5分钟检查一次
|
||||
CheckURL: "https://img.1216.top/u-desk/last-version.json",
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -194,7 +194,8 @@ func DownloadUpdate(downloadURL string, progressCallback DownloadProgress) (*Dow
|
||||
if elapsed >= 0.3 {
|
||||
progress := float64(0)
|
||||
if contentLength > 0 {
|
||||
progress = normalizeProgress(float64(totalDownloaded) / float64(contentLength) * 100)
|
||||
rawProgress := float64(totalDownloaded) / float64(contentLength) * 100
|
||||
progress = normalizeProgress(rawProgress)
|
||||
}
|
||||
|
||||
speed := float64(0)
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
// ==================== 常量定义 ====================
|
||||
|
||||
// AppVersion 应用版本号(发布时直接修改此处)
|
||||
const AppVersion = "0.1.0"
|
||||
const AppVersion = "0.2.0"
|
||||
|
||||
// ==================== 类型定义 ====================
|
||||
|
||||
|
||||
18
internal/storage/models/app_config.go
Normal file
18
internal/storage/models/app_config.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
// AppConfig 应用配置模型
|
||||
type AppConfig struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Key string `gorm:"type:varchar(50);uniqueIndex;not null" json:"key"`
|
||||
Value string `gorm:"type:text;not null" json:"value"`
|
||||
Description string `gorm:"type:varchar(200)" json:"description"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (AppConfig) TableName() string {
|
||||
return "app_config"
|
||||
}
|
||||
@@ -64,6 +64,7 @@ func InitFast() (*gorm.DB, error) {
|
||||
&models.DbConnection{},
|
||||
&models.SqlTab{},
|
||||
&models.SqlResultHistory{},
|
||||
&models.AppConfig{},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user