feat: add copy to task manager

This commit is contained in:
Noah Hsu
2022-06-17 21:23:44 +08:00
parent 53e969e894
commit fa6e918fc7
10 changed files with 143 additions and 70 deletions

View File

@ -2,34 +2,55 @@ package fs
import (
"context"
"fmt"
"github.com/alist-org/alist/v3/pkg/task"
"github.com/alist-org/alist/v3/pkg/utils"
stdpath "path"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations"
"github.com/alist-org/alist/v3/internal/task"
"github.com/pkg/errors"
)
var copyTaskManager = task.NewTaskManager()
func CopyBetween2Accounts(ctx context.Context, srcAccount, dstAccount driver.Driver, srcPath, dstPath string) error {
srcFile, err := operations.Get(ctx, srcAccount, srcPath)
func CopyBetween2Accounts(ctx context.Context, srcAccount, dstAccount driver.Driver, srcPath, dstPath string, setStatus func(status string)) error {
setStatus("getting src object")
srcObj, err := operations.Get(ctx, srcAccount, srcPath)
if err != nil {
return errors.WithMessagef(err, "failed get src [%s] file", srcPath)
}
if srcFile.IsDir() {
if srcObj.IsDir() {
setStatus("src object is dir, listing files")
files, err := operations.List(ctx, srcAccount, srcPath)
if err != nil {
return errors.WithMessagef(err, "failed list src [%s] files", srcPath)
}
for _, file := range files {
if utils.IsCanceled(ctx) {
return nil
}
srcFilePath := stdpath.Join(srcPath, file.GetName())
dstFilePath := stdpath.Join(dstPath, file.GetName())
if err := CopyBetween2Accounts(ctx, srcAccount, dstAccount, srcFilePath, dstFilePath); err != nil {
return errors.WithMessagef(err, "failed copy file [%s] to [%s]", srcFilePath, dstFilePath)
}
copyTaskManager.Add(fmt.Sprintf("copy %s to %s", srcFilePath, dstFilePath), func(task *task.Task) error {
return CopyBetween2Accounts(ctx, srcAccount, dstAccount, srcFilePath, dstFilePath, task.SetStatus)
})
}
} else {
copyTaskManager.Add(fmt.Sprintf("copy %s to %s", srcPath, dstPath), func(task *task.Task) error {
return CopyFileBetween2Accounts(task.Ctx, srcAccount, dstAccount, srcPath, dstPath, func(percentage float64) {
task.SetStatus(fmt.Sprintf("uploading: %2.f%", percentage))
})
})
}
return nil
}
func CopyFileBetween2Accounts(ctx context.Context, srcAccount, dstAccount driver.Driver, srcPath, dstPath string, up driver.UpdateProgress) error {
srcFile, err := operations.Get(ctx, srcAccount, srcPath)
if err != nil {
return errors.WithMessagef(err, "failed get src [%s] file", srcPath)
}
link, err := operations.Link(ctx, srcAccount, srcPath, model.LinkArgs{})
if err != nil {
@ -39,6 +60,5 @@ func CopyBetween2Accounts(ctx context.Context, srcAccount, dstAccount driver.Dri
if err != nil {
return errors.WithMessagef(err, "failed get [%s] stream", srcPath)
}
// TODO add as task
return operations.Put(ctx, dstAccount, dstPath, stream)
return operations.Put(ctx, dstAccount, dstPath, stream, up)
}

View File

@ -3,11 +3,10 @@ package fs
import (
"context"
"fmt"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations"
"github.com/alist-org/alist/v3/internal/task"
"github.com/alist-org/alist/v3/pkg/task"
"github.com/pkg/errors"
)
@ -49,7 +48,7 @@ func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) (
if err != nil {
return false, errors.WithMessage(err, "failed get src account")
}
dstAccount, dstActualPath, err := operations.GetAccountAndActualPath(srcPath)
dstAccount, dstActualPath, err := operations.GetAccountAndActualPath(dstPath)
if err != nil {
return false, errors.WithMessage(err, "failed get dst account")
}
@ -60,7 +59,7 @@ func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) (
// not in an account
// TODO add status set callback to put
copyTaskManager.Add(fmt.Sprintf("copy %s to %s", srcActualPath, dstActualPath), func(task *task.Task) error {
return CopyBetween2Accounts(context.TODO(), srcAccount, dstAccount, srcActualPath, dstActualPath)
return CopyBetween2Accounts(task.Ctx, srcAccount, dstAccount, srcActualPath, dstActualPath, task.SetStatus)
})
return true, nil
}
@ -73,10 +72,11 @@ func Remove(ctx context.Context, account driver.Driver, path string) error {
return operations.Remove(ctx, account, actualPath)
}
// Put add as a put task
func Put(ctx context.Context, account driver.Driver, parentPath string, file model.FileStreamer) error {
account, actualParentPath, err := operations.GetAccountAndActualPath(parentPath)
if err != nil {
return errors.WithMessage(err, "failed get account")
}
return operations.Put(ctx, account, actualParentPath, file)
return operations.Put(ctx, account, actualParentPath, file, nil)
}