2019-11-16 00:37:42 -05:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
2019-11-16 07:31:34 -05:00
|
|
|
"context"
|
2019-11-16 03:11:37 -05:00
|
|
|
"github.com/HFO4/cloudreve/models"
|
2019-11-17 00:50:14 -05:00
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
2019-11-18 01:06:15 -05:00
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
2019-11-18 00:26:32 -05:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-11-16 00:37:42 -05:00
|
|
|
"io"
|
2019-11-19 04:20:59 -05:00
|
|
|
"path"
|
2019-11-17 00:50:14 -05:00
|
|
|
"path/filepath"
|
2019-11-16 00:37:42 -05:00
|
|
|
)
|
|
|
|
|
2019-11-19 03:42:36 -05:00
|
|
|
// FileHeader 上传来的文件数据处理器
|
|
|
|
type FileHeader interface {
|
2019-11-16 00:37:42 -05:00
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
GetSize() uint64
|
|
|
|
GetMIMEType() string
|
2019-11-16 03:05:10 -05:00
|
|
|
GetFileName() string
|
2019-11-19 03:42:36 -05:00
|
|
|
GetVirtualPath() string
|
2019-11-16 00:37:42 -05:00
|
|
|
}
|
|
|
|
|
2019-11-17 00:50:14 -05:00
|
|
|
// Handler 存储策略适配器
|
|
|
|
type Handler interface {
|
2019-11-18 01:06:15 -05:00
|
|
|
// 上传文件
|
2019-11-17 00:50:14 -05:00
|
|
|
Put(ctx context.Context, file io.ReadCloser, dst string) error
|
2019-11-18 01:06:15 -05:00
|
|
|
// 删除一个或多个文件
|
|
|
|
Delete(ctx context.Context, files []string) ([]string, error)
|
2019-11-17 00:50:14 -05:00
|
|
|
}
|
|
|
|
|
2019-11-16 00:37:42 -05:00
|
|
|
// FileSystem 管理文件的文件系统
|
|
|
|
type FileSystem struct {
|
2019-11-16 03:05:10 -05:00
|
|
|
/*
|
|
|
|
文件系统所有者
|
|
|
|
*/
|
2019-11-16 00:37:42 -05:00
|
|
|
User *model.User
|
|
|
|
|
2019-11-16 03:05:10 -05:00
|
|
|
/*
|
|
|
|
钩子函数
|
|
|
|
*/
|
|
|
|
// 上传文件前
|
2019-11-18 06:09:56 -05:00
|
|
|
BeforeUpload func(ctx context.Context, fs *FileSystem) error
|
2019-11-16 03:05:10 -05:00
|
|
|
// 上传文件后
|
2019-11-16 07:31:34 -05:00
|
|
|
AfterUpload func(ctx context.Context, fs *FileSystem) error
|
2019-11-17 00:50:14 -05:00
|
|
|
// 文件保存成功,插入数据库验证失败后
|
2019-11-18 01:06:15 -05:00
|
|
|
AfterValidateFailed func(ctx context.Context, fs *FileSystem) error
|
|
|
|
// 用户取消上传后
|
2019-11-18 06:09:56 -05:00
|
|
|
AfterUploadCanceled func(ctx context.Context, fs *FileSystem) error
|
2019-11-16 03:05:10 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
文件系统处理适配器
|
|
|
|
*/
|
2019-11-17 00:50:14 -05:00
|
|
|
Handler Handler
|
2019-11-16 00:37:42 -05:00
|
|
|
}
|
|
|
|
|
2019-11-16 03:49:03 -05:00
|
|
|
// NewFileSystem 初始化一个文件系统
|
2019-11-17 00:50:14 -05:00
|
|
|
func NewFileSystem(user *model.User) (*FileSystem, error) {
|
|
|
|
var handler Handler
|
|
|
|
|
|
|
|
// 根据存储策略类型分配适配器
|
|
|
|
switch user.Policy.Type {
|
|
|
|
case "local":
|
|
|
|
handler = local.Handler{}
|
|
|
|
default:
|
|
|
|
return nil, UnknownPolicyTypeError
|
2019-11-16 03:49:03 -05:00
|
|
|
}
|
2019-11-17 00:50:14 -05:00
|
|
|
|
|
|
|
// TODO 分配默认钩子
|
|
|
|
return &FileSystem{
|
|
|
|
User: user,
|
|
|
|
Handler: handler,
|
|
|
|
}, nil
|
2019-11-16 03:49:03 -05:00
|
|
|
}
|
|
|
|
|
2019-11-19 05:10:18 -05:00
|
|
|
/* ============
|
|
|
|
文件相关
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
|
|
|
|
// AddFile 新增文件记录
|
2019-11-20 02:24:26 -05:00
|
|
|
func (fs *FileSystem) AddFile(ctx context.Context, parent *model.Folder) (*model.File, error) {
|
|
|
|
file := ctx.Value(FileHeaderCtx).(FileHeader)
|
|
|
|
filePath := ctx.Value(SavePathCtx).(string)
|
|
|
|
|
|
|
|
newFile := model.File{
|
|
|
|
Name: file.GetFileName(),
|
|
|
|
SourceName: filePath,
|
|
|
|
UserID: fs.User.ID,
|
|
|
|
Size: file.GetSize(),
|
|
|
|
FolderID: parent.ID,
|
|
|
|
PolicyID: fs.User.Policy.ID,
|
|
|
|
Dir: parent.PositionAbsolute,
|
|
|
|
}
|
|
|
|
_, err := newFile.Create()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &newFile, nil
|
2019-11-19 05:10:18 -05:00
|
|
|
}
|
|
|
|
|
2019-11-18 06:09:56 -05:00
|
|
|
/* ================
|
|
|
|
上传处理相关
|
|
|
|
================
|
2019-11-18 00:26:32 -05:00
|
|
|
*/
|
|
|
|
|
2019-11-16 00:37:42 -05:00
|
|
|
// Upload 上传文件
|
2019-11-19 03:42:36 -05:00
|
|
|
func (fs *FileSystem) Upload(ctx context.Context, file FileHeader) (err error) {
|
2019-11-19 05:10:18 -05:00
|
|
|
ctx = context.WithValue(ctx, FileHeaderCtx, file)
|
2019-11-18 06:09:56 -05:00
|
|
|
|
2019-11-17 00:50:14 -05:00
|
|
|
// 上传前的钩子
|
2019-11-18 01:06:15 -05:00
|
|
|
if fs.BeforeUpload != nil {
|
2019-11-18 06:09:56 -05:00
|
|
|
err = fs.BeforeUpload(ctx, fs)
|
2019-11-18 01:06:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-16 03:05:10 -05:00
|
|
|
}
|
2019-11-17 00:50:14 -05:00
|
|
|
|
|
|
|
// 生成文件名和路径
|
2019-11-19 03:42:36 -05:00
|
|
|
savePath := fs.GenerateSavePath(ctx, file)
|
2019-11-17 00:50:14 -05:00
|
|
|
|
2019-11-18 00:26:32 -05:00
|
|
|
// 处理客户端未完成上传时,关闭连接
|
|
|
|
go fs.CancelUpload(ctx, savePath, file)
|
|
|
|
|
2019-11-17 00:50:14 -05:00
|
|
|
// 保存文件
|
|
|
|
err = fs.Handler.Put(ctx, file, savePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-18 06:09:56 -05:00
|
|
|
// 上传完成后的钩子
|
|
|
|
if fs.AfterUpload != nil {
|
|
|
|
ctx = context.WithValue(ctx, SavePathCtx, savePath)
|
|
|
|
err = fs.AfterUpload(ctx, fs)
|
2019-11-18 06:32:06 -05:00
|
|
|
|
2019-11-18 06:09:56 -05:00
|
|
|
if err != nil {
|
2019-11-18 06:32:06 -05:00
|
|
|
// 上传完成后续处理失败
|
|
|
|
if fs.AfterValidateFailed != nil {
|
|
|
|
followUpErr := fs.AfterValidateFailed(ctx, fs)
|
|
|
|
// 失败后再失败...
|
|
|
|
if followUpErr != nil {
|
|
|
|
util.Log().Warning("AfterValidateFailed 钩子执行失败,%s", followUpErr)
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 06:09:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 00:37:42 -05:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-17 00:50:14 -05:00
|
|
|
|
|
|
|
// GenerateSavePath 生成要存放文件的路径
|
2019-11-19 03:42:36 -05:00
|
|
|
func (fs *FileSystem) GenerateSavePath(ctx context.Context, file FileHeader) string {
|
2019-11-17 00:50:14 -05:00
|
|
|
return filepath.Join(
|
2019-11-19 03:42:36 -05:00
|
|
|
fs.User.Policy.GeneratePath(
|
|
|
|
fs.User.Model.ID,
|
|
|
|
file.GetVirtualPath(),
|
|
|
|
),
|
|
|
|
fs.User.Policy.GenerateFileName(
|
|
|
|
fs.User.Model.ID,
|
|
|
|
file.GetFileName(),
|
|
|
|
),
|
2019-11-17 00:50:14 -05:00
|
|
|
)
|
|
|
|
}
|
2019-11-18 00:26:32 -05:00
|
|
|
|
|
|
|
// CancelUpload 监测客户端取消上传
|
2019-11-19 03:42:36 -05:00
|
|
|
func (fs *FileSystem) CancelUpload(ctx context.Context, path string, file FileHeader) {
|
2019-11-18 06:09:56 -05:00
|
|
|
ginCtx := ctx.Value(GinCtx).(*gin.Context)
|
2019-11-18 00:26:32 -05:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// 客户端正常关闭,不执行操作
|
|
|
|
case <-ginCtx.Request.Context().Done():
|
2019-11-18 01:06:15 -05:00
|
|
|
// 客户端取消了上传
|
|
|
|
if fs.AfterUploadCanceled == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-11-18 06:09:56 -05:00
|
|
|
ctx = context.WithValue(ctx, SavePathCtx, path)
|
|
|
|
err := fs.AfterUploadCanceled(ctx, fs)
|
2019-11-18 01:06:15 -05:00
|
|
|
if err != nil {
|
|
|
|
util.Log().Warning("执行 AfterUploadCanceled 钩子出错,%s", err)
|
|
|
|
}
|
2019-11-18 00:26:32 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-18 06:09:56 -05:00
|
|
|
|
|
|
|
/* =================
|
|
|
|
路径/目录相关
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// IsPathExist 返回给定目录是否存在
|
2019-11-19 05:10:18 -05:00
|
|
|
// 如果存在就返回目录
|
|
|
|
func (fs *FileSystem) IsPathExist(path string) (bool, model.Folder) {
|
|
|
|
folder, err := model.GetFolderByPath(path, fs.User.ID)
|
|
|
|
return err == nil, folder
|
2019-11-18 06:09:56 -05:00
|
|
|
}
|
2019-11-19 04:20:59 -05:00
|
|
|
|
|
|
|
// IsFileExist 返回给定路径的文件是否存在
|
|
|
|
func (fs *FileSystem) IsFileExist(fullPath string) bool {
|
|
|
|
basePath := path.Dir(fullPath)
|
|
|
|
fileName := path.Base(fullPath)
|
|
|
|
|
|
|
|
_, err := model.GetFileByPathAndName(basePath, fileName, fs.User.ID)
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|