commit
19de20c2f8
2 changed files with 22 additions and 19 deletions
|
@ -3,6 +3,14 @@
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// for www.gravatar.com image cache
|
// 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
|
package avatar
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -14,7 +22,6 @@ import (
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"image/png"
|
"image/png"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -23,6 +30,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gogits/gogs/modules/log"
|
||||||
"github.com/nfnt/resize"
|
"github.com/nfnt/resize"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -30,12 +38,6 @@ var (
|
||||||
gravatar = "http://www.gravatar.com/avatar"
|
gravatar = "http://www.gravatar.com/avatar"
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
// keep this func in order to make this package indenpent
|
||||||
func HashEmail(email string) string {
|
func HashEmail(email string) string {
|
||||||
|
@ -125,7 +127,7 @@ func (this *Avatar) UpdateTimeout(timeout time.Duration) error {
|
||||||
var err error
|
var err error
|
||||||
select {
|
select {
|
||||||
case <-time.After(timeout):
|
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,
|
case err = <-thunder.GoFetch(gravatar+"/"+this.Hash+"?"+this.reqParams,
|
||||||
this.imagePath):
|
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) {
|
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)
|
size := this.mustInt(r, 80, "s", "size") // default size = 80*80
|
||||||
size := this.mustInt(r, 80, "s", "size") // size = 80*80
|
|
||||||
|
|
||||||
avatar := New(hash, this.cacheDir)
|
avatar := New(hash, this.cacheDir)
|
||||||
avatar.AlterImage = this.altImage
|
avatar.AlterImage = this.altImage
|
||||||
if avatar.Expired() {
|
if avatar.Expired() {
|
||||||
err := avatar.UpdateTimeout(time.Millisecond * 500)
|
err := avatar.UpdateTimeout(time.Millisecond * 500)
|
||||||
if err != nil {
|
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 {
|
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")
|
w.Header().Set("Content-Type", "image/jpeg")
|
||||||
err := avatar.Encode(w, size)
|
err := avatar.Encode(w, size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//log.Warn("avatar encode error: %v", err) // will panic when err != nil
|
log.Warn("avatar encode error: %v", err)
|
||||||
debug(err)
|
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -251,7 +250,6 @@ 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)
|
|
||||||
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")
|
||||||
|
|
|
@ -4,13 +4,14 @@
|
||||||
package avatar_test
|
package avatar_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gogits/gogs/modules/avatar"
|
"github.com/gogits/gogs/modules/avatar"
|
||||||
|
"github.com/gogits/gogs/modules/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
const TMPDIR = "test-avatar"
|
const TMPDIR = "test-avatar"
|
||||||
|
@ -28,7 +29,7 @@ func TestFetchMany(t *testing.T) {
|
||||||
os.Mkdir(TMPDIR, 0755)
|
os.Mkdir(TMPDIR, 0755)
|
||||||
defer os.RemoveAll(TMPDIR)
|
defer os.RemoveAll(TMPDIR)
|
||||||
|
|
||||||
log.Println("start")
|
t.Log("start")
|
||||||
var n = 5
|
var n = 5
|
||||||
ch := make(chan bool, n)
|
ch := make(chan bool, n)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
|
@ -36,14 +37,14 @@ func TestFetchMany(t *testing.T) {
|
||||||
hash := avatar.HashEmail(strconv.Itoa(i) + "ssx205@gmail.com")
|
hash := avatar.HashEmail(strconv.Itoa(i) + "ssx205@gmail.com")
|
||||||
a := avatar.New(hash, TMPDIR)
|
a := avatar.New(hash, TMPDIR)
|
||||||
a.Update()
|
a.Update()
|
||||||
log.Println("finish", hash)
|
t.Log("finish", hash)
|
||||||
ch <- true
|
ch <- true
|
||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
<-ch
|
<-ch
|
||||||
}
|
}
|
||||||
log.Println("end")
|
t.Log("end")
|
||||||
}
|
}
|
||||||
|
|
||||||
// cat
|
// cat
|
||||||
|
@ -54,3 +55,7 @@ func TestHttp(t *testing.T) {
|
||||||
http.ListenAndServe(":8001", nil)
|
http.ListenAndServe(":8001", nil)
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
func TestLogTrace(t *testing.T) {
|
||||||
|
log.Trace("%v", errors.New("console log test"))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue