@@ -23,9 +23,10 @@ import (
2323 "context"
2424 "encoding/json"
2525 "fmt"
26+ "time"
27+
2628 "github.com/apache/answer/internal/service/event_queue"
2729 "github.com/apache/answer/pkg/token"
28- "time"
2930
3031 "github.com/apache/answer/internal/base/constant"
3132 questioncommon "github.com/apache/answer/internal/service/question_common"
@@ -41,6 +42,7 @@ import (
4142 "github.com/apache/answer/internal/service/activity_common"
4243 "github.com/apache/answer/internal/service/auth"
4344 "github.com/apache/answer/internal/service/export"
45+ "github.com/apache/answer/internal/service/file_record"
4446 "github.com/apache/answer/internal/service/role"
4547 "github.com/apache/answer/internal/service/siteinfo_common"
4648 usercommon "github.com/apache/answer/internal/service/user_common"
@@ -67,6 +69,7 @@ type UserService struct {
6769 userNotificationConfigService * user_notification_config.UserNotificationConfigService
6870 questionService * questioncommon.QuestionCommon
6971 eventQueueService event_queue.EventQueueService
72+ fileRecordService * file_record.FileRecordService
7073}
7174
7275func NewUserService (userRepo usercommon.UserRepo ,
@@ -82,6 +85,7 @@ func NewUserService(userRepo usercommon.UserRepo,
8285 userNotificationConfigService * user_notification_config.UserNotificationConfigService ,
8386 questionService * questioncommon.QuestionCommon ,
8487 eventQueueService event_queue.EventQueueService ,
88+ fileRecordService * file_record.FileRecordService ,
8589) * UserService {
8690 return & UserService {
8791 userCommonService : userCommonService ,
@@ -97,6 +101,7 @@ func NewUserService(userRepo usercommon.UserRepo,
97101 userNotificationConfigService : userNotificationConfigService ,
98102 questionService : questionService ,
99103 eventQueueService : eventQueueService ,
104+ fileRecordService : fileRecordService ,
100105 }
101106}
102107
@@ -355,6 +360,9 @@ func (us *UserService) UpdateInfo(ctx context.Context, req *schema.UpdateInfoReq
355360 }
356361
357362 cond := us .formatUserInfoForUpdateInfo (oldUserInfo , req , siteUsers )
363+
364+ us .cleanUpRemovedAvatar (ctx , oldUserInfo .Avatar , cond .Avatar )
365+
358366 err = us .userRepo .UpdateInfo (ctx , cond )
359367 if err != nil {
360368 return nil , err
@@ -363,6 +371,42 @@ func (us *UserService) UpdateInfo(ctx context.Context, req *schema.UpdateInfoReq
363371 return nil , err
364372}
365373
374+ func (us * UserService ) cleanUpRemovedAvatar (
375+ ctx context.Context ,
376+ oldAvatarJSON string ,
377+ newAvatarJSON string ,
378+ ) {
379+ if oldAvatarJSON == newAvatarJSON {
380+ return
381+ }
382+
383+ var oldAvatar , newAvatar struct {
384+ Type string `json:"type"`
385+ Custom string `json:"custom"`
386+ }
387+
388+ _ = json .Unmarshal ([]byte (oldAvatarJSON ), & oldAvatar )
389+ _ = json .Unmarshal ([]byte (newAvatarJSON ), & newAvatar )
390+
391+ // clean up if old is custom and it's either removed or replaced
392+ if oldAvatar .Type == "custom" && (newAvatar .Type != "custom" || oldAvatar .Custom != newAvatar .Custom ) {
393+ if oldAvatar .Custom != "" {
394+ fileRecord , err := us .fileRecordService .GetFileRecordByURL (ctx , oldAvatar .Custom )
395+ if err != nil {
396+ log .Error (err )
397+ return
398+ }
399+ if fileRecord == nil {
400+ log .Warn ("no file record found for old avatar url:" , oldAvatar .Custom )
401+ return
402+ }
403+ if err := us .fileRecordService .DeleteAndMoveFileRecord (ctx , fileRecord ); err != nil {
404+ log .Error (err )
405+ }
406+ }
407+ }
408+ }
409+
366410func (us * UserService ) formatUserInfoForUpdateInfo (
367411 oldUserInfo * entity.User , req * schema.UpdateInfoRequest , siteUsersConf * schema.SiteUsersResp ) * entity.User {
368412 avatar , _ := json .Marshal (req .Avatar )
0 commit comments