diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2024-05-19 06:58:39 +0200 |
---|---|---|
committer | Earl Warren <contact@earl-warren.org> | 2024-06-05 08:04:10 +0200 |
commit | 32d8ada0e712070615b22aabe4ef01b65e7b0133 (patch) | |
tree | 330a4534e52508c4191b8bdb66debd21253f7f9e /services/user/avatar.go | |
parent | test(avatar): deleting a user avatar and file is atomic (diff) | |
download | forgejo-32d8ada0e712070615b22aabe4ef01b65e7b0133.tar.xz forgejo-32d8ada0e712070615b22aabe4ef01b65e7b0133.zip |
Fix bug on avatar (#31008)
Co-authored-by: silverwind <me@silverwind.io>
(cherry picked from commit 58a03e9fadb345de5653345c2a68ecfd0750940a)
(cherry picked from commit 1be797faba301503e29db9de7eb32335a684464c)
Diffstat (limited to '')
-rw-r--r-- | services/user/avatar.go | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/services/user/avatar.go b/services/user/avatar.go index 2d6c3faf9a..3f87466eaa 100644 --- a/services/user/avatar.go +++ b/services/user/avatar.go @@ -5,8 +5,10 @@ package user import ( "context" + "errors" "fmt" "io" + "os" "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" @@ -48,16 +50,24 @@ func UploadAvatar(ctx context.Context, u *user_model.User, data []byte) error { func DeleteAvatar(ctx context.Context, u *user_model.User) error { aPath := u.CustomAvatarRelativePath() log.Trace("DeleteAvatar[%d]: %s", u.ID, aPath) - if len(u.Avatar) > 0 { - if err := storage.Avatars.Delete(aPath); err != nil { - return fmt.Errorf("Failed to remove %s: %w", aPath, err) + + return db.WithTx(ctx, func(ctx context.Context) error { + hasAvatar := len(u.Avatar) > 0 + u.UseCustomAvatar = false + u.Avatar = "" + if _, err := db.GetEngine(ctx).ID(u.ID).Cols("avatar, use_custom_avatar").Update(u); err != nil { + return fmt.Errorf("DeleteAvatar: %w", err) } - } - u.UseCustomAvatar = false - u.Avatar = "" - if _, err := db.GetEngine(ctx).ID(u.ID).Cols("avatar, use_custom_avatar").Update(u); err != nil { - return fmt.Errorf("DeleteAvatar: %w", err) - } - return nil + if hasAvatar { + if err := storage.Avatars.Delete(aPath); err != nil { + if !errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("failed to remove %s: %w", aPath, err) + } + log.Warn("Deleting avatar %s but it doesn't exist", aPath) + } + } + + return nil + }) } |