修复: 文件预览误报脏标记与编辑丢失等一批缺陷
- 脏标记行尾归一(含裸\r),CRLF 文件打开不再误报未保存 - 防抖回灌校验发起文件+flushEmit,杜绝串文件与丢尾 - F5/tab淘汰/读失败/草稿/重命名/保存刷新六处状态一致性修复 - GBK 文件按原编码回写(接口零改动),保存不再悄悄转 UTF-8 - .sh 系高亮根修(StreamLanguage 导入源错),面包屑长名截断 - CSV 大文件门豁免+表头贴顶;空标题滤除与 iframe 边距去除 - BGM 播放器增强收编(拖拽seek/音量/连播/快捷键/路径导航折叠) - 版本 0.5.3,CHANGELOG 同步
This commit is contained in:
1 parent
e5e8054916
commit
bce14a0480
26 files changed
+1010
-138
No files matched your search
@@ -277,7 +277,7 @@ func handleLocalFileRequest(w http.ResponseWriter, r *http.Request) {
|
||||
transformedContent := transformCssContent(string(content), basePath)
|
||||
|
||||
w.Header().Set("Content-Type", "text/css")
|
||||
w.Header().Set("Cache-Control", "public, max-age=3600")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Write([]byte(transformedContent))
|
||||
log.Printf("[LocalFileHandler] CSS文件转换完成: %s (原始=%d, 转换后=%d)", filePath, len(content), len(transformedContent))
|
||||
return
|
||||
@@ -299,7 +299,7 @@ func handleLocalFileRequest(w http.ResponseWriter, r *http.Request) {
|
||||
transformedContent := transformJsDynamicImports(string(content), basePath)
|
||||
|
||||
w.Header().Set("Content-Type", "application/javascript")
|
||||
w.Header().Set("Cache-Control", "public, max-age=3600")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Write([]byte(transformedContent))
|
||||
log.Printf("[LocalFileHandler] JS文件转换完成: %s (原始=%d, 转换后=%d)", filePath, len(content), len(transformedContent))
|
||||
return
|
||||
@@ -317,12 +317,9 @@ func handleLocalFileRequest(w http.ResponseWriter, r *http.Request) {
|
||||
// 设置响应头
|
||||
contentType := getContentType(ext)
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
// 媒体文件禁用缓存(避免 Chromium ERR_CACHE_OPERATION_NOT_SUPPORTED)
|
||||
if isMediaExt(ext) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
} else {
|
||||
w.Header().Set("Cache-Control", "public, max-age=3600")
|
||||
}
|
||||
// 预览文件一律禁用缓存:文件内容变化后同 URL 必须返回新内容,
|
||||
// 此前的 max-age=3600 会让本地/远程文件预览命中浏览器 1 小时缓存(陈旧内容,刷新也无效)
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
// 支持 Range 请求
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
|
||||
|
||||
@@ -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 编码内容解码后写入二进制文件
|
||||
|
||||
Reference in new issue
Block a user