From 610ea643ffb8c22dd926dde1d963e8f37e3aca88 Mon Sep 17 00:00:00 2001 From: kevin Date: Sun, 21 Jun 2026 02:13:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=B4=E5=83=8Fbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handlers/profile.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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) }