Skip to content

Commit 2215840

Browse files
authored
fix avatar bug #1114 (#1122)
This PR fix the avatar bug described in #1114. This will fix random avatar is blank problem and potential delete avatars dir problem.
1 parent 0376029 commit 2215840

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

models/user.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ func (u *User) GenerateRandomAvatar() error {
296296
if err != nil {
297297
return fmt.Errorf("RandomImage: %v", err)
298298
}
299+
// NOTICE for random avatar, it still uses id as avatar name, but custom avatar use md5
300+
// since random image is not a user's photo, there is no security for enumable
301+
u.Avatar = fmt.Sprintf("%d", u.ID)
299302
if err = os.MkdirAll(filepath.Dir(u.CustomAvatarPath()), os.ModePerm); err != nil {
300303
return fmt.Errorf("MkdirAll: %v", err)
301304
}
@@ -451,13 +454,15 @@ func (u *User) UploadAvatar(data []byte) error {
451454
// DeleteAvatar deletes the user's custom avatar.
452455
func (u *User) DeleteAvatar() error {
453456
log.Trace("DeleteAvatar[%d]: %s", u.ID, u.CustomAvatarPath())
454-
455-
if err := os.Remove(u.CustomAvatarPath()); err != nil {
456-
return fmt.Errorf("Failed to remove %s: %v", u.CustomAvatarPath(), err)
457+
if len(u.Avatar) > 0 {
458+
if err := os.Remove(u.CustomAvatarPath()); err != nil {
459+
return fmt.Errorf("Failed to remove %s: %v", u.CustomAvatarPath(), err)
460+
}
457461
}
458462

459463
u.UseCustomAvatar = false
460-
if err := UpdateUser(u); err != nil {
464+
u.Avatar = ""
465+
if _, err := x.Id(u.ID).Cols("avatar, use_custom_avatar").Update(u); err != nil {
461466
return fmt.Errorf("UpdateUser: %v", err)
462467
}
463468
return nil
@@ -994,10 +999,12 @@ func deleteUser(e *xorm.Session, u *User) error {
994999
return fmt.Errorf("Failed to RemoveAll %s: %v", path, err)
9951000
}
9961001

997-
avatarPath := u.CustomAvatarPath()
998-
if com.IsExist(avatarPath) {
999-
if err := os.Remove(avatarPath); err != nil {
1000-
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
1002+
if len(u.Avatar) > 0 {
1003+
avatarPath := u.CustomAvatarPath()
1004+
if com.IsExist(avatarPath) {
1005+
if err := os.Remove(avatarPath); err != nil {
1006+
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
1007+
}
10011008
}
10021009
}
10031010

0 commit comments

Comments
 (0)