Private
Public Access
1
0

新增:应用配置管理模块,优化文件系统功能

- 新增 ConfigAPI 和 ConfigService 实现配置管理
- 新增 SettingsPanel 和 UpdateNotification 组件
- 文件系统模块化重构,提升代码质量
- 提取公共函数,优化代码结构
- 版本号更新至 0.2.0
This commit is contained in:
2026-01-28 22:48:10 +08:00
parent 7e79a53dae
commit b849e6cc46
31 changed files with 3024 additions and 917 deletions

View File

@@ -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()