Feat: recycling file storage and user capacity when uploading canceled

This commit is contained in:
HFO4
2019-11-18 14:06:15 +08:00
parent 160f964564
commit 631c23f065
9 changed files with 93 additions and 14 deletions

View File

@@ -2,6 +2,7 @@ package filesystem
import (
"context"
"errors"
)
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
@@ -27,3 +28,19 @@ func GenericBeforeUpload(ctx context.Context, fs *FileSystem, file FileData) err
}
return nil
}
// GenericAfterUploadCanceled 通用上传取消处理钩子,包含数据库操作
func GenericAfterUploadCanceled(ctx context.Context, fs *FileSystem, file FileData) error {
filePath := ctx.Value("path").(string)
// 删除临时文件
_, err := fs.Handler.Delete(ctx, []string{filePath})
if err != nil {
return err
}
// 归还用户容量
if !fs.User.DeductionStorage(file.GetSize()) {
return errors.New("无法继续降低用户已用存储")
}
return nil
}