fix: Fix the issue of garbled characters when opening UTF-8 encoded files (#9454)

refs: #9411
This commit is contained in:
2025-07-08 14:09:54 +08:00 committed by GitHub
parent 4a07603a7c
commit a067a97e21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -18,6 +18,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unicode/utf8"
"github.com/1Panel-dev/1Panel/agent/app/repo" "github.com/1Panel-dev/1Panel/agent/app/repo"
@ -334,6 +335,7 @@ func (f *FileService) GetContent(op request.FileContentReq) (response.FileInfo,
if len(content) > 1024 { if len(content) > 1024 {
content = content[:1024] content = content[:1024]
} }
if !utf8.Valid(content) {
_, decodeName, _ := charset.DetermineEncoding(content, "") _, decodeName, _ := charset.DetermineEncoding(content, "")
decoder := files.GetDecoderByName(decodeName) decoder := files.GetDecoderByName(decodeName)
if decoder != nil { if decoder != nil {
@ -351,6 +353,7 @@ func (f *FileService) GetContent(op request.FileContentReq) (response.FileInfo,
} }
info.Content = string(contents) info.Content = string(contents)
} }
}
return response.FileInfo{FileInfo: *info}, nil return response.FileInfo{FileInfo: *info}, nil
} }