Skip to content

Commit 794ebfa

Browse files
zhengkunwang223wanghe-fit2cloud
authored andcommitted
fix: 解决部分情况下打开文件中文乱码的问题 (#5425)
Refs #5374
1 parent d33b836 commit 794ebfa

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

backend/app/service/file.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"strings"
1212
"time"
13+
"unicode/utf8"
1314

1415
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
1516
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
@@ -236,20 +237,20 @@ func (f *FileService) GetContent(op request.FileContentReq) (response.FileInfo,
236237
}
237238

238239
content := []byte(info.Content)
239-
var decodeName string
240240
if len(content) > 1024 {
241-
_, decodeName, _ = charset.DetermineEncoding(content[:1024], "")
242-
} else {
243-
_, decodeName, _ = charset.DetermineEncoding(content, "")
244-
}
245-
if decodeName == "windows-1252" {
246-
reader := strings.NewReader(info.Content)
247-
item := transform.NewReader(reader, simplifiedchinese.GBK.NewDecoder())
248-
contents, err := io.ReadAll(item)
249-
if err != nil {
250-
return response.FileInfo{}, err
241+
content = content[:1024]
242+
}
243+
if !utf8.Valid(content) {
244+
_, decodeName, _ := charset.DetermineEncoding(content, "")
245+
if decodeName == "windows-1252" {
246+
reader := strings.NewReader(info.Content)
247+
item := transform.NewReader(reader, simplifiedchinese.GBK.NewDecoder())
248+
contents, err := io.ReadAll(item)
249+
if err != nil {
250+
return response.FileInfo{}, err
251+
}
252+
info.Content = string(contents)
251253
}
252-
info.Content = string(contents)
253254
}
254255
return response.FileInfo{FileInfo: *info}, nil
255256
}

frontend/src/lang/modules/en.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,8 @@ const message = {
17291729
takeDown: 'TakeDown',
17301730
allReadyInstalled: 'Installed',
17311731
installHelper: 'Configuring image acceleration can solve the problem of image pull failure',
1732-
upgradeHelper: 'The abnormal application needs to be synchronized to the normal state first',
1732+
upgradeHelper:
1733+
'Abnormal applications need to be synchronized to normal status first. If the upgrade fails, please check the failure reason in the log audit-system log',
17331734
installWarn:
17341735
'Currently, if the port external access is not checked, it will not be able to access through the external network IP: port. Do you want to continue?',
17351736
showIgnore: 'View ignore application',

frontend/src/lang/modules/tw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ const message = {
16101610
takeDown: '已廢棄',
16111611
allReadyInstalled: '已安裝',
16121612
installHelper: '配置鏡像加速可以解決鏡像拉取失敗的問題',
1613-
upgradeHelper: '異常應用需要先同步到正常狀態',
1613+
upgradeHelper: '異常應用程式需要先同步到正常狀態,升級失敗請在日誌審計-系統日誌中查看失敗原因',
16141614
installWarn: '當前未勾選端口外部訪問,將無法通過外網IP:端口訪問,是否繼續? ',
16151615
showIgnore: '查看忽略應用',
16161616
cancelIgnore: '取消忽略',

frontend/src/lang/modules/zh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ const message = {
16101610
takeDown: '已废弃',
16111611
allReadyInstalled: '已安装',
16121612
installHelper: '配置镜像加速可以解决镜像拉取失败的问题',
1613-
upgradeHelper: '异常应用需要先同步到正常状态',
1613+
upgradeHelper: '异常应用需要先同步到正常状态,升级失败请在日志审计-系统日志中查看失败原因',
16141614
installWarn: '当前未勾选端口外部访问,将无法通过外网IP:端口访问,是否继续?',
16151615
showIgnore: '查看忽略应用',
16161616
cancelIgnore: '取消忽略',

0 commit comments

Comments
 (0)