2019-11-16 16:05:10 +08:00
|
|
|
package filesystem
|
|
|
|
|
2019-11-16 20:31:34 +08:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
2019-11-16 16:05:10 +08:00
|
|
|
|
|
|
|
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
|
2019-11-16 20:31:34 +08:00
|
|
|
func GenericBeforeUpload(ctx context.Context, fs *FileSystem, file FileData) error {
|
2019-11-16 16:05:10 +08:00
|
|
|
// 验证单文件尺寸
|
2019-11-16 20:31:34 +08:00
|
|
|
if !fs.ValidateFileSize(ctx, file.GetSize()) {
|
2019-11-17 13:50:14 +08:00
|
|
|
return FileSizeTooBigError
|
2019-11-16 16:05:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 验证扩展名
|
2019-11-16 20:31:34 +08:00
|
|
|
if !fs.ValidateExtension(ctx, file.GetFileName()) {
|
2019-11-17 13:50:14 +08:00
|
|
|
return FileExtensionNotAllowedError
|
|
|
|
}
|
|
|
|
|
|
|
|
// 验证并扣除容量
|
|
|
|
if !fs.ValidateCapacity(ctx, file.GetSize()) {
|
|
|
|
return InsufficientCapacityError
|
2019-11-16 16:05:10 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|