@@ -24,13 +24,15 @@ import (
24
24
"fmt"
25
25
"os"
26
26
"path/filepath"
27
+ "strings"
27
28
"time"
28
29
29
30
"github.com/apache/answer/internal/base/constant"
30
31
"github.com/apache/answer/internal/entity"
31
32
"github.com/apache/answer/internal/service/revision"
32
33
"github.com/apache/answer/internal/service/service_config"
33
34
"github.com/apache/answer/internal/service/siteinfo_common"
35
+ usercommon "github.com/apache/answer/internal/service/user_common"
34
36
"github.com/apache/answer/pkg/checker"
35
37
"github.com/apache/answer/pkg/dir"
36
38
"github.com/apache/answer/pkg/writer"
@@ -44,6 +46,7 @@ type FileRecordRepo interface {
44
46
GetFileRecordPage (ctx context.Context , page , pageSize int , cond * entity.FileRecord ) (
45
47
fileRecordList []* entity.FileRecord , total int64 , err error )
46
48
DeleteFileRecord (ctx context.Context , id int ) (err error )
49
+ GetFileRecordByURL (ctx context.Context , fileURL string ) (record * entity.FileRecord , err error )
47
50
}
48
51
49
52
// FileRecordService file record service
@@ -52,6 +55,7 @@ type FileRecordService struct {
52
55
revisionRepo revision.RevisionRepo
53
56
serviceConfig * service_config.ServiceConfig
54
57
siteInfoService siteinfo_common.SiteInfoCommonService
58
+ userService * usercommon.UserCommon
55
59
}
56
60
57
61
// NewFileRecordService new file record service
@@ -60,12 +64,14 @@ func NewFileRecordService(
60
64
revisionRepo revision.RevisionRepo ,
61
65
serviceConfig * service_config.ServiceConfig ,
62
66
siteInfoService siteinfo_common.SiteInfoCommonService ,
67
+ userService * usercommon.UserCommon ,
63
68
) * FileRecordService {
64
69
return & FileRecordService {
65
70
fileRecordRepo : fileRecordRepo ,
66
71
revisionRepo : revisionRepo ,
67
72
serviceConfig : serviceConfig ,
68
73
siteInfoService : siteInfoService ,
74
+ userService : userService ,
69
75
}
70
76
}
71
77
@@ -104,6 +110,21 @@ func (fs *FileRecordService) CleanOrphanUploadFiles(ctx context.Context) {
104
110
if fileRecord .CreatedAt .AddDate (0 , 0 , 2 ).After (time .Now ()) {
105
111
continue
106
112
}
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
+ }
107
128
if checker .IsNotZeroString (fileRecord .ObjectID ) {
108
129
_ , exist , err := fs .revisionRepo .GetLastRevisionByObjectID (ctx , fileRecord .ObjectID )
109
130
if err != nil {
@@ -129,14 +150,18 @@ func (fs *FileRecordService) CleanOrphanUploadFiles(ctx context.Context) {
129
150
}
130
151
}
131
152
// Delete and move the file record
132
- if err := fs .deleteAndMoveFileRecord (ctx , fileRecord ); err != nil {
153
+ if err := fs .DeleteAndMoveFileRecord (ctx , fileRecord ); err != nil {
133
154
log .Error (err )
134
155
}
135
156
}
136
157
page ++
137
158
}
138
159
}
139
160
161
+ func isBrandingOrAvatarFile (filePath string ) bool {
162
+ return strings .Contains (filePath , constant .BrandingSubPath + "/" ) || strings .Contains (filePath , constant .AvatarSubPath + "/" )
163
+ }
164
+
140
165
func (fs * FileRecordService ) PurgeDeletedFiles (ctx context.Context ) {
141
166
deletedPath := filepath .Join (fs .serviceConfig .UploadPath , constant .DeletedSubPath )
142
167
log .Infof ("purge deleted files: %s" , deletedPath )
@@ -152,7 +177,7 @@ func (fs *FileRecordService) PurgeDeletedFiles(ctx context.Context) {
152
177
return
153
178
}
154
179
155
- func (fs * FileRecordService ) deleteAndMoveFileRecord (ctx context.Context , fileRecord * entity.FileRecord ) error {
180
+ func (fs * FileRecordService ) DeleteAndMoveFileRecord (ctx context.Context , fileRecord * entity.FileRecord ) error {
156
181
// Delete the file record
157
182
if err := fs .fileRecordRepo .DeleteFileRecord (ctx , fileRecord .ID ); err != nil {
158
183
return fmt .Errorf ("delete file record error: %v" , err )
@@ -170,3 +195,12 @@ func (fs *FileRecordService) deleteAndMoveFileRecord(ctx context.Context, fileRe
170
195
log .Debugf ("delete and move file: %s" , fileRecord .FileURL )
171
196
return nil
172
197
}
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