Fix bug on avatar (#31008)
Co-authored-by: silverwind <me@silverwind.io> (cherry picked from commit 58a03e9fadb345de5653345c2a68ecfd0750940a)
This commit is contained in:
parent
99f29e59a1
commit
1be797faba
3 changed files with 25 additions and 11 deletions
|
@ -46,6 +46,7 @@ func UpdateAvatar(ctx *context.APIContext) {
|
||||||
err = user_service.UploadAvatar(ctx, ctx.Org.Organization.AsUser(), content)
|
err = user_service.UploadAvatar(ctx, ctx.Org.Organization.AsUser(), content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err)
|
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Status(http.StatusNoContent)
|
ctx.Status(http.StatusNoContent)
|
||||||
|
@ -72,6 +73,7 @@ func DeleteAvatar(ctx *context.APIContext) {
|
||||||
err := user_service.DeleteAvatar(ctx, ctx.Org.Organization.AsUser())
|
err := user_service.DeleteAvatar(ctx, ctx.Org.Organization.AsUser())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err)
|
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Status(http.StatusNoContent)
|
ctx.Status(http.StatusNoContent)
|
||||||
|
|
|
@ -39,6 +39,7 @@ func UpdateAvatar(ctx *context.APIContext) {
|
||||||
err = user_service.UploadAvatar(ctx, ctx.Doer, content)
|
err = user_service.UploadAvatar(ctx, ctx.Doer, content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err)
|
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Status(http.StatusNoContent)
|
ctx.Status(http.StatusNoContent)
|
||||||
|
@ -57,6 +58,7 @@ func DeleteAvatar(ctx *context.APIContext) {
|
||||||
err := user_service.DeleteAvatar(ctx, ctx.Doer)
|
err := user_service.DeleteAvatar(ctx, ctx.Doer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err)
|
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Status(http.StatusNoContent)
|
ctx.Status(http.StatusNoContent)
|
||||||
|
|
|
@ -5,8 +5,10 @@ package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
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 {
|
func DeleteAvatar(ctx context.Context, u *user_model.User) error {
|
||||||
aPath := u.CustomAvatarRelativePath()
|
aPath := u.CustomAvatarRelativePath()
|
||||||
log.Trace("DeleteAvatar[%d]: %s", u.ID, aPath)
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
u.UseCustomAvatar = false
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
||||||
u.Avatar = ""
|
hasAvatar := len(u.Avatar) > 0
|
||||||
if _, err := db.GetEngine(ctx).ID(u.ID).Cols("avatar, use_custom_avatar").Update(u); err != nil {
|
u.UseCustomAvatar = false
|
||||||
return fmt.Errorf("DeleteAvatar: %w", err)
|
u.Avatar = ""
|
||||||
}
|
if _, err := db.GetEngine(ctx).ID(u.ID).Cols("avatar, use_custom_avatar").Update(u); err != nil {
|
||||||
return nil
|
return fmt.Errorf("DeleteAvatar: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue