重构:Wails升级/mermaid主题切换/代码高亮修复/文件系统UI重构
- Wails v2.12.0升级(App绑定新增API、runtime类型扩展) - 修复mermaid暗色主题切换渲染失败(SVG textContent污染→data-mermaid-src保存源码) - 修复代码高亮全语言失效(languageMap静态白名单替代运行时hljs检查) - 文件系统:FileListPanel重写、FileItemRow合并删除、Toolbar简化 - 新增剪贴板图片粘贴(Ctrl+V粘贴图片到当前目录) - 死代码清理:DeviceTest/errorHandler/useLocalStorage移除 - MarkdownEditor优化、theme store增强、CodeMirror加载器精简
This commit is contained in:
@@ -2,17 +2,22 @@ package filesystem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"u-desk/internal/common"
|
||||
)
|
||||
|
||||
const maxReadWriteSize = 10 * 1024 * 1024 // 10MB 读写上限
|
||||
|
||||
// FileOperationResult 文件操作结果
|
||||
type FileOperationResult struct {
|
||||
Path string `json:"path"`
|
||||
@@ -131,9 +136,8 @@ func (s *FileSystemService) ReadFile(path string) (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("获取文件信息失败: %v", err)
|
||||
}
|
||||
const maxReadSize = 10 * 1024 * 1024 // 10MB
|
||||
if info.Size() > maxReadSize {
|
||||
return "", fmt.Errorf("文件过大 (%.1f MB),超过读取上限 (%d MB)", float64(info.Size())/1024/1024, maxReadSize/1024/1024)
|
||||
if info.Size() > maxReadWriteSize {
|
||||
return "", fmt.Errorf("文件过大 (%.1f MB),超过读取上限 (%d MB)", float64(info.Size())/1024/1024, maxReadWriteSize/1024/1024)
|
||||
}
|
||||
|
||||
// 读取文件
|
||||
@@ -151,30 +155,43 @@ func (s *FileSystemService) Write(path, content string) error {
|
||||
return s.WriteFile(path, content)
|
||||
}
|
||||
|
||||
// WriteFile 写入文件
|
||||
func (s *FileSystemService) WriteFile(path, content string) error {
|
||||
// 路径验证
|
||||
// writeFile 内部写入实现(路径验证+大小检查+写入+日志)
|
||||
func (s *FileSystemService) writeFileWithLog(path string, data []byte) error {
|
||||
if err := s.validatePath(path); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 创建目录
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, DefaultDirPermissions); err != nil {
|
||||
return fmt.Errorf("创建目录失败: %v", err)
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
data := []byte(content)
|
||||
if len(data) > maxReadWriteSize {
|
||||
return fmt.Errorf("文件过大 (%.1f MB),超过写入上限 (%d MB)", float64(len(data))/1024/1024, maxReadWriteSize/1024/1024)
|
||||
}
|
||||
if err := os.WriteFile(path, data, DefaultFilePermissions); err != nil {
|
||||
s.logWrite(path, int64(len(data)), err)
|
||||
return fmt.Errorf("写入文件失败: %v", err)
|
||||
}
|
||||
|
||||
s.logWrite(path, int64(len(data)), nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteFile 写入文件
|
||||
func (s *FileSystemService) WriteFile(path, content string) error {
|
||||
return s.writeFileWithLog(path, []byte(content))
|
||||
}
|
||||
|
||||
// SaveBase64File 将 base64 编码内容解码后写入二进制文件
|
||||
func (s *FileSystemService) SaveBase64File(path, base64Content string) error {
|
||||
if strings.TrimSpace(base64Content) == "" {
|
||||
return errors.New("base64 内容不能为空")
|
||||
}
|
||||
data, err := base64.StdEncoding.DecodeString(base64Content)
|
||||
if err != nil {
|
||||
return fmt.Errorf("base64 解码失败: %v", err)
|
||||
}
|
||||
return s.writeFileWithLog(path, data)
|
||||
}
|
||||
|
||||
// List 列出目录内容(实现 FileService 接口)
|
||||
func (s *FileSystemService) List(path string) ([]map[string]interface{}, error) {
|
||||
return s.ListDir(path)
|
||||
|
||||
Reference in New Issue
Block a user