Cloudreve/pkg/filesystem/upload.go

185 lines
4.6 KiB
Go
Raw Normal View History

package filesystem
import (
"context"
2019-12-15 14:01:37 +08:00
model "github.com/HFO4/cloudreve/models"
2019-12-28 15:50:56 +08:00
"github.com/HFO4/cloudreve/pkg/cache"
2020-01-18 14:08:43 +08:00
"github.com/HFO4/cloudreve/pkg/filesystem/driver/local"
2019-12-10 17:10:34 +08:00
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
2019-12-28 13:14:00 +08:00
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/HFO4/cloudreve/pkg/util"
"github.com/gin-gonic/gin"
2020-01-04 16:21:43 +08:00
"path"
)
/* ================
上传处理相关
================
*/
// Upload 上传文件
func (fs *FileSystem) Upload(ctx context.Context, file FileHeader) (err error) {
2019-12-10 17:10:34 +08:00
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, file)
// 上传前的钩子
err = fs.Trigger(ctx, "BeforeUpload")
2019-11-26 11:42:26 +08:00
if err != nil {
return err
}
2019-12-27 21:15:05 +08:00
// 生成文件名和路径,
2019-12-15 14:01:37 +08:00
var savePath string
2019-12-27 21:15:05 +08:00
// 如果是更新操作就从上下文中获取
if originFile, ok := ctx.Value(fsctx.FileModelCtx).(model.File); ok {
2019-12-15 14:01:37 +08:00
savePath = originFile.SourceName
} else {
savePath = fs.GenerateSavePath(ctx, file)
}
2019-12-27 21:15:05 +08:00
ctx = context.WithValue(ctx, fsctx.SavePathCtx, savePath)
// 处理客户端未完成上传时,关闭连接
go fs.CancelUpload(ctx, savePath, file)
// 保存文件
2019-11-26 11:42:26 +08:00
err = fs.Handler.Put(ctx, file, savePath, file.GetSize())
if err != nil {
fs.Trigger(ctx, "AfterUploadFailed")
return err
}
// 上传完成后的钩子
err = fs.Trigger(ctx, "AfterUpload")
2019-11-26 11:42:26 +08:00
if err != nil {
// 上传完成后续处理失败
followUpErr := fs.Trigger(ctx, "AfterValidateFailed")
2019-11-26 11:42:26 +08:00
// 失败后再失败...
if followUpErr != nil {
2019-11-26 20:59:57 +08:00
util.Log().Debug("AfterValidateFailed 钩子执行失败,%s", followUpErr)
}
2019-11-26 11:42:26 +08:00
return err
}
2019-12-27 21:15:05 +08:00
util.Log().Info(
"新文件PUT:%s , 大小:%d, 上传者:%s",
file.GetFileName(),
file.GetSize(),
fs.User.Nick,
)
return nil
}
// GenerateSavePath 生成要存放文件的路径
2019-12-27 21:15:05 +08:00
// TODO 完善测试
func (fs *FileSystem) GenerateSavePath(ctx context.Context, file FileHeader) string {
2019-12-27 21:15:05 +08:00
if fs.User.Model.ID != 0 {
2020-01-04 16:21:43 +08:00
return path.Join(
2019-12-27 21:15:05 +08:00
fs.User.Policy.GeneratePath(
fs.User.Model.ID,
file.GetVirtualPath(),
),
fs.User.Policy.GenerateFileName(
fs.User.Model.ID,
file.GetFileName(),
),
)
}
2019-12-28 13:14:00 +08:00
// 匿名文件系统尝试根据上下文中的上传策略生成路径
var anonymousPolicy model.Policy
if policy, ok := ctx.Value(fsctx.UploadPolicyCtx).(serializer.UploadPolicy); ok {
anonymousPolicy = model.Policy{
Type: "remote",
AutoRename: policy.AutoRename,
DirNameRule: policy.SavePath,
FileNameRule: policy.FileName,
}
}
2020-01-04 16:21:43 +08:00
return path.Join(
2019-12-28 13:14:00 +08:00
anonymousPolicy.GeneratePath(
2019-12-27 21:15:05 +08:00
0,
"",
),
2019-12-28 13:14:00 +08:00
anonymousPolicy.GenerateFileName(
2019-12-27 21:15:05 +08:00
0,
2019-12-28 13:14:00 +08:00
file.GetFileName(),
),
)
}
// CancelUpload 监测客户端取消上传
func (fs *FileSystem) CancelUpload(ctx context.Context, path string, file FileHeader) {
var reqContext context.Context
if ginCtx, ok := ctx.Value(fsctx.GinCtx).(*gin.Context); ok {
reqContext = ginCtx.Request.Context()
} else {
reqContext = ctx.Value(fsctx.HTTPCtx).(context.Context)
}
select {
case <-reqContext.Done():
select {
case <-ctx.Done():
// 客户端正常关闭,不执行操作
default:
2019-11-27 13:10:19 +08:00
// 客户端取消上传,删除临时文件
util.Log().Debug("客户端取消上传")
if fs.Hooks["AfterUploadCanceled"] == nil {
return
}
2019-12-10 17:10:34 +08:00
ctx = context.WithValue(ctx, fsctx.SavePathCtx, path)
err := fs.Trigger(ctx, "AfterUploadCanceled")
if err != nil {
util.Log().Debug("执行 AfterUploadCanceled 钩子出错,%s", err)
}
}
}
}
2019-12-28 15:50:56 +08:00
// GetUploadToken 生成新的上传凭证
2020-01-18 10:40:03 +08:00
func (fs *FileSystem) GetUploadToken(ctx context.Context, path string, size uint64, name string) (*serializer.UploadCredential, error) {
2019-12-28 15:50:56 +08:00
// 获取相关有效期设置
2020-01-02 12:44:53 +08:00
credentialTTL := model.GetIntSetting("upload_credential_timeout", 3600)
callBackSessionTTL := model.GetIntSetting("upload_session_timeout", 86400)
2019-12-28 15:50:56 +08:00
2020-01-02 12:44:53 +08:00
var err error
2019-12-28 15:50:56 +08:00
2020-01-15 10:14:15 +08:00
// 是否需要预先生成存储路径
var savePath string
2020-01-15 10:14:15 +08:00
if fs.User.Policy.IsPathGenerateNeeded() {
savePath = fs.GenerateSavePath(ctx, local.FileStream{Name: name})
ctx = context.WithValue(ctx, fsctx.SavePathCtx, savePath)
2020-01-15 10:14:15 +08:00
}
ctx = context.WithValue(ctx, fsctx.FileSizeCtx, size)
2020-01-15 10:14:15 +08:00
2019-12-28 15:50:56 +08:00
// 获取上传凭证
callbackKey := util.RandStringRunes(32)
2020-01-02 12:44:53 +08:00
credential, err := fs.Handler.Token(ctx, int64(credentialTTL), callbackKey)
2019-12-28 15:50:56 +08:00
if err != nil {
return nil, serializer.NewError(serializer.CodeEncryptError, "无法获取上传凭证", err)
}
// 创建回调会话
err = cache.Set(
"callback_"+callbackKey,
serializer.UploadSession{
2020-01-23 12:38:32 +08:00
Key: callbackKey,
2019-12-28 15:50:56 +08:00
UID: fs.User.ID,
PolicyID: fs.User.GetPolicyID(),
2019-12-28 15:50:56 +08:00
VirtualPath: path,
2020-01-18 10:40:03 +08:00
Name: name,
Size: size,
SavePath: savePath,
2019-12-28 15:50:56 +08:00
},
2020-01-02 12:44:53 +08:00
callBackSessionTTL,
2019-12-28 15:50:56 +08:00
)
if err != nil {
return nil, err
}
return &credential, nil
}