mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 15:32:27 +08:00

This commit fixes the following golint warnings: pkg/api/avatar/avatar.go:229:12: should omit type *http.Client from declaration of var client; it will be inferred from the right-hand side pkg/login/brute_force_login_protection.go:13:26: should omit type time.Duration from declaration of var loginAttemptsWindow; it will be inferred from the right-hand side pkg/metrics/graphitebridge/graphite.go:58:26: should omit type []string from declaration of var metricCategoryPrefix; it will be inferred from the right-hand side pkg/metrics/graphitebridge/graphite.go:69:22: should omit type []string from declaration of var trimMetricPrefix; it will be inferred from the right-hand side pkg/models/alert.go:37:36: should omit type error from declaration of var ErrCannotChangeStateOnPausedAlert; it will be inferred from the right-hand side pkg/models/alert.go:38:36: should omit type error from declaration of var ErrRequiresNewState; it will be inferred from the right-hand side pkg/models/datasource.go:61:28: should omit type map[string]bool from declaration of var knownDatasourcePlugins; it will be inferred from the right-hand side pkg/plugins/update_checker.go:16:13: should omit type http.Client from declaration of var httpClient; it will be inferred from the right-hand side pkg/services/alerting/engine.go:103:24: should omit type time.Duration from declaration of var unfinishedWorkTimeout; it will be inferred from the right-hand side pkg/services/alerting/engine.go:105:19: should omit type time.Duration from declaration of var alertTimeout; it will be inferred from the right-hand side pkg/services/alerting/engine.go:106:19: should omit type int from declaration of var alertMaxAttempts; it will be inferred from the right-hand side pkg/services/alerting/notifier.go:143:23: should omit type map[string]*NotifierPlugin from declaration of var notifierFactories; it will be inferred from the right-hand side pkg/services/alerting/rule.go:136:24: should omit type map[string]ConditionFactory from declaration of var conditionFactories; it will be inferred from the right-hand side pkg/services/alerting/conditions/evaluator.go:12:15: should omit type []string from declaration of var defaultTypes; it will be inferred from the right-hand side pkg/services/alerting/conditions/evaluator.go:13:15: should omit type []string from declaration of var rangedTypes; it will be inferred from the right-hand side pkg/services/alerting/notifiers/opsgenie.go:44:19: should omit type string from declaration of var opsgenieAlertURL; it will be inferred from the right-hand side pkg/services/alerting/notifiers/pagerduty.go:43:23: should omit type string from declaration of var pagerdutyEventApiUrl; it will be inferred from the right-hand side pkg/services/alerting/notifiers/telegram.go:21:17: should omit type string from declaration of var telegramApiUrl; it will be inferred from the right-hand side pkg/services/provisioning/dashboards/config_reader_test.go:11:24: should omit type string from declaration of var simpleDashboardConfig; it will be inferred from the right-hand side pkg/services/provisioning/dashboards/config_reader_test.go:12:24: should omit type string from declaration of var oldVersion; it will be inferred from the right-hand side pkg/services/provisioning/dashboards/config_reader_test.go:13:24: should omit type string from declaration of var brokenConfigs; it will be inferred from the right-hand side pkg/services/provisioning/dashboards/file_reader.go:22:30: should omit type time.Duration from declaration of var checkDiskForChangesInterval; it will be inferred from the right-hand side pkg/services/provisioning/dashboards/file_reader.go:24:23: should omit type error from declaration of var ErrFolderNameMissing; it will be inferred from the right-hand side pkg/services/provisioning/datasources/config_reader_test.go:15:34: should omit type string from declaration of var twoDatasourcesConfig; it will be inferred from the right-hand side pkg/services/provisioning/datasources/config_reader_test.go:16:34: should omit type string from declaration of var twoDatasourcesConfigPurgeOthers; it will be inferred from the right-hand side pkg/services/provisioning/datasources/config_reader_test.go:17:34: should omit type string from declaration of var doubleDatasourcesConfig; it will be inferred from the right-hand side pkg/services/provisioning/datasources/config_reader_test.go:18:34: should omit type string from declaration of var allProperties; it will be inferred from the right-hand side pkg/services/provisioning/datasources/config_reader_test.go:19:34: should omit type string from declaration of var versionZero; it will be inferred from the right-hand side pkg/services/provisioning/datasources/config_reader_test.go:20:34: should omit type string from declaration of var brokenYaml; it will be inferred from the right-hand side pkg/services/sqlstore/stats.go:16:25: should omit type time.Duration from declaration of var activeUserTimeLimit; it will be inferred from the right-hand side pkg/services/sqlstore/migrator/mysql_dialect.go:69:14: should omit type bool from declaration of var hasLen1; it will be inferred from the right-hand side pkg/services/sqlstore/migrator/mysql_dialect.go:70:14: should omit type bool from declaration of var hasLen2; it will be inferred from the right-hand side pkg/services/sqlstore/migrator/postgres_dialect.go:95:14: should omit type bool from declaration of var hasLen1; it will be inferred from the right-hand side pkg/services/sqlstore/migrator/postgres_dialect.go:96:14: should omit type bool from declaration of var hasLen2; it will be inferred from the right-hand side pkg/setting/setting.go:42:15: should omit type string from declaration of var Env; it will be inferred from the right-hand side pkg/setting/setting.go:161:18: should omit type bool from declaration of var LdapAllowSignup; it will be inferred from the right-hand side pkg/setting/setting.go:473:30: should omit type bool from declaration of var skipStaticRootValidation; it will be inferred from the right-hand side pkg/tsdb/interval.go:14:21: should omit type time.Duration from declaration of var defaultMinInterval; it will be inferred from the right-hand side pkg/tsdb/interval.go:15:21: should omit type time.Duration from declaration of var year; it will be inferred from the right-hand side pkg/tsdb/interval.go:16:21: should omit type time.Duration from declaration of var day; it will be inferred from the right-hand side pkg/tsdb/cloudwatch/credentials.go:26:24: should omit type map[string]cache from declaration of var awsCredentialCache; it will be inferred from the right-hand side pkg/tsdb/influxdb/query.go:15:27: should omit type *regexp.Regexp from declaration of var regexpOperatorPattern; it will be inferred from the right-hand side pkg/tsdb/influxdb/query.go:16:27: should omit type *regexp.Regexp from declaration of var regexpMeasurementPattern; it will be inferred from the right-hand side pkg/tsdb/mssql/mssql_test.go:25:14: should omit type string from declaration of var serverIP; it will be inferred from the right-hand side
264 lines
5.7 KiB
Go
264 lines
5.7 KiB
Go
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Code from https://github.com/gogits/gogs/blob/v0.7.0/modules/avatar/avatar.go
|
|
|
|
package avatar
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
"gopkg.in/macaron.v1"
|
|
|
|
gocache "github.com/patrickmn/go-cache"
|
|
)
|
|
|
|
var gravatarSource string
|
|
|
|
func UpdateGravatarSource() {
|
|
srcCfg := "//secure.gravatar.com/avatar/"
|
|
|
|
gravatarSource = srcCfg
|
|
if strings.HasPrefix(gravatarSource, "//") {
|
|
gravatarSource = "http:" + gravatarSource
|
|
} else if !strings.HasPrefix(gravatarSource, "http://") &&
|
|
!strings.HasPrefix(gravatarSource, "https://") {
|
|
gravatarSource = "http://" + gravatarSource
|
|
}
|
|
}
|
|
|
|
// hash email to md5 string
|
|
// keep this func in order to make this package independent
|
|
func HashEmail(email string) string {
|
|
// https://en.gravatar.com/site/implement/hash/
|
|
email = strings.TrimSpace(email)
|
|
email = strings.ToLower(email)
|
|
|
|
h := md5.New()
|
|
h.Write([]byte(email))
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
// Avatar represents the avatar object.
|
|
type Avatar struct {
|
|
hash string
|
|
reqParams string
|
|
data *bytes.Buffer
|
|
notFound bool
|
|
timestamp time.Time
|
|
}
|
|
|
|
func New(hash string) *Avatar {
|
|
return &Avatar{
|
|
hash: hash,
|
|
reqParams: url.Values{
|
|
"d": {"retro"},
|
|
"size": {"200"},
|
|
"r": {"pg"}}.Encode(),
|
|
}
|
|
}
|
|
|
|
func (this *Avatar) Expired() bool {
|
|
return time.Since(this.timestamp) > (time.Minute * 10)
|
|
}
|
|
|
|
func (this *Avatar) Encode(wr io.Writer) error {
|
|
_, err := wr.Write(this.data.Bytes())
|
|
return err
|
|
}
|
|
|
|
func (this *Avatar) Update() (err error) {
|
|
select {
|
|
case <-time.After(time.Second * 3):
|
|
err = fmt.Errorf("get gravatar image %s timeout", this.hash)
|
|
case err = <-thunder.GoFetch(gravatarSource+this.hash+"?"+this.reqParams, this):
|
|
}
|
|
return err
|
|
}
|
|
|
|
type CacheServer struct {
|
|
notFound *Avatar
|
|
cache *gocache.Cache
|
|
}
|
|
|
|
func (this *CacheServer) mustInt(r *http.Request, defaultValue int, keys ...string) (v int) {
|
|
for _, k := range keys {
|
|
if _, err := fmt.Sscanf(r.FormValue(k), "%d", &v); err == nil {
|
|
defaultValue = v
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func (this *CacheServer) Handler(ctx *macaron.Context) {
|
|
urlPath := ctx.Req.URL.Path
|
|
hash := urlPath[strings.LastIndex(urlPath, "/")+1:]
|
|
|
|
var avatar *Avatar
|
|
|
|
if obj, exist := this.cache.Get(hash); exist {
|
|
avatar = obj.(*Avatar)
|
|
} else {
|
|
avatar = New(hash)
|
|
}
|
|
|
|
if avatar.Expired() {
|
|
if err := avatar.Update(); err != nil {
|
|
log.Trace("avatar update error: %v", err)
|
|
avatar = this.notFound
|
|
}
|
|
}
|
|
|
|
if avatar.notFound {
|
|
avatar = this.notFound
|
|
} else {
|
|
this.cache.Add(hash, avatar, gocache.DefaultExpiration)
|
|
}
|
|
|
|
ctx.Resp.Header().Add("Content-Type", "image/jpeg")
|
|
|
|
if !setting.EnableGzip {
|
|
ctx.Resp.Header().Add("Content-Length", strconv.Itoa(len(avatar.data.Bytes())))
|
|
}
|
|
|
|
ctx.Resp.Header().Add("Cache-Control", "private, max-age=3600")
|
|
|
|
if err := avatar.Encode(ctx.Resp); err != nil {
|
|
log.Warn("avatar encode error: %v", err)
|
|
ctx.WriteHeader(500)
|
|
}
|
|
}
|
|
|
|
func NewCacheServer() *CacheServer {
|
|
UpdateGravatarSource()
|
|
|
|
return &CacheServer{
|
|
notFound: newNotFound(),
|
|
cache: gocache.New(time.Hour, time.Hour*2),
|
|
}
|
|
}
|
|
|
|
func newNotFound() *Avatar {
|
|
avatar := &Avatar{notFound: true}
|
|
|
|
// load user_profile png into buffer
|
|
path := filepath.Join(setting.StaticRootPath, "img", "user_profile.png")
|
|
|
|
if data, err := ioutil.ReadFile(path); err != nil {
|
|
log.Error(3, "Failed to read user_profile.png, %v", path)
|
|
} else {
|
|
avatar.data = bytes.NewBuffer(data)
|
|
}
|
|
|
|
return avatar
|
|
}
|
|
|
|
// thunder downloader
|
|
var thunder = &Thunder{QueueSize: 10}
|
|
|
|
type Thunder struct {
|
|
QueueSize int // download queue size
|
|
q chan *thunderTask
|
|
once sync.Once
|
|
}
|
|
|
|
func (t *Thunder) init() {
|
|
if t.QueueSize < 1 {
|
|
t.QueueSize = 1
|
|
}
|
|
t.q = make(chan *thunderTask, t.QueueSize)
|
|
for i := 0; i < t.QueueSize; i++ {
|
|
go func() {
|
|
for {
|
|
task := <-t.q
|
|
task.Fetch()
|
|
}
|
|
}()
|
|
}
|
|
}
|
|
|
|
func (t *Thunder) Fetch(url string, avatar *Avatar) error {
|
|
t.once.Do(t.init)
|
|
task := &thunderTask{
|
|
Url: url,
|
|
Avatar: avatar,
|
|
}
|
|
task.Add(1)
|
|
t.q <- task
|
|
task.Wait()
|
|
return task.err
|
|
}
|
|
|
|
func (t *Thunder) GoFetch(url string, avatar *Avatar) chan error {
|
|
c := make(chan error)
|
|
go func() {
|
|
c <- t.Fetch(url, avatar)
|
|
}()
|
|
return c
|
|
}
|
|
|
|
// thunder download
|
|
type thunderTask struct {
|
|
Url string
|
|
Avatar *Avatar
|
|
sync.WaitGroup
|
|
err error
|
|
}
|
|
|
|
func (this *thunderTask) Fetch() {
|
|
this.err = this.fetch()
|
|
this.Done()
|
|
}
|
|
|
|
var client = &http.Client{
|
|
Timeout: time.Second * 2,
|
|
Transport: &http.Transport{Proxy: http.ProxyFromEnvironment},
|
|
}
|
|
|
|
func (this *thunderTask) fetch() error {
|
|
this.Avatar.timestamp = time.Now()
|
|
|
|
log.Debug("avatar.fetch(fetch new avatar): %s", this.Url)
|
|
req, _ := http.NewRequest("GET", this.Url, nil)
|
|
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/jpeg,image/png,*/*;q=0.8")
|
|
req.Header.Set("Accept-Encoding", "deflate,sdch")
|
|
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
|
|
req.Header.Set("Cache-Control", "no-cache")
|
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36")
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
this.Avatar.notFound = true
|
|
return fmt.Errorf("gravatar unreachable, %v", err)
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
this.Avatar.notFound = true
|
|
return fmt.Errorf("status code: %d", resp.StatusCode)
|
|
}
|
|
|
|
this.Avatar.data = &bytes.Buffer{}
|
|
writer := bufio.NewWriter(this.Avatar.data)
|
|
|
|
_, err = io.Copy(writer, resp.Body)
|
|
return err
|
|
}
|