Refactor: use universal FileHeader when handling file upload, remove usage of global ctx with FileHeader, SavePath, DisableOverwrite

This commit is contained in:
HFO4
2022-02-27 14:03:07 +08:00
parent 8a222e7df4
commit 868a88e5fc
39 changed files with 331 additions and 359 deletions

View File

@@ -7,6 +7,7 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
)
@@ -106,7 +107,7 @@ func (job *CompressTask) Do() {
job.TaskModel.SetProgress(TransferringProgress)
// 上传文件
err = fs.UploadFromPath(ctx, zipFile, job.TaskProps.Dst, true)
err = fs.UploadFromPath(ctx, zipFile, job.TaskProps.Dst, true, fsctx.Create)
if err != nil {
job.SetErrorMsg(err.Error())
return

View File

@@ -6,7 +6,6 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
)
// DecompressTask 文件压缩任务
@@ -83,11 +82,7 @@ func (job *DecompressTask) Do() {
job.TaskModel.SetProgress(DecompressingProgress)
// 禁止重名覆盖
ctx := context.Background()
ctx = context.WithValue(ctx, fsctx.DisableOverwrite, true)
err = fs.Decompress(ctx, job.TaskProps.Src, job.TaskProps.Dst)
err = fs.Decompress(context.Background(), job.TaskProps.Src, job.TaskProps.Dst)
if err != nil {
job.SetErrorMsg("解压缩失败", err)
return

View File

@@ -7,7 +7,6 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
)
@@ -134,13 +133,12 @@ func (job *ImportTask) Do() {
if !object.IsDir {
// 创建文件信息
virtualPath := path.Dir(path.Join(job.TaskProps.Dst, object.RelativePath))
fileHeader := local.FileStream{
fileHeader := fsctx.FileStream{
Size: object.Size,
VirtualPath: virtualPath,
Name: object.Name,
SavePath: object.Source,
}
addFileCtx := context.WithValue(ctx, fsctx.FileHeaderCtx, fileHeader)
addFileCtx = context.WithValue(addFileCtx, fsctx.SavePathCtx, object.Source)
// 查找父目录
parentFolder := &model.Folder{}
@@ -162,7 +160,7 @@ func (job *ImportTask) Do() {
}
// 插入文件记录
_, err := fs.AddFile(addFileCtx, parentFolder)
_, err := fs.AddFile(context.Background(), parentFolder, &fileHeader)
if err != nil {
util.Log().Warning("导入任务无法创插入文件[%s], %s",
object.RelativePath, err)

View File

@@ -100,7 +100,6 @@ func (job *TransferTask) Do() {
}
fs.SwitchToShadowHandler(master.Instance, master.URL.String(), master.ID)
ctx := context.WithValue(context.Background(), fsctx.DisableOverwrite, true)
file, err := os.Open(util.RelativePath(job.Req.Src))
if err != nil {
job.SetErrorMsg("无法读取源文件", err)
@@ -118,7 +117,12 @@ func (job *TransferTask) Do() {
size := fi.Size()
err = fs.Handler.Put(ctx, file, job.Req.Dst, uint64(size))
err = fs.Handler.Put(context.Background(), &fsctx.FileStream{
File: file,
Mode: fsctx.Create,
SavePath: job.Req.Dst,
Size: uint64(size),
})
if err != nil {
job.SetErrorMsg("文件上传失败", err)
return

View File

@@ -107,8 +107,6 @@ func (job *TransferTask) Do() {
dst = path.Join(job.TaskProps.Dst, strings.TrimPrefix(src, trim))
}
ctx := context.WithValue(context.Background(), fsctx.DisableOverwrite, true)
ctx = context.WithValue(ctx, fsctx.SlaveSrcPath, file)
if job.TaskProps.NodeID > 1 {
// 指定为从机中转
@@ -120,10 +118,16 @@ func (job *TransferTask) Do() {
// 切换为从机节点处理上传
fs.SwitchToSlaveHandler(node)
err = fs.UploadFromStream(ctx, nil, dst, job.TaskProps.SrcSizes[file])
err = fs.UploadFromStream(context.Background(), &fsctx.FileStream{
File: nil,
Size: job.TaskProps.SrcSizes[file],
Name: path.Base(dst),
VirtualPath: path.Dir(dst),
Mode: fsctx.Create,
})
} else {
// 主机节点中转
err = fs.UploadFromPath(ctx, file, dst, true)
err = fs.UploadFromPath(context.Background(), file, dst, true, fsctx.Create)
}
if err != nil {