Feat: keep folder structure in aria2 transferring

This commit is contained in:
HFO4
2020-11-23 18:44:13 +08:00
parent d97bc26042
commit c6110e9e75
3 changed files with 47 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ import (
"os"
"path"
"path/filepath"
"strings"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
@@ -24,9 +25,11 @@ type TransferTask struct {
// TransferProps 中转任务属性
type TransferProps struct {
Src []string `json:"src"` // 原始目录
Src []string `json:"src"` // 原始文件
Parent string `json:"parent"` // 父目录
Dst string `json:"dst"` // 目的目录ID
// 将会保留原始文件的目录结构Src 除去 Parent 开头作为最终路径
TrimPath bool `json:"trim_path"`
}
// Props 获取任务属性
@@ -90,7 +93,16 @@ func (job *TransferTask) Do() {
for index, file := range job.TaskProps.Src {
job.TaskModel.SetProgress(index)
err = fs.UploadFromPath(context.Background(), file, path.Join(job.TaskProps.Dst, filepath.Base(file)))
dst := path.Join(job.TaskProps.Dst, filepath.Base(file))
if job.TaskProps.TrimPath {
// 保留原始目录
trim := util.FormSlash(job.TaskProps.Parent)
src := util.FormSlash(file)
dst = path.Join(job.TaskProps.Dst, strings.TrimPrefix(src, trim))
}
err = fs.UploadFromPath(context.Background(), file, dst)
if err != nil {
job.SetErrorMsg("文件转存失败", err)
}
@@ -108,7 +120,7 @@ func (job *TransferTask) Recycle() {
}
// NewTransferTask 新建中转任务
func NewTransferTask(user uint, src []string, dst, parent string) (Job, error) {
func NewTransferTask(user uint, src []string, dst, parent string, trim bool) (Job, error) {
creator, err := model.GetActiveUserByID(user)
if err != nil {
return nil, err
@@ -117,9 +129,10 @@ func NewTransferTask(user uint, src []string, dst, parent string) (Job, error) {
newTask := &TransferTask{
User: &creator,
TaskProps: TransferProps{
Src: src,
Parent: parent,
Dst: dst,
Src: src,
Parent: parent,
Dst: dst,
TrimPath: trim,
},
}