@@ -33,6 +33,7 @@ import (
3333 "github.com/apache/answer/internal/schema"
3434 "github.com/apache/answer/internal/service/config"
3535 "github.com/apache/answer/internal/service/export"
36+ "github.com/apache/answer/internal/service/file_record"
3637 questioncommon "github.com/apache/answer/internal/service/question_common"
3738 "github.com/apache/answer/internal/service/siteinfo_common"
3839 tagcommon "github.com/apache/answer/internal/service/tag_common"
@@ -49,6 +50,7 @@ type SiteInfoService struct {
4950 tagCommonService * tagcommon.TagCommonService
5051 configService * config.ConfigService
5152 questioncommon * questioncommon.QuestionCommon
53+ fileRecordService * file_record.FileRecordService
5254}
5355
5456func NewSiteInfoService (
@@ -58,6 +60,7 @@ func NewSiteInfoService(
5860 tagCommonService * tagcommon.TagCommonService ,
5961 configService * config.ConfigService ,
6062 questioncommon * questioncommon.QuestionCommon ,
63+ fileRecordService * file_record.FileRecordService ,
6164
6265) * SiteInfoService {
6366 plugin .RegisterGetSiteURLFunc (func () string {
@@ -76,6 +79,7 @@ func NewSiteInfoService(
7679 tagCommonService : tagCommonService ,
7780 configService : configService ,
7881 questioncommon : questioncommon ,
82+ fileRecordService : fileRecordService ,
7983 }
8084}
8185
@@ -438,3 +442,43 @@ func (s *SiteInfoService) UpdatePrivilegesConfig(ctx context.Context, req *schem
438442 }
439443 return
440444}
445+
446+ func (s * SiteInfoService ) CleanUpRemovedBrandingFiles (
447+ ctx context.Context ,
448+ newBranding * schema.SiteBrandingReq ,
449+ currentBranding * schema.SiteBrandingResp ,
450+ ) error {
451+ currentFiles := map [string ]string {
452+ "logo" : currentBranding .Logo ,
453+ "mobile_logo" : currentBranding .MobileLogo ,
454+ "square_icon" : currentBranding .SquareIcon ,
455+ "favicon" : currentBranding .Favicon ,
456+ }
457+
458+ newFiles := map [string ]string {
459+ "logo" : newBranding .Logo ,
460+ "mobile_logo" : newBranding .MobileLogo ,
461+ "square_icon" : newBranding .SquareIcon ,
462+ "favicon" : newBranding .Favicon ,
463+ }
464+
465+ for key , currentFile := range currentFiles {
466+ newFile := newFiles [key ]
467+ if currentFile != "" && currentFile != newFile {
468+ fileRecord , err := s .fileRecordService .GetFileRecordByURL (ctx , currentFile )
469+ if err != nil {
470+ log .Error (err )
471+ continue
472+ }
473+ if fileRecord == nil {
474+ log .Error ("could not fetch file record by url" )
475+ continue
476+ }
477+ if err := s .fileRecordService .DeleteAndMoveFileRecord (ctx , fileRecord ); err != nil {
478+ log .Error (err )
479+ }
480+ }
481+ }
482+
483+ return nil
484+ }
0 commit comments