@@ -24,13 +24,15 @@ import (
2424 "fmt"
2525 "os"
2626 "path/filepath"
27+ "strings"
2728 "time"
2829
2930 "github.com/apache/answer/internal/base/constant"
3031 "github.com/apache/answer/internal/entity"
3132 "github.com/apache/answer/internal/service/revision"
3233 "github.com/apache/answer/internal/service/service_config"
3334 "github.com/apache/answer/internal/service/siteinfo_common"
35+ usercommon "github.com/apache/answer/internal/service/user_common"
3436 "github.com/apache/answer/pkg/checker"
3537 "github.com/apache/answer/pkg/dir"
3638 "github.com/apache/answer/pkg/writer"
@@ -44,6 +46,7 @@ type FileRecordRepo interface {
4446 GetFileRecordPage (ctx context.Context , page , pageSize int , cond * entity.FileRecord ) (
4547 fileRecordList []* entity.FileRecord , total int64 , err error )
4648 DeleteFileRecord (ctx context.Context , id int ) (err error )
49+ GetFileRecordByURL (ctx context.Context , fileURL string ) (record * entity.FileRecord , err error )
4750}
4851
4952// FileRecordService file record service
@@ -52,6 +55,7 @@ type FileRecordService struct {
5255 revisionRepo revision.RevisionRepo
5356 serviceConfig * service_config.ServiceConfig
5457 siteInfoService siteinfo_common.SiteInfoCommonService
58+ userService * usercommon.UserCommon
5559}
5660
5761// NewFileRecordService new file record service
@@ -60,12 +64,14 @@ func NewFileRecordService(
6064 revisionRepo revision.RevisionRepo ,
6165 serviceConfig * service_config.ServiceConfig ,
6266 siteInfoService siteinfo_common.SiteInfoCommonService ,
67+ userService * usercommon.UserCommon ,
6368) * FileRecordService {
6469 return & FileRecordService {
6570 fileRecordRepo : fileRecordRepo ,
6671 revisionRepo : revisionRepo ,
6772 serviceConfig : serviceConfig ,
6873 siteInfoService : siteInfoService ,
74+ userService : userService ,
6975 }
7076}
7177
@@ -104,6 +110,21 @@ func (fs *FileRecordService) CleanOrphanUploadFiles(ctx context.Context) {
104110 if fileRecord .CreatedAt .AddDate (0 , 0 , 2 ).After (time .Now ()) {
105111 continue
106112 }
113+ if isBrandingOrAvatarFile (fileRecord .FilePath ) {
114+ if strings .Contains (fileRecord .FilePath , constant .BrandingSubPath + "/" ) {
115+ if fs .siteInfoService .IsBrandingFileUsed (ctx , fileRecord .FilePath ) {
116+ continue
117+ }
118+ } else if strings .Contains (fileRecord .FilePath , constant .AvatarSubPath + "/" ) {
119+ if fs .userService .IsAvatarFileUsed (ctx , fileRecord .FilePath ) {
120+ continue
121+ }
122+ }
123+ if err := fs .DeleteAndMoveFileRecord (ctx , fileRecord ); err != nil {
124+ log .Error (err )
125+ }
126+ continue
127+ }
107128 if checker .IsNotZeroString (fileRecord .ObjectID ) {
108129 _ , exist , err := fs .revisionRepo .GetLastRevisionByObjectID (ctx , fileRecord .ObjectID )
109130 if err != nil {
@@ -129,14 +150,18 @@ func (fs *FileRecordService) CleanOrphanUploadFiles(ctx context.Context) {
129150 }
130151 }
131152 // Delete and move the file record
132- if err := fs .deleteAndMoveFileRecord (ctx , fileRecord ); err != nil {
153+ if err := fs .DeleteAndMoveFileRecord (ctx , fileRecord ); err != nil {
133154 log .Error (err )
134155 }
135156 }
136157 page ++
137158 }
138159}
139160
161+ func isBrandingOrAvatarFile (filePath string ) bool {
162+ return strings .Contains (filePath , constant .BrandingSubPath + "/" ) || strings .Contains (filePath , constant .AvatarSubPath + "/" )
163+ }
164+
140165func (fs * FileRecordService ) PurgeDeletedFiles (ctx context.Context ) {
141166 deletedPath := filepath .Join (fs .serviceConfig .UploadPath , constant .DeletedSubPath )
142167 log .Infof ("purge deleted files: %s" , deletedPath )
@@ -152,7 +177,7 @@ func (fs *FileRecordService) PurgeDeletedFiles(ctx context.Context) {
152177 return
153178}
154179
155- func (fs * FileRecordService ) deleteAndMoveFileRecord (ctx context.Context , fileRecord * entity.FileRecord ) error {
180+ func (fs * FileRecordService ) DeleteAndMoveFileRecord (ctx context.Context , fileRecord * entity.FileRecord ) error {
156181 // Delete the file record
157182 if err := fs .fileRecordRepo .DeleteFileRecord (ctx , fileRecord .ID ); err != nil {
158183 return fmt .Errorf ("delete file record error: %v" , err )
@@ -170,3 +195,12 @@ func (fs *FileRecordService) deleteAndMoveFileRecord(ctx context.Context, fileRe
170195 log .Debugf ("delete and move file: %s" , fileRecord .FileURL )
171196 return nil
172197}
198+
199+ func (fs * FileRecordService ) GetFileRecordByURL (ctx context.Context , fileURL string ) (record * entity.FileRecord , err error ) {
200+ record , err = fs .fileRecordRepo .GetFileRecordByURL (ctx , fileURL )
201+ if err != nil {
202+ log .Errorf ("error retrieving file record by URL: %v" , err )
203+ return
204+ }
205+ return
206+ }
0 commit comments