Remove cache avatar support and add its tests

This commit is contained in:
Unknwon
2016-02-14 23:14:55 -05:00
parent fd92d91da3
commit 58e004f7da
14 changed files with 45 additions and 390 deletions

View File

@ -11,6 +11,7 @@ watch_dirs = [
"$WORKDIR/routers" "$WORKDIR/routers"
] ]
watch_exts = [".go"] watch_exts = [".go"]
ignore_files = [".+_test.go"]
build_delay = 1500 build_delay = 1500
cmds = [ cmds = [
["make", "build-dev"], # sqlite cert pam tidb ["make", "build-dev"], # sqlite cert pam tidb

View File

@ -19,13 +19,13 @@ build: $(GENERATED)
go install -v -ldflags '$(LDFLAGS)' -tags '$(TAGS)' go install -v -ldflags '$(LDFLAGS)' -tags '$(TAGS)'
cp '$(GOPATH)/bin/gogs' . cp '$(GOPATH)/bin/gogs' .
build-dev: $(GENERATED)
go install -v -race -tags '$(TAGS)'
cp '$(GOPATH)/bin/gogs' .
govet: govet:
go tool vet -composites=false -methods=false -structtags=false . go tool vet -composites=false -methods=false -structtags=false .
build-dev: $(GENERATED) govet
go install -v -race -tags '$(TAGS)'
cp '$(GOPATH)/bin/gogs' .
pack: pack:
rm -rf $(RELEASE_GOGS) rm -rf $(RELEASE_GOGS)
mkdir -p $(RELEASE_GOGS) mkdir -p $(RELEASE_GOGS)
@ -52,4 +52,4 @@ clean-mac: clean
find . -name ".DS_Store" -print0 | xargs -0 rm find . -name ".DS_Store" -print0 | xargs -0 rm
test: test:
go test ./... go test -cover -race ./...

View File

@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true) ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
##### Current version: 0.8.35 ##### Current version: 0.8.36
| Web | UI | Preview | | Web | UI | Preview |
|:-------------:|:-------:|:-------:| |:-------------:|:-------:|:-------:|

View File

@ -34,7 +34,6 @@ import (
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/avatar"
"github.com/gogits/gogs/modules/bindata" "github.com/gogits/gogs/modules/bindata"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
@ -245,11 +244,6 @@ func runWeb(ctx *cli.Context) {
}) })
// ***** END: User ***** // ***** END: User *****
// Gravatar service.
avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
os.MkdirAll("public/img/avatar/", os.ModePerm)
m.Get("/avatar/:hash", avt.ServeHTTP)
adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true}) adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
// ***** START: Admin ***** // ***** START: Admin *****

View File

@ -120,8 +120,6 @@ REGISTER_EMAIL_CONFIRM = false
DISABLE_REGISTRATION = false DISABLE_REGISTRATION = false
; User must sign in to view anything. ; User must sign in to view anything.
REQUIRE_SIGNIN_VIEW = false REQUIRE_SIGNIN_VIEW = false
; Cache avatar as picture
ENABLE_CACHE_AVATAR = false
; Mail notification ; Mail notification
ENABLE_NOTIFY_MAIL = false ENABLE_NOTIFY_MAIL = false
; More detail: https://github.com/gogits/gogs/issues/165 ; More detail: https://github.com/gogits/gogs/issues/165

View File

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.8.35.0214" const APP_VER = "0.8.36.0214"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View File

@ -247,8 +247,6 @@ func (u *User) RelAvatarLink() string {
} }
return "/avatars/" + com.ToStr(u.Id) return "/avatars/" + com.ToStr(u.Id)
case setting.Service.EnableCacheAvatar:
return "/avatar/" + u.Avatar
} }
return setting.GravatarSource + u.Avatar return setting.GravatarSource + u.Avatar
} }
@ -493,7 +491,7 @@ func CreateUser(u *User) (err error) {
u.LowerName = strings.ToLower(u.Name) u.LowerName = strings.ToLower(u.Name)
u.AvatarEmail = u.Email u.AvatarEmail = u.Email
u.Avatar = avatar.HashEmail(u.AvatarEmail) u.Avatar = base.HashEmail(u.AvatarEmail)
u.Rands = GetUserSalt() u.Rands = GetUserSalt()
u.Salt = GetUserSalt() u.Salt = GetUserSalt()
u.EncodePasswd() u.EncodePasswd()
@ -628,7 +626,7 @@ func updateUser(e Engine, u *User) error {
if len(u.AvatarEmail) == 0 { if len(u.AvatarEmail) == 0 {
u.AvatarEmail = u.Email u.AvatarEmail = u.Email
} }
u.Avatar = avatar.HashEmail(u.AvatarEmail) u.Avatar = base.HashEmail(u.AvatarEmail)
} }
u.LowerName = strings.ToLower(u.Name) u.LowerName = strings.ToLower(u.Name)

File diff suppressed because it is too large Load Diff

View File

