Skip to content

Commit

Permalink
Feat: keep folder structure in aria2 transferring
Browse files Browse the repository at this point in the history
  • Loading branch information
HFO4 committed Nov 23, 2020
1 parent d97bc26 commit c6110e9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/aria2/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (monitor *Monitor) Complete(status rpc.StatusInfo) bool {
file,
monitor.Task.Dst,
monitor.Task.Parent,
true,
)
if err != nil {
monitor.setErrorStatus(err)
Expand Down
25 changes: 19 additions & 6 deletions pkg/task/tranfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"
"path/filepath"
"strings"

model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
Expand All @@ -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 获取任务属性
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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,
},
}

Expand Down
29 changes: 27 additions & 2 deletions pkg/task/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ func TestTransferTask_Do(t *testing.T) {
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotEmpty(task.GetError().Msg)
}

// 替换目录前缀
{
task.User = &model.User{
Policy: model.Policy{
Type: "mock",
},
}
task.TaskProps.Src = []string{"test/not_exist"}
task.TaskProps.Parent = "test/not_exist"
task.TaskProps.TrimPath = true
// 更新进度
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
// 更新错误
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
task.Do()
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotEmpty(task.GetError().Msg)
}
}

func TestNewTransferTask(t *testing.T) {
Expand All @@ -113,7 +138,7 @@ func TestNewTransferTask(t *testing.T) {
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
job, err := NewTransferTask(1, []string{}, "/", "/")
job, err := NewTransferTask(1, []string{}, "/", "/", false)
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotNil(job)
asserts.NoError(err)
Expand All @@ -125,7 +150,7 @@ func TestNewTransferTask(t *testing.T) {
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
mock.ExpectRollback()
job, err := NewTransferTask(1, []string{}, "/", "/")
job, err := NewTransferTask(1, []string{}, "/", "/", false)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Nil(job)
asserts.Error(err)
Expand Down

0 comments on commit c6110e9

Please sign in to comment.