Skip to content

Commit 187e7ff

Browse files
Merge pull request #2456 from actiontech/fix-issue2452
convert gbk encoding
2 parents ba52dd1 + fef181f commit 187e7ff

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

sqle/api/controller/v1/sql_audit_record.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ func CreateSQLAuditRecord(c echo.Context) error {
125125
// if task instance is not nil, gorm will update instance when save task.
126126
task.Instance = nil
127127

128+
err = convertSQLSourceEncodingFromTask(task)
129+
if err != nil {
130+
return controller.JSONBaseErrorReq(c, err)
131+
}
132+
128133
recordId, err := utils.GenUid()
129134
if err != nil {
130135
return controller.JSONBaseErrorReq(c, fmt.Errorf("generate audit record id failed: %v", err))

sqle/api/controller/v1/task.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"context"
77
"encoding/csv"
8+
e "errors"
89
"fmt"
910
"mime"
1011
"mime/multipart"
@@ -219,6 +220,17 @@ func getFileRecordsFromZip(multipartFile multipart.File, fileHeader *multipart.F
219220
continue
220221
}
221222
fullName := srcFile.FileHeader.Name // full name with relative path to zip file
223+
if srcFile.NonUTF8 {
224+
utf8NameByte, err := utils.ConvertToUtf8([]byte(fullName))
225+
if err != nil {
226+
if e.Is(err, utils.ErrUnknownEncoding) {
227+
return nil, e.New("the file name contains unrecognized characters. Please ensure the file name is encoded in UTF-8 or use an English file name")
228+
}
229+
return nil, err
230+
} else {
231+
fullName = string(utf8NameByte)
232+
}
233+
}
222234
if strings.HasSuffix(fullName, ".sql") {
223235
auditFiles = append(auditFiles, model.NewFileRecord(0, execOrder, fullName, model.GenUniqueFileName()))
224236
execOrder++
@@ -323,6 +335,12 @@ func CreateAndAuditTask(c echo.Context) error {
323335

324336
task.ExecMode = req.ExecMode
325337
task.FileOrderMethod = req.FileOrderMethod
338+
339+
err = convertSQLSourceEncodingFromTask(task)
340+
if err != nil {
341+
return controller.JSONBaseErrorReq(c, err)
342+
}
343+
326344
taskGroup := model.TaskGroup{Tasks: []*model.Task{task}}
327345
err = s.Save(&taskGroup)
328346
if err != nil {
@@ -353,6 +371,23 @@ func CreateAndAuditTask(c echo.Context) error {
353371
})
354372
}
355373

374+
func convertSQLSourceEncodingFromTask(task *model.Task) error {
375+
for _, sql := range task.ExecuteSQLs {
376+
if sql.SourceFile == "" {
377+
continue
378+
}
379+
utf8NameByte, err := utils.ConvertToUtf8([]byte(sql.SourceFile))
380+
if err != nil {
381+
if e.Is(err, utils.ErrUnknownEncoding) {
382+
return e.New("the file name contains unrecognized characters. Please ensure the file name is encoded in UTF-8 or use an English file name")
383+
}
384+
return err
385+
}
386+
sql.SourceFile = string(utf8NameByte)
387+
}
388+
return nil
389+
}
390+
356391
// @Summary 获取Sql扫描任务信息
357392
// @Description get task
358393
// @Tags task
@@ -987,6 +1022,13 @@ func AuditTaskGroupV1(c echo.Context) error {
9871022
}
9881023
}
9891024

1025+
for _, task := range taskGroup.Tasks {
1026+
err = convertSQLSourceEncodingFromTask(task)
1027+
if err != nil {
1028+
return controller.JSONBaseErrorReq(c, err)
1029+
}
1030+
}
1031+
9901032
if err := s.Save(taskGroup); err != nil {
9911033
return controller.JSONBaseErrorReq(c, err)
9921034
}

0 commit comments

Comments
 (0)