Private
Public Access
1
0
Files
u-desk/internal/filesystem/fs.go
绝尘 edd5b7c869 优化:文件操作精确更新,避免占用问题
后端改进:
- API 返回 FileOperationResult 结构体(类型安全)
- 所有操作返回文件信息,支持精确更新
- 删除过度抽象的接口和全局函数包装器(桌面程序不需要)

前端改进:
- 精确更新文件列表(避免整目录刷新)
- 分离 add/remove/update 三个独立函数
- 重命名前智能关闭文件/文件夹,解决占用问题
- 优化错误提示,用户友好提示

技术细节:
- 定义 FileOperationResult 结构体替代 map[string]interface{}
- 前端 API 返回类型从 void 改为 any
- 保留运行时状态(如 is_favorite)
- 智能识别文件占用错误并给出解决建议
2026-02-04 12:13:12 +08:00

69 lines
1.6 KiB
Go

package filesystem
import (
"fmt"
"os/exec"
"path/filepath"
"runtime"
"time"
)
// ========== 辅助函数 ==========
// OpenPath 打开文件或目录(使用系统默认程序)
func OpenPath(path string) error {
// 使用 path.validator 进行验证
validator := NewPathValidator(DefaultConfig())
if err := validator.Validate(path); err != nil && err.IsError {
return fmt.Errorf("路径不安全: %w", err)
}
path = filepath.Clean(path)
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
// Windows: 使用 rundll32 打开文件(更可靠)
// 这种方式比 cmd start 更稳定,支持所有文件类型
cmd = exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", path)
case "darwin":
// macOS: 使用 open 命令
cmd = exec.Command("open", path)
case "linux":
// Linux: 使用 xdg-open 命令
cmd = exec.Command("xdg-open", path)
default:
return fmt.Errorf("不支持的操作系统")
}
// 启动命令(不等待完成)
if err := cmd.Start(); err != nil {
return fmt.Errorf("打开文件失败: %v", err)
}
// 给进程一点时间启动
go func() {
time.Sleep(100 * time.Millisecond)
cmd.Process.Release()
}()
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])
}