mirror of
https://github.com/go-gitea/gitea
synced 2024-11-18 08:04:25 +00:00
commit
19de20c2f8
@ -3,6 +3,14 @@
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// for www.gravatar.com image cache
|
||||
|
||||
/*
|
||||
It is recommend to use this way
|
||||
|
||||
cacheDir := "./cache"
|
||||
defaultImg := "./default.jpg"
|
||||
http.Handle("/avatar/", avatar.HttpHandler(cacheDir, defaultImg))
|
||||
*/
|
||||
package avatar
|
||||
|
||||
import (
|
||||
@ -14,7 +22,6 @@ import (
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@ -23,6 +30,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
"github.com/nfnt/resize"
|
||||
)
|
||||
|
||||
@ -30,12 +38,6 @@ var (
|
||||
gravatar = "http://www.gravatar.com/avatar"
|
||||
)
|
||||
|
||||
func debug(a ...interface{}) {
|
||||
if true {
|
||||
log.Println(a...)
|
||||
}
|
||||
}
|
||||
|
||||
// hash email to md5 string
|
||||
// keep this func in order to make this package indenpent
|
||||
func HashEmail(email string) string {
|
||||
@ -125,7 +127,7 @@ func (this *Avatar) UpdateTimeout(timeout time.Duration) error {
|
||||
var err error
|
||||
select {
|
||||
case <-time.After(timeout):
|
||||
err = errors.New("get gravatar image timeout")
|
||||
err = fmt.Errorf("get gravatar image %s timeout", this.Hash)
|
||||
case err = <-thunder.GoFetch(gravatar+"/"+this.Hash+"?"+this.reqParams,
|
||||
this.imagePath):
|
||||
}
|
||||
@ -150,16 +152,14 @@ func (this *avatarHandler) mustInt(r *http.Request, defaultValue int, keys ...st
|
||||
func (this *avatarHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
urlPath := r.URL.Path
|
||||
hash := urlPath[strings.LastIndex(urlPath, "/")+1:]
|
||||
//hash = HashEmail(hash)
|
||||
size := this.mustInt(r, 80, "s", "size") // size = 80*80
|
||||
size := this.mustInt(r, 80, "s", "size") // default size = 80*80
|
||||
|
||||
avatar := New(hash, this.cacheDir)
|
||||
avatar.AlterImage = this.altImage
|
||||
if avatar.Expired() {
|
||||
err := avatar.UpdateTimeout(time.Millisecond * 500)
|
||||
if err != nil {
|
||||
debug(err)
|
||||
//log.Trace("avatar update error: %v", err)
|
||||
log.Trace("avatar update error: %v", err)
|
||||
}
|
||||
}
|
||||
if modtime, err := avatar.Modtime(); err == nil {
|
||||
@ -177,8 +177,7 @@ func (this *avatarHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
log.Warn("avatar encode error: %v", err)
|
||||
w.WriteHeader(500)
|
||||
}
|
||||
}
|
||||
@ -251,7 +250,6 @@ func (this *thunderTask) Fetch() {
|
||||
var client = &http.Client{}
|
||||
|
||||
func (this *thunderTask) fetch() error {
|
||||
//log.Println("thunder, fetch", this.Url)
|
||||
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-Encoding", "gzip,deflate,sdch")
|
||||
|
@ -4,13 +4,14 @@
|
||||
package avatar_test
|
||||
|
||||
import (
|
||||
"log"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogits/gogs/modules/avatar"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
)
|
||||
|
||||
const TMPDIR = "test-avatar"
|
||||
@ -28,7 +29,7 @@ func TestFetchMany(t *testing.T) {
|
||||
os.Mkdir(TMPDIR, 0755)
|
||||
defer os.RemoveAll(TMPDIR)
|
||||
|
||||
log.Println("start")
|
||||
t.Log("start")
|
||||
var n = 5
|
||||
ch := make(chan bool, n)
|
||||
for i := 0; i < n; i++ {
|
||||
@ -36,14 +37,14 @@ func TestFetchMany(t *testing.T) {
|
||||
hash := avatar.HashEmail(strconv.Itoa(i) + "ssx205@gmail.com")
|
||||
a := avatar.New(hash, TMPDIR)
|
||||
a.Update()
|
||||
log.Println("finish", hash)
|
||||
t.Log("finish", hash)
|
||||
ch <- true
|
||||
}(i)
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
<-ch
|
||||
}
|
||||
log.Println("end")
|
||||
t.Log("end")
|
||||
}
|
||||
|
||||
// cat
|
||||
@ -54,3 +55,7 @@ func TestHttp(t *testing.T) {
|
||||
http.ListenAndServe(":8001", nil)
|
||||
}
|
||||
*/
|
||||
|
||||
func TestLogTrace(t *testing.T) {
|
||||
log.Trace("%v", errors.New("console log test"))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user