diff --git a/sqle/api/controller/v1/sql_audit_record.go b/sqle/api/controller/v1/sql_audit_record.go index 844156168e..1295513a82 100644 --- a/sqle/api/controller/v1/sql_audit_record.go +++ b/sqle/api/controller/v1/sql_audit_record.go @@ -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)) diff --git a/sqle/api/controller/v1/task.go b/sqle/api/controller/v1/task.go index 4bc1148e9a..6c603982c5 100644 --- a/sqle/api/controller/v1/task.go +++ b/sqle/api/controller/v1/task.go @@ -5,6 +5,7 @@ import ( "bytes" "context" "encoding/csv" + e "errors" "fmt" "mime" "mime/multipart" @@ -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++ @@ -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 { @@ -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 @@ -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) }