diff options
author | Unknwon <u@gogs.io> | 2015-12-25 05:25:47 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-12-25 05:25:47 -0500 |
commit | 85af36332b89a42152c7e2b76af5e2aeb048103a (patch) | |
tree | f1eabbd273416ecf34125183c1df994b2b0ca578 /modules/template | |
parent | 13fe73303726d74356a5d94bad7556802c2900a4 (diff) | |
download | gitea-85af36332b89a42152c7e2b76af5e2aeb048103a.tar.gz gitea-85af36332b89a42152c7e2b76af5e2aeb048103a.zip |
#2282 fix utf-8 recognized as windows-1252
Diffstat (limited to 'modules/template')
-rw-r--r-- | modules/template/template.go | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/modules/template/template.go b/modules/template/template.go index 638febe89d..4149de451a 100644 --- a/modules/template/template.go +++ b/modules/template/template.go @@ -12,6 +12,7 @@ import ( "runtime" "strings" "time" + "unicode/utf8" "golang.org/x/net/html/charset" "golang.org/x/text/transform" @@ -130,20 +131,19 @@ func Sha1(str string) string { } func ToUtf8WithErr(content []byte) (error, string) { - charsetLabel := base.DetectEncoding(content) - if charsetLabel == "UTF-8" { + if utf8.Valid(content[:1024]) { return nil, string(content) } + charsetLabel := base.DetectEncoding(content) encoding, _ := charset.Lookup(charsetLabel) if encoding == nil { - return fmt.Errorf("unknown char decoder %s", charsetLabel), string(content) + return fmt.Errorf("Unknown encoding: %s", charsetLabel), string(content) } - result, n, err := transform.String(encoding.NewDecoder(), string(content)) - // If there is an error, we concatenate the nicely decoded part and the // original left over. This way we won't loose data. + result, n, err := transform.String(encoding.NewDecoder(), string(content)) if err != nil { result = result + string(content[n:]) } |