parent
003274162b
commit
79caf635f9
@ -0,0 +1,22 @@
|
|||||||
|
package filesystem
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
|
||||||
|
func GenericBeforeUpload(fs *FileSystem, file FileData) error {
|
||||||
|
// 验证单文件尺寸
|
||||||
|
if !fs.ValidateFileSize(file.GetSize()) {
|
||||||
|
return errors.New("单个文件尺寸太大")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证并扣除容量
|
||||||
|
if !fs.ValidateCapacity(file.GetSize()) {
|
||||||
|
return errors.New("容量空间不足")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证扩展名
|
||||||
|
if !fs.ValidateExtension(file.GetFileName()) {
|
||||||
|
return errors.New("不允许上传此类型的文件")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package filesystem
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cloudreve/pkg/util"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ValidateFileSize 验证上传的文件大小是否超出限制
|
||||||
|
func (fs *FileSystem) ValidateFileSize(size uint64) bool {
|
||||||
|
return size <= fs.User.Policy.MaxSize
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateCapacity 验证并扣除用户容量
|
||||||
|
func (fs *FileSystem) ValidateCapacity(size uint64) bool {
|
||||||
|
if fs.User.DeductionCapacity(size) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateExtension 验证文件扩展名
|
||||||
|
func (fs *FileSystem) ValidateExtension(fileName string) bool {
|
||||||
|
// 不需要验证
|
||||||
|
if len(fs.User.Policy.OptionsSerialized.FileType) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
ext := filepath.Ext(fileName)
|
||||||
|
|
||||||
|
// 无扩展名时
|
||||||
|
if len(ext) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if util.ContainsString(fs.User.Policy.OptionsSerialized.FileType, ext[1:]) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
Loading…
Reference in new issue