Browse Source

golint fixed for modules/base

tags/v1.0.0
Lunny Xiao 7 years ago
parent
commit
fb3bb69ec6
2 changed files with 16 additions and 7 deletions
  1. 2
    0
      modules/base/base.go
  2. 14
    7
      modules/base/tool.go

+ 2
- 0
modules/base/base.go View File



package base package base


// DocURL api doc url
const DocURL = "https://godoc.org/github.com/go-gitea/go-sdk/gitea" const DocURL = "https://godoc.org/github.com/go-gitea/go-sdk/gitea"


type ( type (
// TplName template relative path type
TplName string TplName string
) )

+ 14
- 7
modules/base/tool.go View File

return hex.EncodeToString(m.Sum(nil)) return hex.EncodeToString(m.Sum(nil))
} }


// Encode string to sha1 hex value.
// EncodeSha1 string to sha1 hex value.
func EncodeSha1(str string) string { func EncodeSha1(str string) string {
h := sha1.New() h := sha1.New()
h.Write([]byte(str)) h.Write([]byte(str))
return TruncateString(sha1, 10) return TruncateString(sha1, 10)
} }


// DetectEncoding detect the encoding of content
func DetectEncoding(content []byte) (string, error) { func DetectEncoding(content []byte) (string, error) {
if utf8.Valid(content) { if utf8.Valid(content) {
log.Debug("Detected encoding: utf-8 (fast)") log.Debug("Detected encoding: utf-8 (fast)")
return result.Charset, err return result.Charset, err
} }


// BasicAuthDecode decode basic auth string
func BasicAuthDecode(encoded string) (string, string, error) { func BasicAuthDecode(encoded string) (string, string, error) {
s, err := base64.StdEncoding.DecodeString(encoded) s, err := base64.StdEncoding.DecodeString(encoded)
if err != nil { if err != nil {
return auth[0], auth[1], nil return auth[0], auth[1], nil
} }


// BasicAuthEncode encode basic auth string
func BasicAuthEncode(username, password string) string { func BasicAuthEncode(username, password string) string {
return base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
} }
return string(bytes) return string(bytes)
} }


// http://code.google.com/p/go/source/browse/pbkdf2/pbkdf2.go?repo=crypto
// PBKDF2 http://code.google.com/p/go/source/browse/pbkdf2/pbkdf2.go?repo=crypto
// FIXME: use https://godoc.org/golang.org/x/crypto/pbkdf2? // FIXME: use https://godoc.org/golang.org/x/crypto/pbkdf2?
func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte { func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte {
prf := hmac.New(h, password) prf := hmac.New(h, password)
return dk[:keyLen] return dk[:keyLen]
} }


// verify time limit code
// VerifyTimeLimitCode verify time limit code
func VerifyTimeLimitCode(data string, minutes int, code string) bool { func VerifyTimeLimitCode(data string, minutes int, code string) bool {
if len(code) <= 18 { if len(code) <= 18 {
return false return false
return false return false
} }


// TimeLimitCodeLength default value for time limit code
const TimeLimitCodeLength = 12 + 6 + 40 const TimeLimitCodeLength = 12 + 6 + 40


// create a time limit code
// CreateTimeLimitCode create a time limit code
// code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string // code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string
func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string { func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string {
format := "200601021504" format := "200601021504"
} }
} }


// RawTimeSince retrieves i18n key of time since t
func RawTimeSince(t time.Time, lang string) string { func RawTimeSince(t time.Time, lang string) string {
return timeSince(t, lang) return timeSince(t, lang)
} }
return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`, t.Format(setting.TimeFormat), timeSince(t, lang))) return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`, t.Format(setting.TimeFormat), timeSince(t, lang)))
} }


// Storage space size types
const ( const (
Byte = 1 Byte = 1
KByte = Byte * 1024 KByte = Byte * 1024
func Subtract(left interface{}, right interface{}) interface{} { func Subtract(left interface{}, right interface{}) interface{} {
var rleft, rright int64 var rleft, rright int64
var fleft, fright float64 var fleft, fright float64
var isInt bool = true
var isInt = true
switch left.(type) { switch left.(type) {
case int: case int:
rleft = int64(left.(int)) rleft = int64(left.(int))


if isInt { if isInt {
return rleft - rright return rleft - rright
} else {
return fleft + float64(rleft) - (fright + float64(rright))
} }
return fleft + float64(rleft) - (fright + float64(rright))
} }


// EllipsisString returns a truncated short string, // EllipsisString returns a truncated short string,
return strings.Index(http.DetectContentType(data), "text/") != -1 return strings.Index(http.DetectContentType(data), "text/") != -1
} }


// IsImageFile detectes if data is an image format
func IsImageFile(data []byte) bool { func IsImageFile(data []byte) bool {
return strings.Index(http.DetectContentType(data), "image/") != -1 return strings.Index(http.DetectContentType(data), "image/") != -1
} }


// IsPDFFile detectes if data is a pdf format
func IsPDFFile(data []byte) bool { func IsPDFFile(data []byte) bool {
return strings.Index(http.DetectContentType(data), "application/pdf") != -1 return strings.Index(http.DetectContentType(data), "application/pdf") != -1
} }

Loading…
Cancel
Save