@@ -5,8 +5,10 @@ package user
5
5
6
6
import (
7
7
"context"
8
+ "errors"
8
9
"fmt"
9
10
"io"
11
+ "os"
10
12
11
13
"code.gitea.io/gitea/models/db"
12
14
user_model "code.gitea.io/gitea/models/user"
@@ -48,16 +50,24 @@ func UploadAvatar(ctx context.Context, u *user_model.User, data []byte) error {
48
50
func DeleteAvatar (ctx context.Context , u * user_model.User ) error {
49
51
aPath := u .CustomAvatarRelativePath ()
50
52
log .Trace ("DeleteAvatar[%d]: %s" , u .ID , aPath )
51
- if len (u .Avatar ) > 0 {
52
- if err := storage .Avatars .Delete (aPath ); err != nil {
53
- return fmt .Errorf ("Failed to remove %s: %w" , aPath , err )
53
+
54
+ return db .WithTx (ctx , func (ctx context.Context ) error {
55
+ hasAvatar := len (u .Avatar ) > 0
56
+ u .UseCustomAvatar = false
57
+ u .Avatar = ""
58
+ if _ , err := db .GetEngine (ctx ).ID (u .ID ).Cols ("avatar, use_custom_avatar" ).Update (u ); err != nil {
59
+ return fmt .Errorf ("DeleteAvatar: %w" , err )
54
60
}
55
- }
56
61
57
- u .UseCustomAvatar = false
58
- u .Avatar = ""
59
- if _ , err := db .GetEngine (ctx ).ID (u .ID ).Cols ("avatar, use_custom_avatar" ).Update (u ); err != nil {
60
- return fmt .Errorf ("DeleteAvatar: %w" , err )
61
- }
62
- return nil
62
+ if hasAvatar {
63
+ if err := storage .Avatars .Delete (aPath ); err != nil {
64
+ if ! errors .Is (err , os .ErrNotExist ) {
65
+ return fmt .Errorf ("failed to remove %s: %w" , aPath , err )
66
+ }
67
+ log .Warn ("Deleting avatar %s but it doesn't exist" , aPath )
68
+ }
69
+ }
70
+
71
+ return nil
72
+ })
63
73
}
0 commit comments