mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Add: context
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/HFO4/cloudreve/models"
|
||||
"io"
|
||||
)
|
||||
@@ -25,11 +26,11 @@ type FileSystem struct {
|
||||
钩子函数
|
||||
*/
|
||||
// 上传文件前
|
||||
BeforeUpload func(fs *FileSystem, file FileData) error
|
||||
BeforeUpload func(ctx context.Context, fs *FileSystem, file FileData) error
|
||||
// 上传文件后
|
||||
AfterUpload func(fs *FileSystem) error
|
||||
AfterUpload func(ctx context.Context, fs *FileSystem) error
|
||||
// 文件验证失败后
|
||||
ValidateFailed func(fs *FileSystem) error
|
||||
ValidateFailed func(ctx context.Context, fs *FileSystem) error
|
||||
|
||||
/*
|
||||
文件系统处理适配器
|
||||
@@ -45,8 +46,8 @@ func NewFileSystem(user *model.User) *FileSystem {
|
||||
}
|
||||
|
||||
// Upload 上传文件
|
||||
func (fs *FileSystem) Upload(file FileData) (err error) {
|
||||
err = fs.BeforeUpload(fs, file)
|
||||
func (fs *FileSystem) Upload(ctx context.Context, file FileData) (err error) {
|
||||
err = fs.BeforeUpload(ctx, fs, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
package filesystem
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
|
||||
func GenericBeforeUpload(fs *FileSystem, file FileData) error {
|
||||
func GenericBeforeUpload(ctx context.Context, fs *FileSystem, file FileData) error {
|
||||
// 验证单文件尺寸
|
||||
if !fs.ValidateFileSize(file.GetSize()) {
|
||||
if !fs.ValidateFileSize(ctx, file.GetSize()) {
|
||||
return errors.New("单个文件尺寸太大")
|
||||
}
|
||||
|
||||
// 验证并扣除容量
|
||||
if !fs.ValidateCapacity(file.GetSize()) {
|
||||
if !fs.ValidateCapacity(ctx, file.GetSize()) {
|
||||
return errors.New("容量空间不足")
|
||||
}
|
||||
|
||||
// 验证扩展名
|
||||
if !fs.ValidateExtension(file.GetFileName()) {
|
||||
if !fs.ValidateExtension(ctx, file.GetFileName()) {
|
||||
return errors.New("不允许上传此类型的文件")
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// ValidateFileSize 验证上传的文件大小是否超出限制
|
||||
func (fs *FileSystem) ValidateFileSize(size uint64) bool {
|
||||
func (fs *FileSystem) ValidateFileSize(ctx context.Context, size uint64) bool {
|
||||
return size <= fs.User.Policy.MaxSize
|
||||
}
|
||||
|
||||
// ValidateCapacity 验证并扣除用户容量
|
||||
func (fs *FileSystem) ValidateCapacity(size uint64) bool {
|
||||
func (fs *FileSystem) ValidateCapacity(ctx context.Context, size uint64) bool {
|
||||
if fs.User.DeductionCapacity(size) {
|
||||
return true
|
||||
}
|
||||
@@ -19,7 +20,7 @@ func (fs *FileSystem) ValidateCapacity(size uint64) bool {
|
||||
}
|
||||
|
||||
// ValidateExtension 验证文件扩展名
|
||||
func (fs *FileSystem) ValidateExtension(fileName string) bool {
|
||||
func (fs *FileSystem) ValidateExtension(ctx context.Context, fileName string) bool {
|
||||
// 不需要验证
|
||||
if len(fs.User.Policy.OptionsSerialized.FileType) == 0 {
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user