Skip to content

Commit

Permalink
Merge pull request #2456 from actiontech/fix-issue2452
Browse files Browse the repository at this point in the history
convert gbk encoding
  • Loading branch information
LordofAvernus committed Jun 14, 2024
2 parents ba52dd1 + fef181f commit 187e7ff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sqle/api/controller/v1/sql_audit_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func CreateSQLAuditRecord(c echo.Context) error {
// if task instance is not nil, gorm will update instance when save task.
task.Instance = nil

err = convertSQLSourceEncodingFromTask(task)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}

recordId, err := utils.GenUid()
if err != nil {
return controller.JSONBaseErrorReq(c, fmt.Errorf("generate audit record id failed: %v", err))
Expand Down
42 changes: 42 additions & 0 deletions sqle/api/controller/v1/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"encoding/csv"
e "errors"
"fmt"
"mime"
"mime/multipart"
Expand Down Expand Up @@ -219,6 +220,17 @@ func getFileRecordsFromZip(multipartFile multipart.File, fileHeader *multipart.F
continue
}
fullName := srcFile.FileHeader.Name // full name with relative path to zip file
if srcFile.NonUTF8 {
utf8NameByte, err := utils.ConvertToUtf8([]byte(fullName))
if err != nil {
if e.Is(err, utils.ErrUnknownEncoding) {
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")
}
return nil, err
} else {
fullName = string(utf8NameByte)
}
}
if strings.HasSuffix(fullName, ".sql") {
auditFiles = append(auditFiles, model.NewFileRecord(0, execOrder, fullName, model.GenUniqueFileName()))
execOrder++
Expand Down Expand Up @@ -323,6 +335,12 @@ func CreateAndAuditTask(c echo.Context) error {

task.ExecMode = req.ExecMode
task.FileOrderMethod = req.FileOrderMethod

err = convertSQLSourceEncodingFromTask(task)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}

taskGroup := model.TaskGroup{Tasks: []*model.Task{task}}
err = s.Save(&taskGroup)
if err != nil {
Expand Down Expand Up @@ -353,6 +371,23 @@ func CreateAndAuditTask(c echo.Context) error {
})
}

func convertSQLSourceEncodingFromTask(task *model.Task) error {
for _, sql := range task.ExecuteSQLs {
if sql.SourceFile == "" {
continue
}
utf8NameByte, err := utils.ConvertToUtf8([]byte(sql.SourceFile))
if err != nil {
if e.Is(err, utils.ErrUnknownEncoding) {
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")
}
return err
}
sql.SourceFile = string(utf8NameByte)
}
return nil
}

// @Summary 获取Sql扫描任务信息
// @Description get task
// @Tags task
Expand Down Expand Up @@ -987,6 +1022,13 @@ func AuditTaskGroupV1(c echo.Context) error {
}
}

for _, task := range taskGroup.Tasks {
err = convertSQLSourceEncodingFromTask(task)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
}

if err := s.Save(taskGroup); err != nil {
return controller.JSONBaseErrorReq(c, err)
}
Expand Down

0 comments on commit 187e7ff

Please sign in to comment.