Feat: file upload handler

This commit is contained in:
HFO4
2019-11-17 13:50:14 +08:00
parent 841832bb65
commit 99e7eecab7
12 changed files with 242 additions and 25 deletions

View File

@@ -2,24 +2,23 @@ package filesystem
import (
"context"
"errors"
)
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
func GenericBeforeUpload(ctx context.Context, fs *FileSystem, file FileData) error {
// 验证单文件尺寸
if !fs.ValidateFileSize(ctx, file.GetSize()) {
return errors.New("单个文件尺寸太大")
}
// 验证并扣除容量
if !fs.ValidateCapacity(ctx, file.GetSize()) {
return errors.New("容量空间不足")
return FileSizeTooBigError
}
// 验证扩展名
if !fs.ValidateExtension(ctx, file.GetFileName()) {
return errors.New("不允许上传此类型的文件")
return FileExtensionNotAllowedError
}
// 验证并扣除容量
if !fs.ValidateCapacity(ctx, file.GetSize()) {
return InsufficientCapacityError
}
return nil
}