优化:代码质量提升,修复重复逻辑和语法高亮支持
- 简化计算属性,删除重复代码 - 优化文件扩展名获取逻辑 - 新增文件工具函数库 fileHelpers.js - 增强 CodeEditor 语法高亮(支持 30+ 语言) - 修复 Office 文档文件服务器访问权限 - 添加特殊文件名支持(Dockerfile、Makefile 等)
This commit is contained in:
@@ -3,43 +3,24 @@ package common
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
const (
|
||||
// AppName 应用名称
|
||||
AppName = "u-desk"
|
||||
|
||||
// AppDataDir 应用数据目录名称(带点号,表示隐藏目录)
|
||||
AppDataDir = ".u-desk"
|
||||
)
|
||||
|
||||
// GetUserDataDir 获取用户数据目录
|
||||
// 跨平台支持:Windows、macOS、Linux
|
||||
// 所有平台统一使用: ~/.u-desk
|
||||
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")
|
||||
}
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "."
|
||||
}
|
||||
|
||||
if basePath == "" {
|
||||
basePath = "."
|
||||
}
|
||||
|
||||
return filepath.Join(basePath, AppName)
|
||||
return filepath.Join(homeDir, AppDataDir)
|
||||
}
|
||||
|
||||
@@ -276,7 +276,13 @@ func getAllowedExtensions() map[string]bool {
|
||||
".wav": true,
|
||||
".ogg": true,
|
||||
// 文档
|
||||
".pdf": true,
|
||||
".pdf": true,
|
||||
".doc": true,
|
||||
".docx": true,
|
||||
".xls": true,
|
||||
".xlsx": true,
|
||||
".ppt": true,
|
||||
".pptx": true,
|
||||
// 文本
|
||||
".txt": true,
|
||||
".md": true,
|
||||
@@ -346,10 +352,20 @@ func getMIMETypeMapping() map[string]string {
|
||||
".wav": "audio/wav",
|
||||
".ogg": "audio/ogg",
|
||||
".pdf": "application/pdf",
|
||||
// Office 文档
|
||||
".doc": "application/msword",
|
||||
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
".xls": "application/vnd.ms-excel",
|
||||
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
".ppt": "application/vnd.ms-powerpoint",
|
||||
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||
// 文本
|
||||
".txt": "text/plain; charset=utf-8",
|
||||
".html": "text/html; charset=utf-8",
|
||||
".css": "text/css",
|
||||
".js": "application/javascript",
|
||||
".json": "application/json",
|
||||
".xml": "application/xml",
|
||||
".md": "text/markdown",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"u-desk/internal/common"
|
||||
)
|
||||
|
||||
// ==================== 类型定义 ====================
|
||||
@@ -409,18 +411,13 @@ func BackupApplication() (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("获取用户目录失败: %v", err)
|
||||
}
|
||||
|
||||
backupDir := filepath.Join(homeDir, ".go-desk", "backups")
|
||||
backupDir := filepath.Join(common.GetUserDataDir(), "backups")
|
||||
if err := os.MkdirAll(backupDir, 0755); err != nil {
|
||||
return "", fmt.Errorf("创建备份目录失败: %v", err)
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format("20060102-150405")
|
||||
backupFileName := fmt.Sprintf("go-desk-backup-%s%s", timestamp, filepath.Ext(execPath))
|
||||
backupFileName := fmt.Sprintf("u-desk-backup-%s%s", timestamp, filepath.Ext(execPath))
|
||||
backupPath := filepath.Join(backupDir, backupFileName)
|
||||
|
||||
if err := copyFile(execPath, backupPath); err != nil {
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"u-desk/internal/common"
|
||||
)
|
||||
|
||||
// UpdateConfig 更新配置
|
||||
@@ -20,17 +22,12 @@ type UpdateConfig struct {
|
||||
|
||||
// GetUpdateConfigPath 获取更新配置文件路径
|
||||
func GetUpdateConfigPath() (string, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("获取用户目录失败: %v", err)
|
||||
}
|
||||
|
||||
configDir := filepath.Join(homeDir, ".go-desk")
|
||||
if err := os.MkdirAll(configDir, 0755); err != nil {
|
||||
dataDir := common.GetUserDataDir()
|
||||
if err := os.MkdirAll(dataDir, 0755); err != nil {
|
||||
return "", fmt.Errorf("创建配置目录失败: %v", err)
|
||||
}
|
||||
|
||||
return filepath.Join(configDir, "update_config.json"), nil
|
||||
return filepath.Join(dataDir, "update_config.json"), nil
|
||||
}
|
||||
|
||||
// LoadUpdateConfig 加载更新配置
|
||||
|
||||
@@ -5,12 +5,15 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"hash"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"u-desk/internal/common"
|
||||
)
|
||||
|
||||
// ==================== 类型定义 ====================
|
||||
@@ -33,12 +36,7 @@ func DownloadUpdate(downloadURL string, progressCallback DownloadProgress) (*Dow
|
||||
log.Printf("[下载] 开始下载,URL: %s", downloadURL)
|
||||
|
||||
// 获取下载目录
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取用户目录失败: %v", err)
|
||||
}
|
||||
|
||||
downloadDir := filepath.Join(homeDir, ".go-desk", "downloads")
|
||||
downloadDir := filepath.Join(common.GetUserDataDir(), "downloads")
|
||||
if err := os.MkdirAll(downloadDir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("创建下载目录失败: %v", err)
|
||||
}
|
||||
@@ -283,7 +281,33 @@ func normalizeProgress(progress float64) float64 {
|
||||
return progress
|
||||
}
|
||||
|
||||
// calculateFileHashes 计算文件的 MD5 和 SHA256 哈希值
|
||||
// calculateHash 计算文件的哈希值(通用函数)
|
||||
func calculateHash(filePath string, hashType string) (string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var hash hash.Hash
|
||||
|
||||
switch hashType {
|
||||
case "md5":
|
||||
hash = md5.New()
|
||||
case "sha256":
|
||||
hash = sha256.New()
|
||||
default:
|
||||
return "", fmt.Errorf("不支持的哈希类型: %s", hashType)
|
||||
}
|
||||
|
||||
if _, err := io.Copy(hash, file); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// calculateFileHashes 计算文件的 MD5 和 SHA256 哈希值(优化版,使用 MultiWriter)
|
||||
func calculateFileHashes(filePath string) (string, string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
@@ -294,7 +318,7 @@ func calculateFileHashes(filePath string) (string, string, error) {
|
||||
md5Hash := md5.New()
|
||||
sha256Hash := sha256.New()
|
||||
|
||||
// 使用 MultiWriter 同时计算两个哈希
|
||||
// 使用 MultiWriter 同时计算两个哈希,只读取文件一次
|
||||
writer := io.MultiWriter(md5Hash, sha256Hash)
|
||||
|
||||
if _, err := io.Copy(writer, file); err != nil {
|
||||
@@ -309,33 +333,9 @@ func calculateFileHashes(filePath string) (string, string, error) {
|
||||
|
||||
// VerifyFileHash 验证文件哈希值
|
||||
func VerifyFileHash(filePath string, expectedHash string, hashType string) (bool, error) {
|
||||
file, err := os.Open(filePath)
|
||||
calculatedHash, err := calculateHash(filePath, hashType)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var hash []byte
|
||||
var calculatedHash string
|
||||
|
||||
switch hashType {
|
||||
case "md5":
|
||||
md5Hash := md5.New()
|
||||
if _, err := io.Copy(md5Hash, file); err != nil {
|
||||
return false, err
|
||||
}
|
||||
hash = md5Hash.Sum(nil)
|
||||
calculatedHash = hex.EncodeToString(hash)
|
||||
case "sha256":
|
||||
sha256Hash := sha256.New()
|
||||
if _, err := io.Copy(sha256Hash, file); err != nil {
|
||||
return false, err
|
||||
}
|
||||
hash = sha256Hash.Sum(nil)
|
||||
calculatedHash = hex.EncodeToString(hash)
|
||||
default:
|
||||
return false, fmt.Errorf("不支持的哈希类型: %s", hashType)
|
||||
}
|
||||
|
||||
return calculatedHash == expectedHash, nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"u-desk/internal/common"
|
||||
"u-desk/internal/storage/models"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -25,17 +27,13 @@ func InitFast() (*gorm.DB, error) {
|
||||
return globalDB, nil
|
||||
}
|
||||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dataDir := filepath.Join(homeDir, ".go-desk")
|
||||
// 使用统一的数据目录
|
||||
dataDir := common.GetUserDataDir()
|
||||
if err := os.MkdirAll(dataDir, 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dbPath := filepath.Join(dataDir, "db-cli.db")
|
||||
dbPath := filepath.Join(dataDir, "app.db")
|
||||
|
||||
// 极限性能优化参数:
|
||||
// - journal_mode=WAL: 写前日志,大幅提升并发性能
|
||||
@@ -53,7 +51,10 @@ func InitFast() (*gorm.DB, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取底层SQL数据库失败: %v", err)
|
||||
}
|
||||
sqlDB.SetMaxOpenConns(1) // SQLite 只需要一个连接
|
||||
sqlDB.SetMaxIdleConns(1)
|
||||
sqlDB.SetConnMaxLifetime(time.Hour)
|
||||
|
||||
Reference in New Issue
Block a user