|
|
@ -1,3 +1,8 @@ |
|
|
|
|
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for www.gravatar.com image cache
|
|
|
|
package avatar |
|
|
|
package avatar |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
@ -22,11 +27,17 @@ import ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
gravatar = "http://www.gravatar.com/avatar" |
|
|
|
gravatar = "http://www.gravatar.com/avatar" |
|
|
|
defaultImagePath = "./default.jpg" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func debug(a ...interface{}) { |
|
|
|
|
|
|
|
if true { |
|
|
|
|
|
|
|
log.Println(a...) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// hash email to md5 string
|
|
|
|
// hash email to md5 string
|
|
|
|
|
|
|
|
// keep this func in order to make this package indenpent
|
|
|
|
func HashEmail(email string) string { |
|
|
|
func HashEmail(email string) string { |
|
|
|
h := md5.New() |
|
|
|
h := md5.New() |
|
|
|
h.Write([]byte(strings.ToLower(email))) |
|
|
|
h.Write([]byte(strings.ToLower(email))) |
|
|
@ -35,6 +46,7 @@ func HashEmail(email string) string { |
|
|
|
|
|
|
|
|
|
|
|
type Avatar struct { |
|
|
|
type Avatar struct { |
|
|
|
Hash string |
|
|
|
Hash string |
|
|
|
|
|
|
|
AlterImage string // image path
|
|
|
|
cacheDir string // image save dir
|
|
|
|
cacheDir string // image save dir
|
|
|
|
reqParams string |
|
|
|
reqParams string |
|
|
|
imagePath string |
|
|
|
imagePath string |
|
|
@ -54,7 +66,7 @@ func New(hash string, cacheDir string) *Avatar { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (this *Avatar) InCache() bool { |
|
|
|
func (this *Avatar) HasCache() bool { |
|
|
|
fileInfo, err := os.Stat(this.imagePath) |
|
|
|
fileInfo, err := os.Stat(this.imagePath) |
|
|
|
return err == nil && fileInfo.Mode().IsRegular() |
|
|
|
return err == nil && fileInfo.Mode().IsRegular() |
|
|
|
} |
|
|
|
} |
|
|
@ -68,11 +80,8 @@ func (this *Avatar) Modtime() (modtime time.Time, err error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (this *Avatar) Expired() bool { |
|
|
|
func (this *Avatar) Expired() bool { |
|
|
|
if !this.InCache() { |
|
|
|
modtime, err := this.Modtime() |
|
|
|
return true |
|
|
|
return err != nil || time.Since(modtime) > this.expireDuration |
|
|
|
} |
|
|
|
|
|
|
|
fileInfo, err := os.Stat(this.imagePath) |
|
|
|
|
|
|
|
return err != nil || time.Since(fileInfo.ModTime()) > this.expireDuration |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// default image format: jpeg
|
|
|
|
// default image format: jpeg
|
|
|
@ -92,8 +101,11 @@ func (this *Avatar) Encode(wr io.Writer, size int) (err error) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
imgPath := this.imagePath |
|
|
|
imgPath := this.imagePath |
|
|
|
if !this.InCache() { |
|
|
|
if !this.HasCache() { |
|
|
|
imgPath = defaultImagePath |
|
|
|
if this.AlterImage == "" { |
|
|
|
|
|
|
|
return errors.New("request image failed, and no alt image offered") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imgPath = this.AlterImage |
|
|
|
} |
|
|
|
} |
|
|
|
img, err = decodeImageFile(imgPath) |
|
|
|
img, err = decodeImageFile(imgPath) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -120,61 +132,66 @@ func (this *Avatar) UpdateTimeout(timeout time.Duration) error { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
type avatarHandler struct { |
|
|
|
log.SetFlags(log.Lshortfile | log.LstdFlags) |
|
|
|
cacheDir string |
|
|
|
|
|
|
|
altImage string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// http.Handle("/avatar/", avatar.HttpHandler("./cache"))
|
|
|
|
func (this *avatarHandler) mustInt(r *http.Request, defaultValue int, keys ...string) int { |
|
|
|
func HttpHandler(cacheDir string) func(w http.ResponseWriter, r *http.Request) { |
|
|
|
var v int |
|
|
|
MustInt := func(r *http.Request, defaultValue int, keys ...string) int { |
|
|
|
for _, k := range keys { |
|
|
|
var v int |
|
|
|
if _, err := fmt.Sscanf(r.FormValue(k), "%d", &v); err == nil { |
|
|
|
for _, k := range keys { |
|
|
|
defaultValue = v |
|
|
|
if _, err := fmt.Sscanf(r.FormValue(k), "%d", &v); err == nil { |
|
|
|
|
|
|
|
defaultValue = v |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return defaultValue |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return defaultValue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
func (this *avatarHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
|
|
|
urlPath := r.URL.Path |
|
|
|
urlPath := r.URL.Path |
|
|
|
hash := urlPath[strings.LastIndex(urlPath, "/")+1:] |
|
|
|
hash := urlPath[strings.LastIndex(urlPath, "/")+1:] |
|
|
|
hash = HashEmail(hash) |
|
|
|
//hash = HashEmail(hash)
|
|
|
|
size := MustInt(r, 80, "s", "size") // size = 80*80
|
|
|
|
size := this.mustInt(r, 80, "s", "size") // size = 80*80
|
|
|
|
|
|
|
|
|
|
|
|
avatar := New(hash, cacheDir) |
|
|
|
avatar := New(hash, this.cacheDir) |
|
|
|
if avatar.Expired() { |
|
|
|
avatar.AlterImage = this.altImage |
|
|
|
err := avatar.UpdateTimeout(time.Millisecond * 500) |
|
|
|
if avatar.Expired() { |
|
|
|
if err != nil { |
|
|
|
err := avatar.UpdateTimeout(time.Millisecond * 500) |
|
|
|
log.Println(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if modtime, err := avatar.Modtime(); err == nil { |
|
|
|
|
|
|
|
etag := fmt.Sprintf("size(%d)", size) |
|
|
|
|
|
|
|
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") { |
|
|
|
|
|
|
|
h := w.Header() |
|
|
|
|
|
|
|
delete(h, "Content-Type") |
|
|
|
|
|
|
|
delete(h, "Content-Length") |
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNotModified) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat)) |
|
|
|
|
|
|
|
w.Header().Set("ETag", etag) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/jpeg") |
|
|
|
|
|
|
|
err := avatar.Encode(w, size) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Println(err) |
|
|
|
debug(err) |
|
|
|
w.WriteHeader(500) |
|
|
|
//log.Trace("avatar update error: %v", err)
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if modtime, err := avatar.Modtime(); err == nil { |
|
|
|
|
|
|
|
etag := fmt.Sprintf("size(%d)", size) |
|
|
|
|
|
|
|
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") { |
|
|
|
|
|
|
|
h := w.Header() |
|
|
|
|
|
|
|
delete(h, "Content-Type") |
|
|
|
|
|
|
|
delete(h, "Content-Length") |
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNotModified) |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat)) |
|
|
|
|
|
|
|
w.Header().Set("ETag", etag) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/jpeg") |
|
|
|
|
|
|
|
err := avatar.Encode(w, size) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
//log.Warn("avatar encode error: %v", err) // will panic when err != nil
|
|
|
|
|
|
|
|
debug(err) |
|
|
|
|
|
|
|
w.WriteHeader(500) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
// http.Handle("/avatar/", avatar.HttpHandler("./cache"))
|
|
|
|
http.HandleFunc("/", HttpHandler("./")) |
|
|
|
func HttpHandler(cacheDir string, defaultImgPath string) http.Handler { |
|
|
|
log.Fatal(http.ListenAndServe(":8001", nil)) |
|
|
|
return &avatarHandler{ |
|
|
|
|
|
|
|
cacheDir: cacheDir, |
|
|
|
|
|
|
|
altImage: defaultImgPath, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// thunder downloader
|
|
|
|
var thunder = &Thunder{QueueSize: 10} |
|
|
|
var thunder = &Thunder{QueueSize: 10} |
|
|
|
|
|
|
|
|
|
|
|
type Thunder struct { |
|
|
|
type Thunder struct { |
|
|
@ -234,7 +251,7 @@ func (this *thunderTask) Fetch() { |
|
|
|
var client = &http.Client{} |
|
|
|
var client = &http.Client{} |
|
|
|
|
|
|
|
|
|
|
|
func (this *thunderTask) fetch() error { |
|
|
|
func (this *thunderTask) fetch() error { |
|
|
|
log.Println("thunder, fetch", this.Url) |
|
|
|
//log.Println("thunder, fetch", this.Url)
|
|
|
|
req, _ := http.NewRequest("GET", this.Url, nil) |
|
|
|
req, _ := http.NewRequest("GET", this.Url, nil) |
|
|
|
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") |
|
|
|
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") |
|
|
|
req.Header.Set("Accept-Encoding", "gzip,deflate,sdch") |
|
|
|
req.Header.Set("Accept-Encoding", "gzip,deflate,sdch") |
|
|
|