mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: basic file validator
This commit is contained in:
@@ -11,18 +11,37 @@ type FileData interface {
|
||||
io.Closer
|
||||
GetSize() uint64
|
||||
GetMIMEType() string
|
||||
GetFileName() string
|
||||
}
|
||||
|
||||
// FileSystem 管理文件的文件系统
|
||||
type FileSystem struct {
|
||||
// 文件系统所有者
|
||||
/*
|
||||
文件系统所有者
|
||||
*/
|
||||
User *model.User
|
||||
|
||||
// 文件系统处理适配器
|
||||
/*
|
||||
钩子函数
|
||||
*/
|
||||
// 上传文件前
|
||||
BeforeUpload func(fs *FileSystem, file FileData) error
|
||||
// 上传文件后
|
||||
AfterUpload func(fs *FileSystem) error
|
||||
// 文件验证失败后
|
||||
ValidateFailed func(fs *FileSystem) error
|
||||
|
||||
/*
|
||||
文件系统处理适配器
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
// Upload 上传文件
|
||||
func (fs *FileSystem) Upload(File FileData) (err error) {
|
||||
func (fs *FileSystem) Upload(file FileData) (err error) {
|
||||
err = fs.BeforeUpload(fs, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
22
pkg/filesystem/hook.go
Normal file
22
pkg/filesystem/hook.go
Normal file
@@ -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
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import "mime/multipart"
|
||||
type FileData struct {
|
||||
File multipart.File
|
||||
Size uint64
|
||||
Name string
|
||||
MIMEType string
|
||||
}
|
||||
|
||||
@@ -24,3 +25,7 @@ func (file FileData) GetSize() uint64 {
|
||||
func (file FileData) Close() error {
|
||||
return file.Close()
|
||||
}
|
||||
|
||||
func (file FileData) GetFileName() string {
|
||||
return file.Name
|
||||
}
|
||||
|
||||
40
pkg/filesystem/validator.go
Normal file
40
pkg/filesystem/validator.go
Normal file
@@ -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
|
||||
}
|
||||
@@ -19,6 +19,8 @@ const (
|
||||
CodeCheckLogin = 401
|
||||
// CodeNoRightErr 未授权访问
|
||||
CodeNoRightErr = 403
|
||||
// CodeUploadFailed 上传出错
|
||||
CodeUploadFailed = 4001
|
||||
// CodeDBError 数据库操作失败
|
||||
CodeDBError = 50001
|
||||
// CodeEncryptError 加密失败
|
||||
|
||||
@@ -24,3 +24,13 @@ func ContainsUint(s []uint, e uint) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ContainsString 返回list中是否包含
|
||||
func ContainsString(s []string, e string) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user