avatar.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // Code from https://github.com/gogits/gogs/blob/v0.7.0/modules/avatar/avatar.go
  5. package avatar
  6. import (
  7. "bufio"
  8. "bytes"
  9. "crypto/md5"
  10. "encoding/hex"
  11. "fmt"
  12. "io"
  13. "io/ioutil"
  14. "net/http"
  15. "net/url"
  16. "path/filepath"
  17. "strconv"
  18. "strings"
  19. "sync"
  20. "time"
  21. "github.com/grafana/grafana/pkg/log"
  22. "github.com/grafana/grafana/pkg/setting"
  23. "gopkg.in/macaron.v1"
  24. gocache "github.com/patrickmn/go-cache"
  25. )
  26. var gravatarSource string
  27. func UpdateGravatarSource() {
  28. srcCfg := "//secure.gravatar.com/avatar/"
  29. gravatarSource = srcCfg
  30. if strings.HasPrefix(gravatarSource, "//") {
  31. gravatarSource = "http:" + gravatarSource
  32. } else if !strings.HasPrefix(gravatarSource, "http://") &&
  33. !strings.HasPrefix(gravatarSource, "https://") {
  34. gravatarSource = "http://" + gravatarSource
  35. }
  36. }
  37. // hash email to md5 string
  38. // keep this func in order to make this package independent
  39. func HashEmail(email string) string {
  40. // https://en.gravatar.com/site/implement/hash/
  41. email = strings.TrimSpace(email)
  42. email = strings.ToLower(email)
  43. h := md5.New()
  44. h.Write([]byte(email))
  45. return hex.EncodeToString(h.Sum(nil))
  46. }
  47. // Avatar represents the avatar object.
  48. type Avatar struct {
  49. hash string
  50. reqParams string
  51. data *bytes.Buffer
  52. notFound bool
  53. timestamp time.Time
  54. }
  55. func New(hash string) *Avatar {
  56. return &Avatar{
  57. hash: hash,
  58. reqParams: url.Values{
  59. "d": {"retro"},
  60. "size": {"200"},
  61. "r": {"pg"}}.Encode(),
  62. }
  63. }
  64. func (this *Avatar) Expired() bool {
  65. return time.Since(this.timestamp) > (time.Minute * 10)
  66. }
  67. func (this *Avatar) Encode(wr io.Writer) error {
  68. _, err := wr.Write(this.data.Bytes())
  69. return err
  70. }
  71. func (this *Avatar) Update() (err error) {
  72. select {
  73. case <-time.After(time.Second * 3):
  74. err = fmt.Errorf("get gravatar image %s timeout", this.hash)
  75. case err = <-thunder.GoFetch(gravatarSource+this.hash+"?"+this.reqParams, this):
  76. }
  77. return err
  78. }
  79. type CacheServer struct {
  80. notFound *Avatar
  81. cache *gocache.Cache
  82. }
  83. func (this *CacheServer) Handler(ctx *macaron.Context) {
  84. urlPath := ctx.Req.URL.Path
  85. hash := urlPath[strings.LastIndex(urlPath, "/")+1:]
  86. var avatar *Avatar
  87. if obj, exist := this.cache.Get(hash); exist {
  88. avatar = obj.(*Avatar)
  89. } else {
  90. avatar = New(hash)
  91. }
  92. if avatar.Expired() {
  93. if err := avatar.Update(); err != nil {
  94. log.Trace("avatar update error: %v", err)
  95. avatar = this.notFound
  96. }
  97. }
  98. if avatar.notFound {
  99. avatar = this.notFound
  100. } else {
  101. this.cache.Add(hash, avatar, gocache.DefaultExpiration)
  102. }
  103. ctx.Resp.Header().Add("Content-Type", "image/jpeg")
  104. if !setting.EnableGzip {
  105. ctx.Resp.Header().Add("Content-Length", strconv.Itoa(len(avatar.data.Bytes())))
  106. }
  107. ctx.Resp.Header().Add("Cache-Control", "private, max-age=3600")
  108. if err := avatar.Encode(ctx.Resp); err != nil {
  109. log.Warn("avatar encode error: %v", err)
  110. ctx.WriteHeader(500)
  111. }
  112. }
  113. func NewCacheServer() *CacheServer {
  114. UpdateGravatarSource()
  115. return &CacheServer{
  116. notFound: newNotFound(),
  117. cache: gocache.New(time.Hour, time.Hour*2),
  118. }
  119. }
  120. func newNotFound() *Avatar {
  121. avatar := &Avatar{notFound: true}
  122. // load user_profile png into buffer
  123. path := filepath.Join(setting.StaticRootPath, "img", "user_profile.png")
  124. if data, err := ioutil.ReadFile(path); err != nil {
  125. log.Error(3, "Failed to read user_profile.png, %v", path)
  126. } else {
  127. avatar.data = bytes.NewBuffer(data)
  128. }
  129. return avatar
  130. }
  131. // thunder downloader
  132. var thunder = &Thunder{QueueSize: 10}
  133. type Thunder struct {
  134. QueueSize int // download queue size
  135. q chan *thunderTask
  136. once sync.Once
  137. }
  138. func (t *Thunder) init() {
  139. if t.QueueSize < 1 {
  140. t.QueueSize = 1
  141. }
  142. t.q = make(chan *thunderTask, t.QueueSize)
  143. for i := 0; i < t.QueueSize; i++ {
  144. go func() {
  145. for {
  146. task := <-t.q
  147. task.Fetch()
  148. }
  149. }()
  150. }
  151. }
  152. func (t *Thunder) Fetch(url string, avatar *Avatar) error {
  153. t.once.Do(t.init)
  154. task := &thunderTask{
  155. Url: url,
  156. Avatar: avatar,
  157. }
  158. task.Add(1)
  159. t.q <- task
  160. task.Wait()
  161. return task.err
  162. }
  163. func (t *Thunder) GoFetch(url string, avatar *Avatar) chan error {
  164. c := make(chan error)
  165. go func() {
  166. c <- t.Fetch(url, avatar)
  167. }()
  168. return c
  169. }
  170. // thunder download
  171. type thunderTask struct {
  172. Url string
  173. Avatar *Avatar
  174. sync.WaitGroup
  175. err error
  176. }
  177. func (this *thunderTask) Fetch() {
  178. this.err = this.fetch()
  179. this.Done()
  180. }
  181. var client = &http.Client{
  182. Timeout: time.Second * 2,
  183. Transport: &http.Transport{Proxy: http.ProxyFromEnvironment},
  184. }
  185. func (this *thunderTask) fetch() error {
  186. this.Avatar.timestamp = time.Now()
  187. log.Debug("avatar.fetch(fetch new avatar): %s", this.Url)
  188. req, _ := http.NewRequest("GET", this.Url, nil)
  189. req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/jpeg,image/png,*/*;q=0.8")
  190. req.Header.Set("Accept-Encoding", "deflate,sdch")
  191. req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
  192. req.Header.Set("Cache-Control", "no-cache")
  193. 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")
  194. resp, err := client.Do(req)
  195. if err != nil {
  196. this.Avatar.notFound = true
  197. return fmt.Errorf("gravatar unreachable, %v", err)
  198. }
  199. defer resp.Body.Close()
  200. if resp.StatusCode != 200 {
  201. this.Avatar.notFound = true
  202. return fmt.Errorf("status code: %d", resp.StatusCode)
  203. }
  204. this.Avatar.data = &bytes.Buffer{}
  205. writer := bufio.NewWriter(this.Avatar.data)
  206. _, err = io.Copy(writer, resp.Body)
  207. return err
  208. }