avatar.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // for www.gravatar.com image cache
  5. /*
  6. It is recommend to use this way
  7. cacheDir := "./cache"
  8. defaultImg := "./default.jpg"
  9. http.Handle("/avatar/", avatar.CacheServer(cacheDir, defaultImg))
  10. */
  11. package avatar
  12. import (
  13. "crypto/md5"
  14. "encoding/hex"
  15. "errors"
  16. "fmt"
  17. "image"
  18. "image/color/palette"
  19. "image/jpeg"
  20. "image/png"
  21. "io"
  22. "math/rand"
  23. "net/http"
  24. "net/url"
  25. "os"
  26. "path/filepath"
  27. "strings"
  28. "sync"
  29. "time"
  30. "github.com/nfnt/resize"
  31. "github.com/gogits/gogs/modules/identicon"
  32. "github.com/gogits/gogs/modules/log"
  33. "github.com/gogits/gogs/modules/setting"
  34. )
  35. var gravatarSource string
  36. func UpdateGravatarSource() {
  37. gravatarSource = setting.GravatarSource
  38. log.Debug("avatar.UpdateGravatarSource(gavatar source): %s", gravatarSource)
  39. if !strings.HasPrefix(gravatarSource, "http://") ||
  40. !strings.HasPrefix(gravatarSource, "https://") {
  41. gravatarSource = "http://" + gravatarSource
  42. log.Debug("avatar.UpdateGravatarSource(update gavatar source): %s", gravatarSource)
  43. }
  44. }
  45. // hash email to md5 string
  46. // keep this func in order to make this package independent
  47. func HashEmail(email string) string {
  48. // https://en.gravatar.com/site/implement/hash/
  49. email = strings.TrimSpace(email)
  50. email = strings.ToLower(email)
  51. h := md5.New()
  52. h.Write([]byte(email))
  53. return hex.EncodeToString(h.Sum(nil))
  54. }
  55. const _RANDOM_AVATAR_SIZE = 200
  56. // RandomImage generates and returns a random avatar image.
  57. func RandomImage(data []byte) (image.Image, error) {
  58. randExtent := len(palette.WebSafe) - 32
  59. rand.Seed(time.Now().UnixNano())
  60. colorIndex := rand.Intn(randExtent)
  61. backColorIndex := colorIndex - 1
  62. if backColorIndex < 0 {
  63. backColorIndex = randExtent - 1
  64. }
  65. // Size, background, forecolor
  66. imgMaker, err := identicon.New(_RANDOM_AVATAR_SIZE,
  67. palette.WebSafe[backColorIndex], palette.WebSafe[colorIndex:colorIndex+32]...)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return imgMaker.Make(data), nil
  72. }
  73. // Avatar represents the avatar object.
  74. type Avatar struct {
  75. Hash string
  76. AlterImage string // image path
  77. cacheDir string // image save dir
  78. reqParams string
  79. imagePath string
  80. expireDuration time.Duration
  81. }
  82. func New(hash string, cacheDir string) *Avatar {
  83. return &Avatar{
  84. Hash: hash,
  85. cacheDir: cacheDir,
  86. expireDuration: time.Minute * 10,
  87. reqParams: url.Values{
  88. "d": {"retro"},
  89. "size": {"200"},
  90. "r": {"pg"}}.Encode(),
  91. imagePath: filepath.Join(cacheDir, hash+".image"), //maybe png or jpeg
  92. }
  93. }
  94. func (this *Avatar) HasCache() bool {
  95. fileInfo, err := os.Stat(this.imagePath)
  96. return err == nil && fileInfo.Mode().IsRegular()
  97. }
  98. func (this *Avatar) Modtime() (modtime time.Time, err error) {
  99. fileInfo, err := os.Stat(this.imagePath)
  100. if err != nil {
  101. return
  102. }
  103. return fileInfo.ModTime(), nil
  104. }
  105. func (this *Avatar) Expired() bool {
  106. modtime, err := this.Modtime()
  107. return err != nil || time.Since(modtime) > this.expireDuration
  108. }
  109. // default image format: jpeg
  110. func (this *Avatar) Encode(wr io.Writer, size int) (err error) {
  111. var img image.Image
  112. decodeImageFile := func(file string) (img image.Image, err error) {
  113. fd, err := os.Open(file)
  114. if err != nil {
  115. return
  116. }
  117. defer fd.Close()
  118. if img, err = jpeg.Decode(fd); err != nil {
  119. fd.Seek(0, os.SEEK_SET)
  120. img, err = png.Decode(fd)
  121. }
  122. return
  123. }
  124. imgPath := this.imagePath
  125. if !this.HasCache() {
  126. if this.AlterImage == "" {
  127. return errors.New("request image failed, and no alt image offered")
  128. }
  129. imgPath = this.AlterImage
  130. }
  131. if img, err = decodeImageFile(imgPath); err != nil {
  132. return
  133. }
  134. m := resize.Resize(uint(size), 0, img, resize.NearestNeighbor)
  135. return jpeg.Encode(wr, m, nil)
  136. }
  137. // get image from gravatar.com
  138. func (this *Avatar) Update() {
  139. UpdateGravatarSource()
  140. thunder.Fetch(gravatarSource+this.Hash+"?"+this.reqParams,
  141. this.imagePath)
  142. }
  143. func (this *Avatar) UpdateTimeout(timeout time.Duration) (err error) {
  144. UpdateGravatarSource()
  145. select {
  146. case <-time.After(timeout):
  147. err = fmt.Errorf("get gravatar image %s timeout", this.Hash)
  148. case err = <-thunder.GoFetch(gravatarSource+this.Hash+"?"+this.reqParams,
  149. this.imagePath):
  150. }
  151. return err
  152. }
  153. type service struct {
  154. cacheDir string
  155. altImage string
  156. }
  157. func (this *service) mustInt(r *http.Request, defaultValue int, keys ...string) (v int) {
  158. for _, k := range keys {
  159. if _, err := fmt.Sscanf(r.FormValue(k), "%d", &v); err == nil {
  160. defaultValue = v
  161. }
  162. }
  163. return defaultValue
  164. }
  165. func (this *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  166. urlPath := r.URL.Path
  167. hash := urlPath[strings.LastIndex(urlPath, "/")+1:]
  168. size := this.mustInt(r, 80, "s", "size") // default size = 80*80
  169. avatar := New(hash, this.cacheDir)
  170. avatar.AlterImage = this.altImage
  171. if avatar.Expired() {
  172. if err := avatar.UpdateTimeout(time.Millisecond * 1000); err != nil {
  173. log.Trace("avatar update error: %v", err)
  174. return
  175. }
  176. }
  177. if modtime, err := avatar.Modtime(); err == nil {
  178. etag := fmt.Sprintf("size(%d)", size)
  179. if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) && etag == r.Header.Get("If-None-Match") {
  180. h := w.Header()
  181. delete(h, "Content-Type")
  182. delete(h, "Content-Length")
  183. w.WriteHeader(http.StatusNotModified)
  184. return
  185. }
  186. w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
  187. w.Header().Set("ETag", etag)
  188. }
  189. w.Header().Set("Content-Type", "image/jpeg")
  190. if err := avatar.Encode(w, size); err != nil {
  191. log.Warn("avatar encode error: %v", err)
  192. w.WriteHeader(500)
  193. }
  194. }
  195. // http.Handle("/avatar/", avatar.CacheServer("./cache"))
  196. func CacheServer(cacheDir string, defaultImgPath string) http.Handler {
  197. return &service{
  198. cacheDir: cacheDir,
  199. altImage: defaultImgPath,
  200. }
  201. }
  202. // thunder downloader
  203. var thunder = &Thunder{QueueSize: 10}
  204. type Thunder struct {
  205. QueueSize int // download queue size
  206. q chan *thunderTask
  207. once sync.Once
  208. }
  209. func (t *Thunder) init() {
  210. if t.QueueSize < 1 {
  211. t.QueueSize = 1
  212. }
  213. t.q = make(chan *thunderTask, t.QueueSize)
  214. for i := 0; i < t.QueueSize; i++ {
  215. go func() {
  216. for {
  217. task := <-t.q
  218. task.Fetch()
  219. }
  220. }()
  221. }
  222. }
  223. func (t *Thunder) Fetch(url string, saveFile string) error {
  224. t.once.Do(t.init)
  225. task := &thunderTask{
  226. Url: url,
  227. SaveFile: saveFile,
  228. }
  229. task.Add(1)
  230. t.q <- task
  231. task.Wait()
  232. return task.err
  233. }
  234. func (t *Thunder) GoFetch(url, saveFile string) chan error {
  235. c := make(chan error)
  236. go func() {
  237. c <- t.Fetch(url, saveFile)
  238. }()
  239. return c
  240. }
  241. // thunder download
  242. type thunderTask struct {
  243. Url string
  244. SaveFile string
  245. sync.WaitGroup
  246. err error
  247. }
  248. func (this *thunderTask) Fetch() {
  249. this.err = this.fetch()
  250. this.Done()
  251. }
  252. var client = &http.Client{}
  253. func (this *thunderTask) fetch() error {
  254. log.Debug("avatar.fetch(fetch new avatar): %s", this.Url)
  255. req, _ := http.NewRequest("GET", this.Url, nil)
  256. req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/jpeg,image/png,*/*;q=0.8")
  257. req.Header.Set("Accept-Encoding", "deflate,sdch")
  258. req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
  259. req.Header.Set("Cache-Control", "no-cache")
  260. 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")
  261. resp, err := client.Do(req)
  262. if err != nil {
  263. return err
  264. }
  265. defer resp.Body.Close()
  266. if resp.StatusCode != 200 {
  267. return fmt.Errorf("status code: %d", resp.StatusCode)
  268. }
  269. /*
  270. log.Println("headers:", resp.Header)
  271. switch resp.Header.Get("Content-Type") {
  272. case "image/jpeg":
  273. this.SaveFile += ".jpeg"
  274. case "image/png":
  275. this.SaveFile += ".png"
  276. }
  277. */
  278. /*
  279. imgType := resp.Header.Get("Content-Type")
  280. if imgType != "image/jpeg" && imgType != "image/png" {
  281. return errors.New("not png or jpeg")
  282. }
  283. */
  284. tmpFile := this.SaveFile + ".part" // mv to destination when finished
  285. fd, err := os.Create(tmpFile)
  286. if err != nil {
  287. return err
  288. }
  289. _, err = io.Copy(fd, resp.Body)
  290. fd.Close()
  291. if err != nil {
  292. os.Remove(tmpFile)
  293. return err
  294. }
  295. return os.Rename(tmpFile, this.SaveFile)
  296. }