From 1559bd58e7dee6927d31e1aaf1b6ebfc0ffd59dc Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 13 Nov 2015 16:43:43 -0500 Subject: [PATCH] save custom avatar as PNG --- .bra.toml | 4 ++-- models/user.go | 16 ++++++++-------- modules/avatar/avatar.go | 4 +++- modules/middleware/context.go | 2 +- routers/user/home.go | 23 +++++++++++++++-------- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/.bra.toml b/.bra.toml index 8789ca8f..7d49786d 100644 --- a/.bra.toml +++ b/.bra.toml @@ -13,7 +13,7 @@ watch_dirs = [ watch_exts = [".go"] build_delay = 1500 cmds = [ - ["go", "install"], # sqlite redis memcache cert pam tidb - ["go", "build"], + ["go", "install", "-race"], # sqlite redis memcache cert pam tidb + ["go", "build", "-race"], ["./gogs", "web"] ] \ No newline at end of file diff --git a/models/user.go b/models/user.go index 4f9095ed..628a50f7 100644 --- a/models/user.go +++ b/models/user.go @@ -14,6 +14,7 @@ import ( "image" "image/jpeg" _ "image/jpeg" + "image/png" "os" "path" "path/filepath" @@ -253,11 +254,9 @@ func (u *User) ValidatePassword(passwd string) bool { // UploadAvatar saves custom avatar for user. // FIXME: split uploads to different subdirs in case we have massive users. func (u *User) UploadAvatar(data []byte) error { - u.UseCustomAvatar = true - img, _, err := image.Decode(bytes.NewReader(data)) if err != nil { - return err + return fmt.Errorf("Decode: %v", err) } m := resize.Resize(234, 234, img, resize.NearestNeighbor) @@ -268,19 +267,20 @@ func (u *User) UploadAvatar(data []byte) error { return err } - if _, err = sess.Id(u.Id).AllCols().Update(u); err != nil { - return err + u.UseCustomAvatar = true + if err = updateUser(sess, u); err != nil { + return fmt.Errorf("updateUser: %v", err) } os.MkdirAll(setting.AvatarUploadPath, os.ModePerm) fw, err := os.Create(u.CustomAvatarPath()) if err != nil { - return err + return fmt.Errorf("Create: %v", err) } defer fw.Close() - if err = jpeg.Encode(fw, m, nil); err != nil { - return err + if err = png.Encode(fw, m); err != nil { + return fmt.Errorf("Encode: %v", err) } return sess.Commit() diff --git a/modules/avatar/avatar.go b/modules/avatar/avatar.go index e037703b..60f7e2ec 100644 --- a/modules/avatar/avatar.go +++ b/modules/avatar/avatar.go @@ -39,6 +39,8 @@ import ( "github.com/gogits/gogs/modules/setting" ) +//FIXME: remove cache module + var gravatarSource string func UpdateGravatarSource() { @@ -153,7 +155,7 @@ func (this *Avatar) Encode(wr io.Writer, size int) (err error) { if img, err = decodeImageFile(imgPath); err != nil { return } - m := resize.Resize(uint(size), 0, img, resize.NearestNeighbor) + m := resize.Resize(uint(size), 0, img, resize.Lanczos3) return jpeg.Encode(wr, m, nil) } diff --git a/modules/middleware/context.go b/modules/middleware/context.go index dffebe6f..3910a205 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -205,7 +205,7 @@ func Contexter() macaron.Handler { Session: sess, } // Compute current URL for real-time change language. - ctx.Data["Link"] = setting.AppSubUrl + ctx.Req.URL.Path + ctx.Data["Link"] = setting.AppSubUrl + strings.TrimSuffix(ctx.Req.URL.Path, "/") ctx.Data["PageStartTime"] = time.Now() diff --git a/routers/user/home.go b/routers/user/home.go index c5c42694..fc94dfc9 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -111,6 +111,7 @@ func Dashboard(ctx *middleware.Context) { // Check access of private repositories. feeds := make([]*models.Action, 0, len(actions)) + unameAvatars := make(map[string]string) for _, act := range actions { if act.IsPrivate { // This prevents having to retrieve the repository for each action @@ -122,16 +123,22 @@ func Dashboard(ctx *middleware.Context) { } } - // FIXME: cache results? - u, err := models.GetUserByName(act.ActUserName) - if err != nil { - if models.IsErrUserNotExist(err) { - continue + + // Cache results to reduce queries. + _, ok := unameAvatars[act.ActUserName] + if !ok { + u, err := models.GetUserByName(act.ActUserName) + if err != nil { + if models.IsErrUserNotExist(err) { + continue + } + ctx.Handle(500, "GetUserByName", err) + return } - ctx.Handle(500, "GetUserByName", err) - return + unameAvatars[act.ActUserName] = u.AvatarLink() } - act.ActAvatar = u.AvatarLink() + + act.ActAvatar = unameAvatars[act.ActUserName] feeds = append(feeds, act) } ctx.Data["Feeds"] = feeds