You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/pkg/filesystem/filesystem.go

85 lines
1.8 KiB

5 years ago
package filesystem
import (
5 years ago
"context"
"github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
"github.com/gin-gonic/gin"
testMock "github.com/stretchr/testify/mock"
5 years ago
"io"
)
// FileHeader 上传来的文件数据处理器
type FileHeader interface {
5 years ago
io.Reader
io.Closer
GetSize() uint64
GetMIMEType() string
GetFileName() string
GetVirtualPath() string
5 years ago
}
// Handler 存储策略适配器
type Handler interface {
// 上传文件
Put(ctx context.Context, file io.ReadCloser, dst string) error
// 删除一个或多个文件
Delete(ctx context.Context, files []string) ([]string, error)
}
5 years ago
// FileSystem 管理文件的文件系统
type FileSystem struct {
/*
*/
testMock.Mock
/*
*/
5 years ago
User *model.User
/*
*/
// 上传文件前
BeforeUpload func(ctx context.Context, fs *FileSystem) error
// 上传文件后
5 years ago
AfterUpload func(ctx context.Context, fs *FileSystem) error
// 文件保存成功,插入数据库验证失败后
AfterValidateFailed func(ctx context.Context, fs *FileSystem) error
// 用户取消上传后
AfterUploadCanceled func(ctx context.Context, fs *FileSystem) error
/*
*/
Handler Handler
5 years ago
}
// NewFileSystem 初始化一个文件系统
func NewFileSystem(user *model.User) (*FileSystem, error) {
var handler Handler
// 根据存储策略类型分配适配器
switch user.Policy.Type {
case "local":
handler = local.Handler{}
default:
return nil, ErrUnknownPolicyType
}
// TODO 分配默认钩子
return &FileSystem{
User: user,
Handler: handler,
}, nil
}
// NewFileSystemFromContext 从gin.Context创建文件系统
// TODO:test
func NewFileSystemFromContext(c *gin.Context) (*FileSystem, error) {
user, _ := c.Get("user")
fs, err := NewFileSystem(user.(*model.User))
return fs, err
}