avatar.go 5.4 KB

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