diff options
Diffstat (limited to 'internal/route/user/setting.go')
-rw-r--r-- | internal/route/user/setting.go | 39 |
1 files changed, 16 insertions, 23 deletions
diff --git a/internal/route/user/setting.go b/internal/route/user/setting.go index cdb5eee2..3ea1c33e 100644 --- a/internal/route/user/setting.go +++ b/internal/route/user/setting.go @@ -13,9 +13,9 @@ import ( "io" "strings" + "github.com/pkg/errors" "github.com/pquerna/otp" "github.com/pquerna/otp/totp" - "github.com/unknwon/com" log "unknwon.dev/clog/v2" "gogs.io/gogs/internal/auth" @@ -23,7 +23,6 @@ import ( "gogs.io/gogs/internal/context" "gogs.io/gogs/internal/cryptoutil" "gogs.io/gogs/internal/db" - "gogs.io/gogs/internal/db/errors" "gogs.io/gogs/internal/email" "gogs.io/gogs/internal/form" "gogs.io/gogs/internal/tool" @@ -117,10 +116,15 @@ func SettingsPost(c *context.Context, f form.UpdateProfile) { // FIXME: limit upload size func UpdateAvatarSetting(c *context.Context, f form.Avatar, ctxUser *db.User) error { - ctxUser.UseCustomAvatar = f.Source == form.AVATAR_LOCAL - if len(f.Gravatar) > 0 { + if f.Source == form.AVATAR_BYMAIL && len(f.Gravatar) > 0 { + ctxUser.UseCustomAvatar = false ctxUser.Avatar = cryptoutil.MD5(f.Gravatar) ctxUser.AvatarEmail = f.Gravatar + + if err := db.UpdateUser(ctxUser); err != nil { + return fmt.Errorf("update user: %v", err) + } + return nil } if f.Avatar != nil && f.Avatar.Filename != "" { @@ -128,9 +132,7 @@ func UpdateAvatarSetting(c *context.Context, f form.Avatar, ctxUser *db.User) er if err != nil { return fmt.Errorf("open avatar reader: %v", err) } - defer func() { - _ = r.Close() - }() + defer func() { _ = r.Close() }() data, err := io.ReadAll(r) if err != nil { @@ -139,23 +141,13 @@ func UpdateAvatarSetting(c *context.Context, f form.Avatar, ctxUser *db.User) er if !tool.IsImageFile(data) { return errors.New(c.Tr("settings.uploaded_avatar_not_a_image")) } - if err = ctxUser.UploadAvatar(data); err != nil { - return fmt.Errorf("upload avatar: %v", err) - } - } else { - // No avatar is uploaded but setting has been changed to enable, - // generate a random one when needed. - if ctxUser.UseCustomAvatar && !com.IsFile(userutil.CustomAvatarPath(ctxUser.ID)) { - if err := userutil.GenerateRandomAvatar(ctxUser.ID, ctxUser.Name, ctxUser.Email); err != nil { - log.Error("generate random avatar [%d]: %v", ctxUser.ID, err) - } - } - } - if err := db.UpdateUser(ctxUser); err != nil { - return fmt.Errorf("update user: %v", err) + err = db.Users.UseCustomAvatar(c.Req.Context(), ctxUser.ID, data) + if err != nil { + return errors.Wrap(err, "save avatar") + } + return nil } - return nil } @@ -176,7 +168,8 @@ func SettingsAvatarPost(c *context.Context, f form.Avatar) { } func SettingsDeleteAvatar(c *context.Context) { - if err := c.User.DeleteAvatar(); err != nil { + err := db.Users.DeleteCustomAvatar(c.Req.Context(), c.User.ID) + if err != nil { c.Flash.Error(fmt.Sprintf("Failed to delete avatar: %v", err)) } |