mirror of https://github.com/Xhofe/alist
🐛 fix gbk garbled
parent
82272fcbf5
commit
da74e29b26
|
@ -99,9 +99,18 @@ func init() {
|
|||
|
||||
func Text(c *gin.Context, link string) {
|
||||
res, err := client.R().Get(link)
|
||||
text := res.String()
|
||||
if utils.IsGBK(res.Body()) {
|
||||
body, err := utils.GbkToUtf8(res.Body())
|
||||
if err != nil {
|
||||
ErrorResp(c,err,500)
|
||||
return
|
||||
}
|
||||
c.String(200,res.String())
|
||||
text = string(body)
|
||||
}
|
||||
if err != nil {
|
||||
ErrorResp(c,err,500)
|
||||
return
|
||||
}
|
||||
c.String(200,text)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
"golang.org/x/text/transform"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -88,3 +91,38 @@ func ParsePath(path string) string {
|
|||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func IsGBK(data []byte) bool {
|
||||
length := len(data)
|
||||
var i int = 0
|
||||
for i < length {
|
||||
//fmt.Printf("for %x\n", data[i])
|
||||
if data[i] <= 0xff {
|
||||
//编码小于等于127,只有一个字节的编码,兼容ASCII吗
|
||||
i++
|
||||
continue
|
||||
} else {
|
||||
//大于127的使用双字节编码
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue