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

@ -43,6 +43,7 @@ const (
ThumbStatusNotAvailable = "not_available"
ThumbStatusMetadataKey = "thumb_status"
ThumbSidecarMetadataKey = "thumb_sidecar"
)
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, driver.ErrorThumbNotSupported
res, err := handler.Client.GetThumbURL(ctx, file.SourceName, thumbSize[0], thumbSize[1])
if err != nil {
var apiErr *RespError

@ -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

@ -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

@ -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{}

Loading…
Cancel
Save