From 95f9c85bcc8f915d823f57cd5923302dfc156504 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 13 Dec 2015 19:56:33 -0500 Subject: #2185 use Go sub-repo to detect encoding --- modules/base/tool.go | 13 ++++--------- modules/template/template.go | 6 +----- 2 files changed, 5 insertions(+), 14 deletions(-) (limited to 'modules') diff --git a/modules/base/tool.go b/modules/base/tool.go index b1d94511..46eeca9b 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -22,8 +22,7 @@ import ( "github.com/Unknwon/com" "github.com/Unknwon/i18n" "github.com/microcosm-cc/bluemonday" - - "github.com/gogits/chardet" + "golang.org/x/net/html/charset" "github.com/gogits/gogs/modules/avatar" "github.com/gogits/gogs/modules/setting" @@ -52,13 +51,9 @@ func ShortSha(sha1 string) string { return sha1 } -func DetectEncoding(content []byte) (string, error) { - detector := chardet.NewTextDetector() - result, err := detector.DetectBest(content) - if result.Charset != "UTF-8" && len(setting.Repository.AnsiCharset) > 0 { - return setting.Repository.AnsiCharset, err - } - return result.Charset, err +func DetectEncoding(content []byte) string { + _, name, _ := charset.DetermineEncoding(content, setting.Repository.AnsiCharset) + return name } func BasicAuthDecode(encoded string) (string, string, error) { diff --git a/modules/template/template.go b/modules/template/template.go index 9d63452d..cc670a65 100644 --- a/modules/template/template.go +++ b/modules/template/template.go @@ -127,11 +127,7 @@ func Sha1(str string) string { } func ToUtf8WithErr(content []byte) (error, string) { - charsetLabel, err := base.DetectEncoding(content) - if err != nil { - return err, "" - } - + charsetLabel := base.DetectEncoding(content) if charsetLabel == "UTF-8" { return nil, string(content) } -- cgit v1.2.3