mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: after uploading hooks and checks
This commit is contained in:
12
pkg/filesystem/context.go
Normal file
12
pkg/filesystem/context.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package filesystem
|
||||
|
||||
type key int
|
||||
|
||||
const (
|
||||
// GinCtx Gin的上下文
|
||||
GinCtx key = iota
|
||||
// SavePathCtx 文件物理路径
|
||||
SavePathCtx
|
||||
// FileCtx 上传的文件
|
||||
FileCtx
|
||||
)
|
||||
@@ -2,7 +2,6 @@ package filesystem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
@@ -39,13 +38,13 @@ type FileSystem struct {
|
||||
钩子函数
|
||||
*/
|
||||
// 上传文件前
|
||||
BeforeUpload func(ctx context.Context, fs *FileSystem, file FileData) error
|
||||
BeforeUpload func(ctx context.Context, fs *FileSystem) error
|
||||
// 上传文件后
|
||||
AfterUpload func(ctx context.Context, fs *FileSystem) error
|
||||
// 文件保存成功,插入数据库验证失败后
|
||||
AfterValidateFailed func(ctx context.Context, fs *FileSystem) error
|
||||
// 用户取消上传后
|
||||
AfterUploadCanceled func(ctx context.Context, fs *FileSystem, file FileData) error
|
||||
AfterUploadCanceled func(ctx context.Context, fs *FileSystem) error
|
||||
|
||||
/*
|
||||
文件系统处理适配器
|
||||
@@ -72,15 +71,18 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
/*
|
||||
上传处理相关
|
||||
/* ================
|
||||
上传处理相关
|
||||
================
|
||||
*/
|
||||
|
||||
// Upload 上传文件
|
||||
func (fs *FileSystem) Upload(ctx context.Context, file FileData) (err error) {
|
||||
ctx = context.WithValue(ctx, FileCtx, file)
|
||||
|
||||
// 上传前的钩子
|
||||
if fs.BeforeUpload != nil {
|
||||
err = fs.BeforeUpload(ctx, fs, file)
|
||||
err = fs.BeforeUpload(ctx, fs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -98,6 +100,15 @@ func (fs *FileSystem) Upload(ctx context.Context, file FileData) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
// 上传完成后的钩子
|
||||
if fs.AfterUpload != nil {
|
||||
ctx = context.WithValue(ctx, SavePathCtx, savePath)
|
||||
err = fs.AfterUpload(ctx, fs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -111,20 +122,30 @@ func (fs *FileSystem) GenerateSavePath(file FileData) string {
|
||||
|
||||
// CancelUpload 监测客户端取消上传
|
||||
func (fs *FileSystem) CancelUpload(ctx context.Context, path string, file FileData) {
|
||||
ginCtx := ctx.Value("ginCtx").(*gin.Context)
|
||||
ginCtx := ctx.Value(GinCtx).(*gin.Context)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println("正常关闭")
|
||||
// 客户端正常关闭,不执行操作
|
||||
case <-ginCtx.Request.Context().Done():
|
||||
// 客户端取消了上传
|
||||
if fs.AfterUploadCanceled == nil {
|
||||
return
|
||||
}
|
||||
ctx = context.WithValue(ctx, "path", path)
|
||||
err := fs.AfterUploadCanceled(ctx, fs, file)
|
||||
ctx = context.WithValue(ctx, SavePathCtx, path)
|
||||
err := fs.AfterUploadCanceled(ctx, fs)
|
||||
if err != nil {
|
||||
util.Log().Warning("执行 AfterUploadCanceled 钩子出错,%s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* =================
|
||||
路径/目录相关
|
||||
=================
|
||||
*/
|
||||
|
||||
// IsPathExist 返回给定目录是否存在
|
||||
func (fs *FileSystem) IsPathExist(path string) bool {
|
||||
_, err := model.GetFolderByPath(path, fs.User.ID)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
)
|
||||
|
||||
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
|
||||
func GenericBeforeUpload(ctx context.Context, fs *FileSystem, file FileData) error {
|
||||
func GenericBeforeUpload(ctx context.Context, fs *FileSystem) error {
|
||||
file := ctx.Value(FileCtx).(FileData)
|
||||
|
||||
// 验证单文件尺寸
|
||||
if !fs.ValidateFileSize(ctx, file.GetSize()) {
|
||||
return FileSizeTooBigError
|
||||
@@ -31,7 +33,9 @@ func GenericBeforeUpload(ctx context.Context, fs *FileSystem, file FileData) err
|
||||
}
|
||||
|
||||
// GenericAfterUploadCanceled 通用上传取消处理钩子,包含数据库操作
|
||||
func GenericAfterUploadCanceled(ctx context.Context, fs *FileSystem, file FileData) error {
|
||||
func GenericAfterUploadCanceled(ctx context.Context, fs *FileSystem) error {
|
||||
file := ctx.Value(FileCtx).(FileData)
|
||||
|
||||
filePath := ctx.Value("path").(string)
|
||||
// 删除临时文件
|
||||
if util.Exists(filePath) {
|
||||
@@ -47,3 +51,17 @@ func GenericAfterUploadCanceled(ctx context.Context, fs *FileSystem, file FileDa
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenericAfterUpload 文件上传完成后,包含数据库操作
|
||||
func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
|
||||
// 获取Gin的上下文
|
||||
//ginCtx := ctx.Value(GinCtx).(*gin.Context)
|
||||
// 文件存放的虚拟路径
|
||||
//virtualPath := ginCtx.GetHeader("X-Path")
|
||||
|
||||
// 检查路径是否存在
|
||||
if !fs.IsPathExist("/") {
|
||||
return errors.New("sss")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -12,11 +12,17 @@ var reservedCharacter = []string{"\\", "?", "*", "<", "\"", ":", ">", "/"}
|
||||
|
||||
// ValidateLegalName 验证文件名/文件夹名是否合法
|
||||
func (fs *FileSystem) ValidateLegalName(ctx context.Context, name string) bool {
|
||||
// 是否包含保留字符
|
||||
for _, value := range reservedCharacter {
|
||||
if strings.Contains(name, value) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 是否超出长度限制
|
||||
if len(name) >= 256 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user