summaryrefslogtreecommitdiffstats
path: root/modules/base/tool.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/base/tool.go')
-rw-r--r--modules/base/tool.go18
1 files changed, 18 insertions, 0 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go
index b9a97c9c3d..78983b3611 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -23,6 +23,8 @@ import (
"github.com/Unknwon/i18n"
"github.com/microcosm-cc/bluemonday"
+ "github.com/gogits/chardet"
+
"github.com/gogits/gogs/modules/avatar"
"github.com/gogits/gogs/modules/setting"
)
@@ -43,6 +45,22 @@ func EncodeSha1(str string) string {
return hex.EncodeToString(h.Sum(nil))
}
+func ShortSha(sha1 string) string {
+ if len(sha1) == 40 {
+ return sha1[:10]
+ }
+ 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 BasicAuthDecode(encoded string) (string, string, error) {
s, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {