Private
Public Access
1
0

优化:代码质量提升,修复重复逻辑和语法高亮支持

- 简化计算属性,删除重复代码
- 优化文件扩展名获取逻辑
- 新增文件工具函数库 fileHelpers.js
- 增强 CodeEditor 语法高亮(支持 30+ 语言)
- 修复 Office 文档文件服务器访问权限
- 添加特殊文件名支持(Dockerfile、Makefile 等)
This commit is contained in:
2026-01-30 02:24:09 +08:00
parent b849e6cc46
commit eb2cbad17b
15 changed files with 962 additions and 761 deletions

View File

@@ -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 {

View File

@@ -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 加载更新配置

View File

@@ -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
}