avatar.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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) mustInt(r *http.Request, defaultValue int, keys ...string) (v int) {
  84. for _, k := range keys {
  85. if _, err := fmt.Sscanf(r.FormValue(k), "%d", &v); err == nil {
  86. defaultValue = v
  87. }
  88. }
  89. return defaultValue
  90. }
  91. func (this *CacheServer) Handler(ctx *macaron.Context) {
  92. urlPath := ctx.Req.URL.Path
  93. hash := urlPath[strings.LastIndex(urlPath, "/")+1:]
  94. var avatar *Avatar
  95. if obj, exist := this.cache.Get(hash); exist {
  96. avatar = obj.(*Avatar)
  97. } else {
  98. avatar = New(hash)
  99. }
  100. if avatar.Expired() {
  101. if err := avatar.Update(); err != nil {
  102. log.Trace("avatar update error: %v", err)
  103. avatar = this.notFound
  104. }
  105. }
  106. if avatar.notFound {
  107. avatar = this.notFound
  108. } else {
  109. this.cache.Add(hash, avatar, gocache.DefaultExpiration)
  110. }
  111. ctx.Resp.Header().Add("Content-Type", "image/jpeg")
  112. if !setting.EnableGzip {
  113. ctx.Resp.Header().Add("Content-Length", strconv.Itoa(len(avatar.data.Bytes())))
  114. }
  115. ctx.Resp.Header().Add("Cache-Control", "private, max-age=3600")
  116. if err := avatar.Encode(ctx.Resp); err != nil {
  117. log.Warn("avatar encode error: %v", err)
  118. ctx.WriteHeader(500)
  119. }
  120. }
  121. func NewCacheServer() *CacheServer {
  122. UpdateGravatarSource()
  123. return &CacheServer{
  124. notFound: newNotFound(),
  125. cache: gocache.New(time.Hour, time.Hour*2),
  126. }
  127. }
  128. func newNotFound() *Avatar {
  129. avatar := &Avatar{notFound: true}
  130. // load transparent png into buffer
  131. path := filepath.Join(setting.StaticRootPath, "img", "transparent.png")
  132. if data, err := ioutil.ReadFile(path); err != nil {
  133. log.Error(3, "Failed to read transparent.png, %v", path)
  134. } else {
  135. avatar.data = bytes.NewBuffer(data)
  136. }
  137. return avatar
  138. }
  139. // thunder downloader
  140. var thunder = &Thunder{QueueSize: 10}
  141. type Thunder struct {
  142. QueueSize int // download queue size
  143. q chan *thunderTask
  144. once sync.Once
  145. }
  146. func (t *Thunder) init() {
  147. if t.QueueSize < 1 {
  148. t.QueueSize = 1
  149. }
  150. t.q = make(chan *thunderTask, t.QueueSize)
  151. for i := 0; i < t.QueueSize; i++ {
  152. go func() {
  153. for {
  154. task := <-t.q
  155. task.Fetch()
  156. }
  157. }()
  158. }
  159. }
  160. func (t *Thunder) Fetch(url string, avatar *Avatar) error {
  161. t.once.Do(t.init)
  162. task := &thunderTask{
  163. Url: url,
  164. Avatar: avatar,
  165. }
  166. task.Add(1)
  167. t.q <- task
  168. task.Wait()
  169. return task.err
  170. }
  171. func (t *Thunder) GoFetch(url string, avatar *Avatar) chan error {
  172. c := make(chan error)
  173. go func() {
  174. c <- t.Fetch(url, avatar)
  175. }()
  176. return c
  177. }
  178. // thunder download
  179. type thunderTask struct {
  180. Url string
  181. Avatar *Avatar
  182. sync.WaitGroup
  183. err error
  184. }
  185. func (this *thunderTask) Fetch() {
  186. this.err = this.fetch()
  187. this.Done()
  188. }
  189. var client *http.Client = &http.Client{
  190. Timeout: time.Second * 2,
  191. Transport: &http.Transport{Proxy: http.ProxyFromEnvironment},
  192. }
  193. func (this *thunderTask) fetch() error {
  194. this.Avatar.timestamp = time.Now()
  195. log.Debug("avatar.fetch(fetch new avatar): %s", this.Url)
  196. req, _ := http.NewRequest("GET", this.Url, nil)
  197. req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/jpeg,image/png,*/*;q=0.8")
  198. req.Header.Set("Accept-Encoding", "deflate,sdch")
  199. req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
  200. req.Header.Set("Cache-Control", "no-cache")
  201. 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")
  202. resp, err := client.Do(req)
  203. if err != nil {
  204. this.Avatar.notFound = true
  205. return fmt.Errorf("gravatar unreachable, %v", err)
  206. }
  207. defer resp.Body.Close()
  208. if resp.StatusCode != 200 {
  209. this.Avatar.notFound = true
  210. return fmt.Errorf("status code: %d", resp.StatusCode)
  211. }
  212. this.Avatar.data = &bytes.Buffer{}
  213. writer := bufio.NewWriter(this.Avatar.data)
  214. if _, err = io.Copy(writer, resp.Body); err != nil {
  215. return err
  216. }
  217. return nil
  218. }