save custom avatar as PNG

This commit is contained in:
Unknwon 2015-11-13 16:43:43 -05:00
parent 6a664e88c7
commit 1559bd58e7
5 changed files with 29 additions and 20 deletions

View file

@ -13,7 +13,7 @@ watch_dirs = [
watch_exts = [".go"] watch_exts = [".go"]
build_delay = 1500 build_delay = 1500
cmds = [ cmds = [
["go", "install"], # sqlite redis memcache cert pam tidb ["go", "install", "-race"], # sqlite redis memcache cert pam tidb
["go", "build"], ["go", "build", "-race"],
["./gogs", "web"] ["./gogs", "web"]
] ]

View file

@ -14,6 +14,7 @@ import (
"image" "image"
"image/jpeg" "image/jpeg"
_ "image/jpeg" _ "image/jpeg"
"image/png"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -253,11 +254,9 @@ func (u *User) ValidatePassword(passwd string) bool {
// UploadAvatar saves custom avatar for user. // UploadAvatar saves custom avatar for user.
// FIXME: split uploads to different subdirs in case we have massive users. // FIXME: split uploads to different subdirs in case we have massive users.
func (u *User) UploadAvatar(data []byte) error { func (u *User) UploadAvatar(data []byte) error {
u.UseCustomAvatar = true
img, _, err := image.Decode(bytes.NewReader(data)) img, _, err := image.Decode(bytes.NewReader(data))
if err != nil { if err != nil {
return err return fmt.Errorf("Decode: %v", err)
} }
m := resize.Resize(234, 234, img, resize.NearestNeighbor) m := resize.Resize(234, 234, img, resize.NearestNeighbor)
@ -268,19 +267,20 @@ func (u *User) UploadAvatar(data []byte) error {
return err return err
} }
if _, err = sess.Id(u.Id).AllCols().Update(u); err != nil { u.UseCustomAvatar = true
return err if err = updateUser(sess, u); err != nil {
return fmt.Errorf("updateUser: %v", err)
} }
os.MkdirAll(setting.AvatarUploadPath, os.ModePerm) os.MkdirAll(setting.AvatarUploadPath, os.ModePerm)
fw, err := os.Create(u.CustomAvatarPath()) fw, err := os.Create(u.CustomAvatarPath())
if err != nil { if err != nil {
return err return fmt.Errorf("Create: %v", err)
} }
defer fw.Close() defer fw.Close()
if err = jpeg.Encode(fw, m, nil); err != nil { if err = png.Encode(fw, m); err != nil {
return err return fmt.Errorf("Encode: %v", err)
} }
return sess.Commit() return sess.Commit()

View file

@ -39,6 +39,8 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
//FIXME: remove cache module
var gravatarSource string var gravatarSource string
func UpdateGravatarSource() { func UpdateGravatarSource() {
@ -153,7 +155,7 @@ func (this *Avatar) Encode(wr io.Writer, size int) (err error) {
if img, err = decodeImageFile(imgPath); err != nil { if img, err = decodeImageFile(imgPath); err != nil {
return 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) return jpeg.Encode(wr, m, nil)
} }

View file

@ -205,7 +205,7 @@ func Contexter() macaron.Handler {
Session: sess, Session: sess,
} }
// Compute current URL for real-time change language. // 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() ctx.Data["PageStartTime"] = time.Now()

View file

@ -111,6 +111,7 @@ func Dashboard(ctx *middleware.Context) {
// Check access of private repositories. // Check access of private repositories.
feeds := make([]*models.Action, 0, len(actions)) feeds := make([]*models.Action, 0, len(actions))
unameAvatars := make(map[string]string)
for _, act := range actions { for _, act := range actions {
if act.IsPrivate { if act.IsPrivate {
// This prevents having to retrieve the repository for each action // This prevents having to retrieve the repository for each action
@ -122,7 +123,10 @@ func Dashboard(ctx *middleware.Context) {
} }
} }
// FIXME: cache results?
// Cache results to reduce queries.
_, ok := unameAvatars[act.ActUserName]
if !ok {
u, err := models.GetUserByName(act.ActUserName) u, err := models.GetUserByName(act.ActUserName)
if err != nil { if err != nil {
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
@ -131,7 +135,10 @@ func Dashboard(ctx *middleware.Context) {
ctx.Handle(500, "GetUserByName", err) ctx.Handle(500, "GetUserByName", err)
return return
} }
act.ActAvatar = u.AvatarLink() unameAvatars[act.ActUserName] = u.AvatarLink()
}
act.ActAvatar = unameAvatars[act.ActUserName]
feeds = append(feeds, act) feeds = append(feeds, act)
} }
ctx.Data["Feeds"] = feeds ctx.Data["Feeds"] = feeds