Skip to content

Commit f061bdd

Browse files
author
Daniel Auer
committed
feat: delete branding files
+ a file is being deleted after its reference has been removed from the site info. The user has no way to access the file again. Therefore the file would become an orphan if not deleted. + an if clause had to be added to CleanOrphanUploadFiles, because it would otherwise remove the branding and avatar files.
1 parent 2dc8368 commit f061bdd

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

internal/controller_admin/siteinfo_controller.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/apache/answer/internal/schema"
2929
"github.com/apache/answer/internal/service/siteinfo"
3030
"github.com/gin-gonic/gin"
31+
"github.com/segmentfault/pacman/log"
3132
)
3233

3334
// SiteInfoController site info controller
@@ -274,7 +275,12 @@ func (sc *SiteInfoController) UpdateBranding(ctx *gin.Context) {
274275
if handler.BindAndCheck(ctx, req) {
275276
return
276277
}
277-
err := sc.siteInfoService.SaveSiteBranding(ctx, req)
278+
currentBranding, _ := sc.siteInfoService.GetSiteBranding(ctx)
279+
err := sc.siteInfoService.CleanUpRemovedBrandingFiles(ctx, req, currentBranding)
280+
if err != nil {
281+
log.Error(err)
282+
}
283+
err = sc.siteInfoService.SaveSiteBranding(ctx, req)
278284
handler.HandleResponse(ctx, err, nil)
279285
}
280286

internal/repo/file_record/file_record_repo.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,18 @@ func (fr *fileRecordRepo) UpdateFileRecord(ctx context.Context, fileRecord *enti
8282
}
8383
return
8484
}
85+
86+
// GetFileRecordByURL gets a file record by its url
87+
func (fr *fileRecordRepo) GetFileRecordByURL(ctx context.Context, fileURL string) (record *entity.FileRecord, err error) {
88+
record = &entity.FileRecord{}
89+
session := fr.data.DB.Context(ctx)
90+
exists, err := session.Where("file_url = ? AND status = ?", fileURL, entity.FileRecordStatusAvailable).Get(record)
91+
if err != nil {
92+
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
93+
return
94+
}
95+
if !exists {
96+
return
97+
}
98+
return record, nil
99+
}

internal/service/file_record/file_record_service.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"os"
2626
"path/filepath"
27+
"strings"
2728
"time"
2829

2930
"github.com/apache/answer/internal/base/constant"
@@ -44,6 +45,7 @@ type FileRecordRepo interface {
4445
GetFileRecordPage(ctx context.Context, page, pageSize int, cond *entity.FileRecord) (
4546
fileRecordList []*entity.FileRecord, total int64, err error)
4647
DeleteFileRecord(ctx context.Context, id int) (err error)
48+
GetFileRecordByURL(ctx context.Context, fileURL string) (record *entity.FileRecord, err error)
4749
}
4850

4951
// FileRecordService file record service
@@ -104,6 +106,9 @@ func (fs *FileRecordService) CleanOrphanUploadFiles(ctx context.Context) {
104106
if fileRecord.CreatedAt.AddDate(0, 0, 2).After(time.Now()) {
105107
continue
106108
}
109+
if isBrandingOrAvatarFile(fileRecord.FilePath) {
110+
continue
111+
}
107112
if checker.IsNotZeroString(fileRecord.ObjectID) {
108113
_, exist, err := fs.revisionRepo.GetLastRevisionByObjectID(ctx, fileRecord.ObjectID)
109114
if err != nil {
@@ -129,14 +134,18 @@ func (fs *FileRecordService) CleanOrphanUploadFiles(ctx context.Context) {
129134
}
130135
}
131136
// Delete and move the file record
132-
if err := fs.deleteAndMoveFileRecord(ctx, fileRecord); err != nil {
137+
if err := fs.DeleteAndMoveFileRecord(ctx, fileRecord); err != nil {
133138
log.Error(err)
134139
}
135140
}
136141
page++
137142
}
138143
}
139144

145+
func isBrandingOrAvatarFile(filePath string) bool {
146+
return strings.Contains(filePath, constant.BrandingSubPath+"/") || strings.Contains(filePath, constant.AvatarSubPath+"/")
147+
}
148+
140149
func (fs *FileRecordService) PurgeDeletedFiles(ctx context.Context) {
141150
deletedPath := filepath.Join(fs.serviceConfig.UploadPath, constant.DeletedSubPath)
142151
log.Infof("purge deleted files: %s", deletedPath)
@@ -152,7 +161,7 @@ func (fs *FileRecordService) PurgeDeletedFiles(ctx context.Context) {
152161
return
153162
}
154163

155-
func (fs *FileRecordService) deleteAndMoveFileRecord(ctx context.Context, fileRecord *entity.FileRecord) error {
164+
func (fs *FileRecordService) DeleteAndMoveFileRecord(ctx context.Context, fileRecord *entity.FileRecord) error {
156165
// Delete the file record
157166
if err := fs.fileRecordRepo.DeleteFileRecord(ctx, fileRecord.ID); err != nil {
158167
return fmt.Errorf("delete file record error: %v", err)
@@ -170,3 +179,12 @@ func (fs *FileRecordService) deleteAndMoveFileRecord(ctx context.Context, fileRe
170179
log.Debugf("delete and move file: %s", fileRecord.FileURL)
171180
return nil
172181
}
182+
183+
func (fs *FileRecordService) GetFileRecordByURL(ctx context.Context, fileURL string) (record *entity.FileRecord, err error) {
184+
record, err = fs.fileRecordRepo.GetFileRecordByURL(ctx, fileURL)
185+
if err != nil {
186+
log.Errorf("error retrieving file record by URL: %v", err)
187+
return
188+
}
189+
return
190+
}

internal/service/siteinfo/siteinfo_service.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5456
func 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

Comments
 (0)