Private
Public Access
1
0

重构:文件系统模块化架构,优化应用启动流程

This commit is contained in:
2026-01-28 00:28:54 +08:00
parent 4a9b25a505
commit 8c577f70e7
123 changed files with 32030 additions and 967 deletions

View File

@@ -3,11 +3,47 @@ package filesystem
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
)
// 在包级别存储审计日志记录器
var auditLogger *AuditLogger
// 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 读取文件内容
func ReadFile(path string) (string, error) {
if !isSafePath(path) {
@@ -51,7 +87,7 @@ func ListDir(path string) ([]map[string]interface{}, error) {
return nil, fmt.Errorf("读取目录失败: %v", err)
}
var result []map[string]interface{}
result := []map[string]interface{}{}
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
@@ -84,12 +120,44 @@ func CreateDir(path string) error {
return nil
}
// DeletePath 删除文件或目录
func DeletePath(path string) error {
// 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)
if err != nil {
return fmt.Errorf("创建文件失败: %v", err)
}
file.Close()
return nil
}
// DeletePath 删除文件或目录
// 优化:使用配置驱动的安全检查,支持确认机制
func DeletePath(path string) error {
// 使用默认配置
return DeletePathWithConfig(path, DefaultConfig())
}
// 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)
if err != nil {
if os.IsNotExist(err) {
@@ -98,6 +166,28 @@ func DeletePath(path string) error {
return fmt.Errorf("获取文件信息失败: %v", err)
}
// 3. 检查删除限制(配置驱动)
exceeds, details, checkErr := CheckDeleteRestrictions(path, info, config)
if checkErr != nil {
return checkErr
}
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)
@@ -111,6 +201,18 @@ func DeletePath(path string) error {
return nil
}
// 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 获取文件信息
func GetFileInfo(path string) (map[string]interface{}, error) {
if !isSafePath(path) {
@@ -125,19 +227,6 @@ func GetFileInfo(path string) (map[string]interface{}, error) {
return nil, fmt.Errorf("获取文件信息失败: %v", err)
}
formatBytes := func(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])
}
return map[string]interface{}{
"name": info.Name(),
"path": path,
@@ -155,36 +244,35 @@ func OpenPath(path string) error {
return fmt.Errorf("路径不安全")
}
// 注意:这里需要导入 os/exec但为了安全暂时不实现执行命令
// 可以考虑使用 Wails 的 runtime 包提供的功能
return fmt.Errorf("打开功能暂未实现,请手动打开: %s", path)
}
path = filepath.Clean(path)
// isSafePath 检查路径是否安全(防止路径遍历攻击)
func isSafePath(path string) bool {
// 清理路径
cleanPath := filepath.Clean(path)
var cmd *exec.Cmd
// 检查是否包含路径遍历
if strings.Contains(cleanPath, "..") {
return false
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("不支持的操作系统")
}
// Windows 下检查是否尝试访问系统关键目录
if runtime.GOOS == "windows" {
lowerPath := strings.ToLower(cleanPath)
// 禁止访问系统关键目录(可根据需要调整)
forbidden := []string{
"c:\\windows",
"c:\\program files",
"c:\\programdata",
}
for _, fb := range forbidden {
if strings.HasPrefix(lowerPath, fb) {
return false
}
}
// 启动命令(不等待完成)
if err := cmd.Start(); err != nil {
return fmt.Errorf("打开文件失败: %v", err)
}
return true
// 给进程一点时间启动
go func() {
time.Sleep(100 * time.Millisecond)
cmd.Process.Release()
}()
return nil
}