@ -1,61 +1,23 @@
// Copyright 2014 The Gogs Authors. All rights reserved. // Copyright 2016 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package avatar_test
package avatar
import ( import (
"errors"
"os"
"strconv"
"testing" "testing"
"time"
"github.com/gogits/gogs/modules/avatar" . "github.com/smartystreets/goconvey/convey"
"github.com/gogits/gogs/modules/log"
) )
const TMPDIR = "test-avatar" func Test_RandomImage(t *testing.T) {
Convey("Generate a random avatar from email", t, func() {
_, err := RandomImage([]byte("gogs@local"))
So(err, ShouldBeNil)
func TestFetch(t *testing.T) { Convey("Try to generate an image with size zero", func() {
os.Mkdir(TMPDIR, 0755) _, err := RandomImageSize(0, []byte("gogs@local"))
defer os.RemoveAll(TMPDIR) So(err, ShouldNotBeNil)
})
hash := avatar.HashEmail("ssx205@gmail.com") })
a := avatar.New(hash, TMPDIR)
a.UpdateTimeout(time.Millisecond * 200)
}
func TestFetchMany(t *testing.T) {
os.Mkdir(TMPDIR, 0755)
defer os.RemoveAll(TMPDIR)
t.Log("start")
var n = 5
ch := make(chan bool, n)
for i := 0; i < n; i++ {
go func(i int) {
hash := avatar.HashEmail(strconv.Itoa(i) + "ssx205@gmail.com")
a := avatar.New(hash, TMPDIR)
a.Update()
t.Log("finish", hash)
ch <- true
}(i)
}
for i := 0; i < n; i++ {
<-ch
}
t.Log("end")
}
// cat
// wget http://www.artsjournal.com/artfulmanager/wp/wp-content/uploads/2013/12/200x200xmirror_cat.jpg.pagespeed.ic.GOZSv6v1_H.jpg -O default.jpg
/*
func TestHttp(t *testing.T) {
http.Handle("/", avatar.CacheServer("./", "default.jpg"))
http.ListenAndServe(":8001", nil)
}
*/
func TestLogTrace(t *testing.T) {
log.Trace("%v", errors.New("console log test"))
} }

View File

@ -26,7 +26,6 @@ import (
"github.com/gogits/chardet" "github.com/gogits/chardet"
"github.com/gogits/gogs/modules/avatar"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
@ -209,17 +208,22 @@ func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string
return code return code
} }
// AvatarLink returns avatar link by given e-mail. // HashEmail hashes email address to MD5 string.
// https://en.gravatar.com/site/implement/hash/
func HashEmail(email string) string {
email = strings.ToLower(strings.TrimSpace(email))
h := md5.New()
h.Write([]byte(email))
return hex.EncodeToString(h.Sum(nil))
}
// AvatarLink returns avatar link by given email.
func AvatarLink(email string) string { func AvatarLink(email string) string {
if setting.DisableGravatar || setting.OfflineMode { if setting.DisableGravatar || setting.OfflineMode {
return setting.AppSubUrl + "/img/avatar_default.jpg" return setting.AppSubUrl + "/img/avatar_default.jpg"
} }
gravatarHash := avatar.HashEmail(email) return setting.GravatarSource + HashEmail(email)
if setting.Service.EnableCacheAvatar {
return setting.AppSubUrl + "/avatar/" + gravatarHash
}
return setting.GravatarSource + gravatarHash
} }
// Seconds-based time units // Seconds-based time units

File diff suppressed because one or more lines are too long

View File

@ -455,7 +455,6 @@ var Service struct {
DisableRegistration bool DisableRegistration bool
ShowRegistrationButton bool ShowRegistrationButton bool
RequireSignInView bool RequireSignInView bool
EnableCacheAvatar bool
EnableNotifyMail bool EnableNotifyMail bool
EnableReverseProxyAuth bool EnableReverseProxyAuth bool
EnableReverseProxyAutoRegister bool EnableReverseProxyAutoRegister bool
@ -469,7 +468,6 @@ func newService() {
Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool() Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool()
Service.ShowRegistrationButton = sec.Key("SHOW_REGISTRATION_BUTTON").MustBool(!Service.DisableRegistration) Service.ShowRegistrationButton = sec.Key("SHOW_REGISTRATION_BUTTON").MustBool(!Service.DisableRegistration)
Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool() Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool()
Service.EnableCacheAvatar = sec.Key("ENABLE_CACHE_AVATAR").MustBool()
Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool() Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool()
Service.EnableReverseProxyAutoRegister = sec.Key("ENABLE_REVERSE_PROXY_AUTO_REGISTRATION").MustBool() Service.EnableReverseProxyAutoRegister = sec.Key("ENABLE_REVERSE_PROXY_AUTO_REGISTRATION").MustBool()
Service.EnableCaptcha = sec.Key("ENABLE_CAPTCHA").MustBool() Service.EnableCaptcha = sec.Key("ENABLE_CAPTCHA").MustBool()

View File

@ -145,8 +145,6 @@ func listen(config *ssh.ServerConfig, port int) {
go handleServerConn(sConn.Permissions.Extensions["key-id"], chans) go handleServerConn(sConn.Permissions.Extensions["key-id"], chans)
}() }()
} }
panic("SSH: Unexpected exit of listen loop")
} }
// Listen starts a SSH server listens on given port. // Listen starts a SSH server listens on given port.

View File

@ -1 +1 @@
0.8.35.0214 0.8.36.0214