diff --git a/handlers/profile.go b/handlers/profile.go index 618fe3c..e314609 100644 --- a/handlers/profile.go +++ b/handlers/profile.go @@ -91,7 +91,13 @@ func UpdateProfile(db *gorm.DB, storagePath string) gin.HandlerFunc { 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) savedPath := filepath.Join(avatarDir, savedName) @@ -108,7 +114,6 @@ func UpdateProfile(db *gorm.DB, storagePath string) gin.HandlerFunc { } user.Avatar = savedName - // Update session display immediately. session.Set("avatar", savedName) }