save custom avatar as PNG

release/v1.15
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"]
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"]
]

View File

@ -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()

View File

@ -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)
}

View File

@ -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()

View File

@ -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,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)
if err != nil {
if models.IsErrUserNotExist(err) {
@ -131,7 +135,10 @@ func Dashboard(ctx *middleware.Context) {
ctx.Handle(500, "GetUserByName", err)
return
}
act.ActAvatar = u.AvatarLink()
unameAvatars[act.ActUserName] = u.AvatarLink()
}
act.ActAvatar = unameAvatars[act.ActUserName]
feeds = append(feeds, act)
}
ctx.Data["Feeds"] = feeds