avatar.go 5.6 KB

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