Skip to content

Commit

Permalink
feat: add upload file to task manager
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 17, 2022
1 parent b9f9e58 commit 72a7659
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
22 changes: 22 additions & 0 deletions internal/fs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ import (

var copyTaskManager = task.NewTaskManager()

// Copy if in an account, call move method
// if not, add copy task
func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) (bool, error) {
srcAccount, srcActualPath, err := operations.GetAccountAndActualPath(srcPath)
if err != nil {
return false, errors.WithMessage(err, "failed get src account")
}
dstAccount, dstActualPath, err := operations.GetAccountAndActualPath(dstPath)
if err != nil {
return false, errors.WithMessage(err, "failed get dst account")
}
// copy if in an account, just call driver.Copy
if srcAccount.GetAccount() == dstAccount.GetAccount() {
return false, operations.Copy(ctx, account, srcActualPath, dstActualPath)
}
// not in an account
copyTaskManager.Add(fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcActualPath, dstAccount.GetAccount().VirtualPath, dstActualPath), func(task *task.Task) error {
return CopyBetween2Accounts(task.Ctx, srcAccount, dstAccount, srcActualPath, dstActualPath, task.SetStatus)
})
return true, nil
}

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)
Expand Down
25 changes: 25 additions & 0 deletions internal/fs/put.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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/pkg/task"
"github.com/pkg/errors"
)

var uploadTaskManager = task.NewTaskManager()

// 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")
}
uploadTaskManager.Add(fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), account.GetAccount().VirtualPath, actualParentPath), func(task *task.Task) error {
return operations.Put(task.Ctx, account, actualParentPath, file, nil)
})
return nil
}
34 changes: 0 additions & 34 deletions internal/fs/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ 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/pkg/task"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -41,41 +38,10 @@ func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string)
return operations.Rename(ctx, account, srcActualPath, dstName)
}

// Copy if in an account, call move method
// if not, add copy task
func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) (bool, error) {
srcAccount, srcActualPath, err := operations.GetAccountAndActualPath(srcPath)
if err != nil {
return false, errors.WithMessage(err, "failed get src account")
}
dstAccount, dstActualPath, err := operations.GetAccountAndActualPath(dstPath)
if err != nil {
return false, errors.WithMessage(err, "failed get dst account")
}
// copy if in an account, just call driver.Copy
if srcAccount.GetAccount() == dstAccount.GetAccount() {
return false, operations.Copy(ctx, account, srcActualPath, dstActualPath)
}
// not in an account
copyTaskManager.Add(fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcActualPath, dstAccount.GetAccount().VirtualPath, dstActualPath), func(task *task.Task) error {
return CopyBetween2Accounts(task.Ctx, srcAccount, dstAccount, srcActualPath, dstActualPath, task.SetStatus)
})
return true, nil
}

func Remove(ctx context.Context, account driver.Driver, path string) error {
account, actualPath, err := operations.GetAccountAndActualPath(path)
if err != nil {
return errors.WithMessage(err, "failed get account")
}
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, nil)
}

0 comments on commit 72a7659

Please sign in to comment.