修改头像bug

This commit is contained in:
2026-06-21 02:13:02 +08:00
parent 126e1d9234
commit 610ea643ff
+7 -2
View File
@@ -91,7 +91,13 @@ func UpdateProfile(db *gorm.DB, storagePath string) gin.HandlerFunc {
return return
} }
// Use user ID as filename base to avoid duplicates. // Remove old avatar file if it exists (different extension or same).
if user.Avatar != "" {
oldPath := filepath.Join(avatarDir, user.Avatar)
os.Remove(oldPath) // ignore error — file may not exist
}
// Use user ID as filename base.
savedName := fmt.Sprintf("%d%s", user.ID, ext) savedName := fmt.Sprintf("%d%s", user.ID, ext)
savedPath := filepath.Join(avatarDir, savedName) savedPath := filepath.Join(avatarDir, savedName)
@@ -108,7 +114,6 @@ func UpdateProfile(db *gorm.DB, storagePath string) gin.HandlerFunc {
} }
user.Avatar = savedName user.Avatar = savedName
// Update session display immediately.
session.Set("avatar", savedName) session.Set("avatar", savedName)
} }