新增:应用配置管理模块,优化文件系统功能
- 新增 ConfigAPI 和 ConfigService 实现配置管理 - 新增 SettingsPanel 和 UpdateNotification 组件 - 文件系统模块化重构,提升代码质量 - 提取公共函数,优化代码结构 - 版本号更新至 0.2.0
This commit is contained in:
@@ -2,6 +2,7 @@ package filesystem
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
@@ -128,3 +129,15 @@ func GetStackTrace(skip int) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
@@ -2,246 +2,101 @@ package filesystem
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 在包级别存储审计日志记录器
|
||||
var auditLogger *AuditLogger
|
||||
// ========== 向后兼容的全局函数包装器 ==========
|
||||
// 这些函数提供向后兼容性,内部委托给 FileSystemService
|
||||
// 新代码应该使用 FileSystemService 而不是这些全局函数
|
||||
|
||||
// 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 读取文件内容
|
||||
// ReadFile 读取文件内容(向后兼容包装器)
|
||||
func ReadFile(path string) (string, error) {
|
||||
if !isSafePath(path) {
|
||||
return "", fmt.Errorf("路径不安全")
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("读取文件失败: %v", err)
|
||||
return "", fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
return service.ReadFile(path)
|
||||
}
|
||||
|
||||
// WriteFile 写入文件
|
||||
// WriteFile 写入文件(向后兼容包装器)
|
||||
func WriteFile(path, content string) error {
|
||||
if !isSafePath(path) {
|
||||
return fmt.Errorf("路径不安全")
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("创建目录失败: %v", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
return fmt.Errorf("写入文件失败: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return service.WriteFile(path, content)
|
||||
}
|
||||
|
||||
// ListDir 列出目录内容
|
||||
// ListDir 列出目录内容(向后兼容包装器)
|
||||
func ListDir(path string) ([]map[string]interface{}, error) {
|
||||
if !isSafePath(path) {
|
||||
return nil, fmt.Errorf("路径不安全")
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(path)
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取目录失败: %v", err)
|
||||
return nil, fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
result := []map[string]interface{}{}
|
||||
for _, entry := range entries {
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(path, entry.Name())
|
||||
result = append(result, map[string]interface{}{
|
||||
"name": entry.Name(),
|
||||
"path": fullPath,
|
||||
"is_dir": entry.IsDir(),
|
||||
"size": info.Size(),
|
||||
"mod_time": info.ModTime().Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return service.ListDir(path)
|
||||
}
|
||||
|
||||
// CreateDir 创建目录
|
||||
// CreateDir 创建目录(向后兼容包装器)
|
||||
func CreateDir(path string) error {
|
||||
if !isSafePath(path) {
|
||||
return fmt.Errorf("路径不安全")
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(path, 0755); err != nil {
|
||||
return fmt.Errorf("创建目录失败: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return service.CreateDir(path)
|
||||
}
|
||||
|
||||
// CreateFile 创建空文件
|
||||
// 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)
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建文件失败: %v", err)
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
file.Close()
|
||||
|
||||
return nil
|
||||
return service.CreateFile(path)
|
||||
}
|
||||
|
||||
// DeletePath 删除文件或目录
|
||||
// 优化:使用配置驱动的安全检查,支持确认机制
|
||||
// DeletePath 删除文件或目录(向后兼容包装器)
|
||||
func DeletePath(path string) error {
|
||||
// 使用默认配置
|
||||
return DeletePathWithConfig(path, DefaultConfig())
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
return service.DeletePath(path)
|
||||
}
|
||||
|
||||
// DeletePathWithConfig 使用指定配置删除文件或目录
|
||||
// 支持配置化的安全策略和确认机制
|
||||
// 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)
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return fmt.Errorf("文件或目录不存在")
|
||||
}
|
||||
return fmt.Errorf("获取文件信息失败: %v", err)
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
// 3. 检查删除限制(配置驱动)
|
||||
exceeds, details, checkErr := CheckDeleteRestrictions(path, info, config)
|
||||
if checkErr != nil {
|
||||
return checkErr
|
||||
}
|
||||
// 临时替换服务的配置
|
||||
originalConfig := service.config
|
||||
service.config = config
|
||||
defer func() { service.config = originalConfig }()
|
||||
|
||||
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)
|
||||
}
|
||||
} else {
|
||||
if err := os.Remove(path); err != nil {
|
||||
return fmt.Errorf("删除文件失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return service.DeletePath(path)
|
||||
}
|
||||
|
||||
// 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 获取文件信息
|
||||
// GetFileInfo 获取文件信息(向后兼容包装器)
|
||||
func GetFileInfo(path string) (map[string]interface{}, error) {
|
||||
if !isSafePath(path) {
|
||||
return nil, fmt.Errorf("路径不安全")
|
||||
}
|
||||
|
||||
info, err := os.Stat(path)
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("文件或目录不存在")
|
||||
}
|
||||
return nil, fmt.Errorf("获取文件信息失败: %v", err)
|
||||
return nil, fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"name": info.Name(),
|
||||
"path": path,
|
||||
"size": info.Size(),
|
||||
"size_str": formatBytes(info.Size()),
|
||||
"is_dir": info.IsDir(),
|
||||
"mod_time": info.ModTime().Format("2006-01-02 15:04:05"),
|
||||
"mode": info.Mode().String(),
|
||||
}, nil
|
||||
return service.GetFileInfo(path)
|
||||
}
|
||||
|
||||
// OpenPath 打开文件或目录(使用系统默认程序)
|
||||
// 这是一个核心工具函数,保留为独立函数
|
||||
func OpenPath(path string) error {
|
||||
if !isSafePath(path) {
|
||||
return fmt.Errorf("路径不安全")
|
||||
// 使用 path.validator 进行验证
|
||||
validator := NewPathValidator(DefaultConfig())
|
||||
if err := validator.Validate(path); err != nil && err.IsError {
|
||||
return fmt.Errorf("路径不安全: %w", err)
|
||||
}
|
||||
|
||||
path = filepath.Clean(path)
|
||||
@@ -276,3 +131,28 @@ func OpenPath(path string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RenamePath 重命名文件或目录(向后兼容包装器)
|
||||
func RenamePath(oldPath, newPath string) error {
|
||||
service, err := GetGlobalService()
|
||||
if err != nil {
|
||||
return fmt.Errorf("服务未初始化: %v", err)
|
||||
}
|
||||
return service.RenamePath(oldPath, newPath)
|
||||
}
|
||||
|
||||
// ========== 辅助函数 ==========
|
||||
|
||||
// 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])
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -17,6 +17,9 @@ type FileService interface {
|
||||
GetInfo(path string) (map[string]interface{}, error)
|
||||
Open(path string) error
|
||||
|
||||
// 快捷方式
|
||||
ResolveShortcut(lnkPath string) (targetPath string, err error)
|
||||
|
||||
// 配置
|
||||
GetConfig() *Config
|
||||
Close(ctx context.Context) error
|
||||
|
||||
Reference in New Issue
Block a user