feat(thumb): delete generated thumb file

fix(s3): return empty list of file failed to be deleted
pull/1690/head
Aaron Liu 2 years ago
parent e115497dfe
commit b910254cc5

@ -42,7 +42,8 @@ const (
ThumbStatusExist = "exist" ThumbStatusExist = "exist"
ThumbStatusNotAvailable = "not_available" ThumbStatusNotAvailable = "not_available"
ThumbStatusMetadataKey = "thumb_status" ThumbStatusMetadataKey = "thumb_status"
ThumbSidecarMetadataKey = "thumb_sidecar"
) )
func init() { func init() {

@ -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, errors.New("failed to get thumbnail size")
} }
return nil, driver.ErrorThumbNotSupported
res, err := handler.Client.GetThumbURL(ctx, file.SourceName, thumbSize[0], thumbSize[1]) res, err := handler.Client.GetThumbURL(ctx, file.SourceName, thumbSize[0], thumbSize[1])
if err != nil { if err != nil {
var apiErr *RespError var apiErr *RespError

@ -257,7 +257,7 @@ func (handler *Driver) Delete(ctx context.Context, files []string) ([]string, er
for _, deleteRes := range res.Deleted { for _, deleteRes := range res.Deleted {
deleted = append(deleted, *deleteRes.Key) deleted = append(deleted, *deleteRes.Key)
} }
failed = util.SliceDifference(failed, deleted) failed = util.SliceDifference(files, deleted)
return failed, nil return failed, nil

@ -171,6 +171,7 @@ func (fs *FileSystem) deleteGroupedFile(ctx context.Context, files map[uint][]*m
// 失败的文件列表 // 失败的文件列表
// TODO 并行删除 // TODO 并行删除
failed := make(map[uint][]string, len(files)) failed := make(map[uint][]string, len(files))
thumbs := make([]string, 0)
for policyID, toBeDeletedFiles := range files { for policyID, toBeDeletedFiles := range files {
// 列举出需要物理删除的文件的物理路径 // 列举出需要物理删除的文件的物理路径
@ -185,7 +186,11 @@ func (fs *FileSystem) deleteGroupedFile(ctx context.Context, files map[uint][]*m
uploadSession := session.(serializer.UploadSession) uploadSession := session.(serializer.UploadSession)
uploadSessions = append(uploadSessions, &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) toBeDeletedSrcs := append(sourceNamesAll, thumbs...)
failed[policyID] = failedFile failedFile, _ := fs.Handler.Delete(ctx, toBeDeletedSrcs)
failed[policyID] = util.SliceDifference(failedFile, thumbs)
} }
return failed return failed

@ -62,7 +62,7 @@ func (fs *FileSystem) GetThumb(ctx context.Context, id uint) (*response.ContentR
} else { } else {
// if not exist, generate and upload the sidecar thumb. // if not exist, generate and upload the sidecar thumb.
fs.GenerateThumbnail(ctx, &file) fs.GenerateThumbnail(ctx, &file)
res, err = fs.Handler.Thumb(ctx, &file) return fs.GetThumb(ctx, id)
} }
} else { } else {
// thumb not supported and proxy is disabled, mark as not available // 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() defer cancel()
// TODO: check file size // TODO: check file size
getThumbWorker().addWorker()
defer getThumbWorker().releaseWorker()
// 获取文件数据 // 获取文件数据
source, err := fs.Handler.Get(newCtx, file.SourceName) source, err := fs.Handler.Get(newCtx, file.SourceName)
if err != nil { if err != nil {
return return
} }
defer source.Close() defer source.Close()
getThumbWorker().addWorker()
defer getThumbWorker().releaseWorker()
thumbPath, err := thumb.Generators.Generate(source, file.Name, model.GetSettingByNames( thumbPath, err := thumb.Generators.Generate(source, file.Name, model.GetSettingByNames(
"thumb_width", "thumb_width",
@ -186,9 +187,15 @@ func (fs *FileSystem) GenerateThumbnailSize(w, h int) (uint, uint) {
func updateThumbStatus(file *model.File, status string) error { func updateThumbStatus(file *model.File, status string) error {
if file.Model.ID > 0 { if file.Model.ID > 0 {
return file.UpdateMetadata(map[string]string{ meta := map[string]string{
model.ThumbStatusMetadataKey: status, model.ThumbStatusMetadataKey: status,
}) }
if status == model.ThumbStatusExist {
meta[model.ThumbSidecarMetadataKey] = "true"
}
return file.UpdateMetadata(meta)
} else { } else {
if file.MetadataSerialized == nil { if file.MetadataSerialized == nil {
file.MetadataSerialized = map[string]string{} file.MetadataSerialized = map[string]string{}

Loading…
Cancel
Save