新增: 云OSS存储集成(七牛云+阿里云)+多桶导航+GBK编码自动转换
This commit is contained in:
@@ -218,9 +218,10 @@ func handleLocalFileRequest(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
log.Printf("[LocalFileHandler] 最终路径: %s", filePath)
|
||||
|
||||
// 🔒 文件类型白名单检查
|
||||
// 🔒 文件类型白名单检查(临时目录文件放行,用于 OSS/SFTP 预览)
|
||||
ext := strings.ToLower(filepath.Ext(filePath))
|
||||
if !isAllowedFileType(ext) {
|
||||
isTemp := strings.HasPrefix(filePath, os.TempDir())
|
||||
if !isTemp && !isAllowedFileType(ext) {
|
||||
log.Printf("[LocalFileHandler] 不允许的文件类型: %s", ext)
|
||||
http.Error(w, fmt.Sprintf("Forbidden file type: %s", ext), http.StatusForbidden)
|
||||
return
|
||||
|
||||
24
internal/filesystem/encoding.go
Normal file
24
internal/filesystem/encoding.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
// BytesToString 智能编码转换:UTF-8 直接返回,否则尝试 GBK → UTF-8
|
||||
func BytesToString(data []byte) string {
|
||||
if utf8.Valid(data) {
|
||||
return string(data)
|
||||
}
|
||||
// 尝试 GBK 解码
|
||||
reader := transform.NewReader(bytes.NewReader(data), simplifiedchinese.GBK.NewDecoder())
|
||||
decoded, err := io.ReadAll(reader)
|
||||
if err != nil || !utf8.Valid(decoded) {
|
||||
return string(data) // 转换失败或结果无效,返回原始内容
|
||||
}
|
||||
return string(decoded)
|
||||
}
|
||||
@@ -142,7 +142,7 @@ func (s *FileSystemService) ReadFile(path string) (string, error) {
|
||||
}
|
||||
|
||||
s.logRead(path, int64(len(data)), nil)
|
||||
return string(data), nil
|
||||
return BytesToString(data), nil
|
||||
}
|
||||
|
||||
// Write 写入文件内容(实现 FileService 接口)
|
||||
|
||||
Reference in New Issue
Block a user