Browse Source

fix: gbk to utf8 if output bytes is gbk (#45)

jeessy2 3 years ago
parent
commit
03d391f08f
3 changed files with 47 additions and 0 deletions
  1. 3 0
      client/backup.go
  2. 1 0
      go.mod
  3. 43 0
      util/gbk_util.go

+ 3 - 0
client/backup.go

@@ -187,6 +187,9 @@ func backup(backupConf entity.BackupConfig, encryptKey string, s3Conf entity.S3C
 	shell.Dir = backupConf.GetProjectPath()
 	outputBytes, err := shell.CombinedOutput()
 	if len(outputBytes) > 0 {
+		if util.IsGBK(outputBytes) {
+			outputBytes, _ = util.GbkToUtf8(outputBytes)
+		}
 		log.Printf("<span title=\"%s\">%s 执行shell的输出: 鼠标移动此处查看</span>\n", util.EscapeShell(string(outputBytes)), backupConf.ProjectName)
 	} else {
 		log.Printf("执行shell的输出为空\n")

+ 1 - 0
go.mod

@@ -11,4 +11,5 @@ require (
 require (
 	github.com/jmespath/go-jmespath v0.4.0 // indirect
 	golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e // indirect
+	golang.org/x/text v0.3.7
 )

+ 43 - 0
util/gbk_util.go

@@ -0,0 +1,43 @@
+package util
+
+import (
+	"bytes"
+	"io/ioutil"
+
+	"golang.org/x/text/encoding/simplifiedchinese"
+	"golang.org/x/text/transform"
+)
+
+func IsGBK(data []byte) bool {
+	length := len(data)
+	var i int = 0
+	for i < length {
+		if data[i] <= 0x7f {
+			//编码0~127,只有一个字节的编码,兼容ASCII码
+			i++
+			continue
+		} else {
+			//大于127的使用双字节编码,落在gbk编码范围内的字符
+			if data[i] >= 0x81 &&
+				data[i] <= 0xfe &&
+				data[i+1] >= 0x40 &&
+				data[i+1] <= 0xfe &&
+				data[i+1] != 0xf7 {
+				i += 2
+				continue
+			} else {
+				return false
+			}
+		}
+	}
+	return true
+}
+
+func GbkToUtf8(s []byte) ([]byte, error) {
+	reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
+	d, e := ioutil.ReadAll(reader)
+	if e != nil {
+		return nil, e
+	}
+	return d, nil
+}