avatar.go 5.5 KB

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