Skip to content

Commit

Permalink
Fix bug on avatar (#31008)
Browse files Browse the repository at this point in the history
Co-authored-by: silverwind <me@silverwind.io>
  • Loading branch information
lunny and silverwind authored May 19, 2024
1 parent 0289924 commit 58a03e9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions routers/api/v1/org/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func UpdateAvatar(ctx *context.APIContext) {
err = user_service.UploadAvatar(ctx, ctx.Org.Organization.AsUser(), content)
if err != nil {
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err)
return
}

ctx.Status(http.StatusNoContent)
Expand All @@ -72,6 +73,7 @@ func DeleteAvatar(ctx *context.APIContext) {
err := user_service.DeleteAvatar(ctx, ctx.Org.Organization.AsUser())
if err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err)
return
}

ctx.Status(http.StatusNoContent)
Expand Down
2 changes: 2 additions & 0 deletions routers/api/v1/user/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func UpdateAvatar(ctx *context.APIContext) {
err = user_service.UploadAvatar(ctx, ctx.Doer, content)
if err != nil {
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err)
return
}

ctx.Status(http.StatusNoContent)
Expand All @@ -57,6 +58,7 @@ func DeleteAvatar(ctx *context.APIContext) {
err := user_service.DeleteAvatar(ctx, ctx.Doer)
if err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err)
return
}

ctx.Status(http.StatusNoContent)
Expand Down
30 changes: 20 additions & 10 deletions services/user/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
})
}

0 comments on commit 58a03e9

Please sign in to comment.