修复: 文件预览误报脏标记与编辑丢失等一批缺陷

- 脏标记行尾归一(含裸\r),CRLF 文件打开不再误报未保存
- 防抖回灌校验发起文件+flushEmit,杜绝串文件与丢尾
- F5/tab淘汰/读失败/草稿/重命名/保存刷新六处状态一致性修复
- GBK 文件按原编码回写(接口零改动),保存不再悄悄转 UTF-8
- .sh 系高亮根修(StreamLanguage 导入源错),面包屑长名截断
- CSV 大文件门豁免+表头贴顶;空标题滤除与 iframe 边距去除
- BGM 播放器增强收编(拖拽seek/音量/连播/快捷键/路径导航折叠)
- 版本 0.5.3,CHANGELOG 同步
This commit is contained in:
lxy committed 2026-11-22 01:23:26 +08:00
1 parent e5e8054916
commit bce14a0480
26 files changed
+1010 -138

No files matched your search

+53 -2
View File
@@ -15,6 +15,9 @@ import (
"strings"
"sync"
"time"
"unicode/utf8"
"golang.org/x/text/encoding/simplifiedchinese"
"u-desk/internal/common"
)
@@ -50,6 +53,10 @@ type FileSystemService struct {
// 状态管理
mu sync.RWMutex
initialized bool
// 编码回写缓存:ReadFile 判定为 GBK 的文件记录路径 → "gbk",WriteFile 按原编码回写
encodingMu sync.Mutex
encodingMap map[string]string
}
// NewFileSystemService 创建新的文件系统服务
@@ -63,6 +70,7 @@ func NewFileSystemService(config *Config) (*FileSystemService, error) {
config: config,
pathValidator: NewPathValidator(config),
fileTypeManager: NewFileTypeManager(config),
encodingMap: make(map[string]string),
}
// 初始化基础设施组件
@@ -145,9 +153,29 @@ func (s *FileSystemService) ReadFile(path string) (string, error) {
}
s.logRead(path, int64(len(data)), nil)
// 记录 GBK 文件路径,WriteFile 按原编码回写(与 common.BytesToString 同判定口径)
if isGBKBytes(data) {
s.encodingMu.Lock()
s.encodingMap[path] = "gbk"
s.encodingMu.Unlock()
}
return common.BytesToString(data), nil
}
// isGBKBytes 判断原始字节是否为 GBK 编码(非 UTF-8 且 GBK 解码后为合法 UTF-8)
func isGBKBytes(data []byte) bool {
if utf8.Valid(data) {
return false
}
decoded, err := simplifiedchinese.GBK.NewDecoder().Bytes(data)
if err != nil || !utf8.Valid(decoded) {
return false
}
return true
}
// writeFileWithLog 内部写入实现(路径验证+大小检查+写入+日志)
func (s *FileSystemService) writeFileWithLog(path string, data []byte) error {
if err := s.validatePath(path); err != nil {
@@ -168,9 +196,32 @@ func (s *FileSystemService) writeFileWithLog(path string, data []byte) error {
return nil
}
// WriteFile 写入文件
// WriteFile 写入文件(GBK 文件按原编码回写)
func (s *FileSystemService) WriteFile(path, content string) error {
return s.writeFileWithLog(path, []byte(content))
s.encodingMu.Lock()
encoding := s.encodingMap[path]
s.encodingMu.Unlock()
data := []byte(content)
if encoding == "gbk" {
encoded, err := simplifiedchinese.GBK.NewEncoder().Bytes(data)
if err != nil {
return fmt.Errorf("GBK 编码失败: %v", err)
}
data = encoded
}
if err := s.writeFileWithLog(path, data); err != nil {
return err
}
// 写入成功后清除编码缓存项(内容已按原编码落盘,下次读取会重新判定)
if encoding == "gbk" {
s.encodingMu.Lock()
delete(s.encodingMap, path)
s.encodingMu.Unlock()
}
return nil
}
// SaveBase64File 将 base64 编码内容解码后写入二进制文件