Skip to content

Commit

Permalink
feat: copy files between 2 accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 15, 2022
1 parent 4fa7846 commit 2d60dab
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
41 changes: 41 additions & 0 deletions internal/fs/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fs

import (
"context"
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/pkg/errors"
)

func CopyBetween2Accounts(ctx context.Context, srcAccount, dstAccount driver.Driver, srcPath, dstPath string) error {
srcFile, err := operations.Get(ctx, srcAccount, srcPath)
if err != nil {
return errors.WithMessagef(err, "failed get src [%s] file", srcPath)
}
if srcFile.IsDir() {
files, err := operations.List(ctx, srcAccount, srcPath)
if err != nil {
return errors.WithMessagef(err, "failed list src [%s] files", srcPath)
}
for _, file := range files {
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)
}
}
}
link, err := operations.Link(ctx, srcAccount, srcPath, model.LinkArgs{})
if err != nil {
return errors.WithMessagef(err, "failed get [%s] link", srcPath)
}
stream, err := getFileStreamFromLink(srcFile, link)
if err != nil {
return errors.WithMessagef(err, "failed get [%s] stream", srcPath)
}
// TODO add as task
return operations.Put(ctx, dstAccount, dstPath, stream)
}
25 changes: 13 additions & 12 deletions internal/fs/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) e
if srcAccount.GetAccount() == dstAccount.GetAccount() {
return operations.Copy(ctx, account, srcActualPath, dstActualPath)
}
// not in an account, add copy task
srcFile, err := operations.Get(ctx, srcAccount, srcActualPath)
if srcFile.IsDir() {
// TODO: recursive copy
return nil
}
// TODO: add copy task, maybe like this:
// operations.Link(ctx,srcAccount,srcActualPath,args)
// get a Reader from link
// boxing the Reader to a driver.FileStream
// operations.Put(ctx,dstParentPath, stream)
panic("TODO")
// not in an account
return CopyBetween2Accounts(ctx, srcAccount, dstAccount, srcActualPath, dstActualPath)
// srcFile, err := operations.Get(ctx, srcAccount, srcActualPath)
// if srcFile.IsDir() {
// // TODO: recursive copy
// return nil
// }
// // TODO: add copy task, maybe like this:
// // operations.Link(ctx,srcAccount,srcActualPath,args)
// // get a Reader from link
// // boxing the Reader to a driver.FileStream
// // operations.Put(ctx,dstParentPath, stream)
// panic("TODO")
}

func Remove(ctx context.Context, account driver.Driver, path string) error {
Expand Down

0 comments on commit 2d60dab

Please sign in to comment.