From b910254cc5f449f4335acb6d2e1b15002257ed43 Mon Sep 17 00:00:00 2001 From: Aaron Liu <912394456@qq.com> Date: Fri, 7 Apr 2023 19:27:57 +0800 Subject: [PATCH] feat(thumb): delete generated thumb file fix(s3): return empty list of file failed to be deleted --- models/file.go | 3 ++- pkg/filesystem/driver/onedrive/handler.go | 1 + pkg/filesystem/driver/s3/handler.go | 2 +- pkg/filesystem/file.go | 10 ++++++++-- pkg/filesystem/image.go | 17 ++++++++++++----- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/models/file.go b/models/file.go index 9000dbc..788391c 100644 --- a/models/file.go +++ b/models/file.go @@ -42,7 +42,8 @@ const ( ThumbStatusExist = "exist" ThumbStatusNotAvailable = "not_available" - ThumbStatusMetadataKey = "thumb_status" + ThumbStatusMetadataKey = "thumb_status" + ThumbSidecarMetadataKey = "thumb_sidecar" ) func init() { diff --git a/pkg/filesystem/driver/onedrive/handler.go b/pkg/filesystem/driver/onedrive/handler.go index be47d99..1aa3c43 100644 --- a/pkg/filesystem/driver/onedrive/handler.go +++ b/pkg/filesystem/driver/onedrive/handler.go @@ -145,6 +145,7 @@ func (handler Driver) Thumb(ctx context.Context, file *model.File) (*response.Co return nil, errors.New("failed to get thumbnail size") } + return nil, driver.ErrorThumbNotSupported res, err := handler.Client.GetThumbURL(ctx, file.SourceName, thumbSize[0], thumbSize[1]) if err != nil { var apiErr *RespError diff --git a/pkg/filesystem/driver/s3/handler.go b/pkg/filesystem/driver/s3/handler.go index ef88e51..98cb662 100644 --- a/pkg/filesystem/driver/s3/handler.go +++ b/pkg/filesystem/driver/s3/handler.go @@ -257,7 +257,7 @@ func (handler *Driver) Delete(ctx context.Context, files []string) ([]string, er for _, deleteRes := range res.Deleted { deleted = append(deleted, *deleteRes.Key) } - failed = util.SliceDifference(failed, deleted) + failed = util.SliceDifference(files, deleted) return failed, nil diff --git a/pkg/filesystem/file.go b/pkg/filesystem/file.go index d22acb8..4a7f245 100644 --- a/pkg/filesystem/file.go +++ b/pkg/filesystem/file.go @@ -171,6 +171,7 @@ func (fs *FileSystem) deleteGroupedFile(ctx context.Context, files map[uint][]*m // 失败的文件列表 // TODO 并行删除 failed := make(map[uint][]string, len(files)) + thumbs := make([]string, 0) for policyID, toBeDeletedFiles := range files { // 列举出需要物理删除的文件的物理路径 @@ -185,7 +186,11 @@ func (fs *FileSystem) deleteGroupedFile(ctx context.Context, files map[uint][]*m uploadSession := session.(serializer.UploadSession) uploadSessions = append(uploadSessions, &uploadSession) } + } + // Check if sidecar thumb file exist + if model.IsTrueVal(toBeDeletedFiles[i].MetadataSerialized[model.ThumbSidecarMetadataKey]) { + thumbs = append(thumbs, toBeDeletedFiles[i].ThumbFile()) } } @@ -207,8 +212,9 @@ func (fs *FileSystem) deleteGroupedFile(ctx context.Context, files map[uint][]*m } // 执行删除 - failedFile, _ := fs.Handler.Delete(ctx, sourceNamesAll) - failed[policyID] = failedFile + toBeDeletedSrcs := append(sourceNamesAll, thumbs...) + failedFile, _ := fs.Handler.Delete(ctx, toBeDeletedSrcs) + failed[policyID] = util.SliceDifference(failedFile, thumbs) } return failed diff --git a/pkg/filesystem/image.go b/pkg/filesystem/image.go index bcf3b1e..96f03ff 100644 --- a/pkg/filesystem/image.go +++ b/pkg/filesystem/image.go @@ -62,7 +62,7 @@ func (fs *FileSystem) GetThumb(ctx context.Context, id uint) (*response.ContentR } else { // if not exist, generate and upload the sidecar thumb. fs.GenerateThumbnail(ctx, &file) - res, err = fs.Handler.Thumb(ctx, &file) + return fs.GetThumb(ctx, id) } } else { // thumb not supported and proxy is disabled, mark as not available @@ -117,14 +117,15 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) { defer cancel() // TODO: check file size + getThumbWorker().addWorker() + defer getThumbWorker().releaseWorker() + // 获取文件数据 source, err := fs.Handler.Get(newCtx, file.SourceName) if err != nil { return } defer source.Close() - getThumbWorker().addWorker() - defer getThumbWorker().releaseWorker() thumbPath, err := thumb.Generators.Generate(source, file.Name, model.GetSettingByNames( "thumb_width", @@ -186,9 +187,15 @@ func (fs *FileSystem) GenerateThumbnailSize(w, h int) (uint, uint) { func updateThumbStatus(file *model.File, status string) error { if file.Model.ID > 0 { - return file.UpdateMetadata(map[string]string{ + meta := map[string]string{ model.ThumbStatusMetadataKey: status, - }) + } + + if status == model.ThumbStatusExist { + meta[model.ThumbSidecarMetadataKey] = "true" + } + + return file.UpdateMetadata(meta) } else { if file.MetadataSerialized == nil { file.MetadataSerialized = map[string]string{}