From ec89bb70c7a51b20367648391c615672005d432b Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Fri, 10 Jun 2022 21:00:51 +0800 Subject: [PATCH] feat: fs and operations --- drivers/local/driver.go | 4 +- internal/driver/driver.go | 10 ++--- internal/driver/link.go | 1 - internal/fs/error.go | 7 +++ internal/fs/get.go | 16 ------- internal/fs/{list.go => read.go} | 16 +++++++ internal/fs/write.go | 77 ++++++++++++++++++++++++++++++++ internal/operations/fs.go | 30 +++++++++++++ 8 files changed, 137 insertions(+), 24 deletions(-) create mode 100644 internal/fs/error.go delete mode 100644 internal/fs/get.go rename internal/fs/{list.go => read.go} (62%) create mode 100644 internal/fs/write.go diff --git a/drivers/local/driver.go b/drivers/local/driver.go index dda37e82..1e5a328c 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -59,7 +59,7 @@ func (d *Driver) List(ctx context.Context, path string) ([]driver.FileInfo, erro panic("implement me") } -func (d *Driver) Link(ctx context.Context, args driver.LinkArgs) (*driver.Link, error) { +func (d *Driver) Link(ctx context.Context, path string, args driver.LinkArgs) (*driver.Link, error) { //TODO implement me panic("implement me") } @@ -94,7 +94,7 @@ func (d *Driver) Remove(ctx context.Context, path string) error { panic("implement me") } -func (d *Driver) Put(ctx context.Context, stream driver.FileStream, parentPath string) error { +func (d *Driver) Put(ctx context.Context, parentPath string, stream driver.FileStream) error { //TODO implement me panic("implement me") } diff --git a/internal/driver/driver.go b/internal/driver/driver.go index 2e992635..6ec870c8 100644 --- a/internal/driver/driver.go +++ b/internal/driver/driver.go @@ -30,14 +30,14 @@ type Other interface { type Reader interface { Get(ctx context.Context, path string) (FileInfo, error) List(ctx context.Context, path string) ([]FileInfo, error) - Link(ctx context.Context, args LinkArgs) (*Link, error) + Link(ctx context.Context, path string, args LinkArgs) (*Link, error) } type Writer interface { MakeDir(ctx context.Context, path string) error - Move(ctx context.Context, src, dst string) error - Rename(ctx context.Context, src, dst string) error - Copy(ctx context.Context, src, dst string) error + Move(ctx context.Context, srcPath, dstPath string) error + Rename(ctx context.Context, srcPath, dstName string) error + Copy(ctx context.Context, srcPath, dstPath string) error Remove(ctx context.Context, path string) error - Put(ctx context.Context, stream FileStream, parentPath string) error + Put(ctx context.Context, parentPath string, stream FileStream) error } diff --git a/internal/driver/link.go b/internal/driver/link.go index 1bdc4d08..88f1b53b 100644 --- a/internal/driver/link.go +++ b/internal/driver/link.go @@ -6,7 +6,6 @@ import ( ) type LinkArgs struct { - Path string IP string Header http.Header } diff --git a/internal/fs/error.go b/internal/fs/error.go new file mode 100644 index 00000000..d37edb8b --- /dev/null +++ b/internal/fs/error.go @@ -0,0 +1,7 @@ +package fs + +import "errors" + +var ( + ErrMoveBetwwenTwoAccounts = errors.New("can't move files between two account, try to copy") +) diff --git a/internal/fs/get.go b/internal/fs/get.go deleted file mode 100644 index e06e7e12..00000000 --- a/internal/fs/get.go +++ /dev/null @@ -1,16 +0,0 @@ -package fs - -import ( - "context" - "github.com/alist-org/alist/v3/internal/driver" - "github.com/alist-org/alist/v3/internal/operations" - "github.com/pkg/errors" -) - -func Get(ctx context.Context, path string) (driver.FileInfo, error) { - account, actualPath, err := operations.GetAccountAndActualPath(path) - if err != nil { - return nil, errors.WithMessage(err, "failed get account") - } - return operations.Get(ctx, account, actualPath) -} diff --git a/internal/fs/list.go b/internal/fs/read.go similarity index 62% rename from internal/fs/list.go rename to internal/fs/read.go index 64ea98e7..1796aaf2 100644 --- a/internal/fs/list.go +++ b/internal/fs/read.go @@ -35,3 +35,19 @@ func List(ctx context.Context, path string) ([]driver.FileInfo, error) { } return files, nil } + +func Get(ctx context.Context, path string) (driver.FileInfo, error) { + account, actualPath, err := operations.GetAccountAndActualPath(path) + if err != nil { + return nil, errors.WithMessage(err, "failed get account") + } + return operations.Get(ctx, account, actualPath) +} + +func Link(ctx context.Context, path string, args driver.LinkArgs) (*driver.Link, error) { + account, actualPath, err := operations.GetAccountAndActualPath(path) + if err != nil { + return nil, errors.WithMessage(err, "failed get account") + } + return operations.Link(ctx, account, actualPath, args) +} diff --git a/internal/fs/write.go b/internal/fs/write.go new file mode 100644 index 00000000..d144116e --- /dev/null +++ b/internal/fs/write.go @@ -0,0 +1,77 @@ +package fs + +import ( + "context" + "github.com/alist-org/alist/v3/internal/driver" + "github.com/alist-org/alist/v3/internal/operations" + "github.com/pkg/errors" +) + +func MakeDir(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.MakeDir(ctx, account, actualPath) +} + +func Move(ctx context.Context, account driver.Driver, srcPath, dstPath string) error { + srcAccount, srcActualPath, err := operations.GetAccountAndActualPath(srcPath) + if err != nil { + return errors.WithMessage(err, "failed get src account") + } + dstAccount, dstActualPath, err := operations.GetAccountAndActualPath(srcPath) + if err != nil { + return errors.WithMessage(err, "failed get dst account") + } + if srcAccount.GetAccount() != dstAccount.GetAccount() { + return errors.WithStack(ErrMoveBetwwenTwoAccounts) + } + return operations.Move(ctx, account, srcActualPath, dstActualPath) +} + +func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string) error { + account, srcActualPath, err := operations.GetAccountAndActualPath(srcPath) + if err != nil { + return errors.WithMessage(err, "failed get account") + } + return operations.Rename(ctx, account, srcActualPath, dstName) +} + +// Copy if in an account, call move method +// TODO: if not, add copy task +func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) error { + srcAccount, srcActualPath, err := operations.GetAccountAndActualPath(srcPath) + if err != nil { + return errors.WithMessage(err, "failed get src account") + } + dstAccount, dstActualPath, err := operations.GetAccountAndActualPath(srcPath) + if err != nil { + return errors.WithMessage(err, "failed get dst account") + } + if srcAccount.GetAccount() == dstAccount.GetAccount() { + return operations.Copy(ctx, account, srcActualPath, dstActualPath) + } + // 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 { + account, actualPath, err := operations.GetAccountAndActualPath(path) + if err != nil { + return errors.WithMessage(err, "failed get account") + } + return operations.Remove(ctx, account, actualPath) +} + +func Put(ctx context.Context, account driver.Driver, parentPath string, file driver.FileStream) error { + account, actualParentPath, err := operations.GetAccountAndActualPath(parentPath) + if err != nil { + return errors.WithMessage(err, "failed get account") + } + return operations.Put(ctx, account, actualParentPath, file) +} diff --git a/internal/operations/fs.go b/internal/operations/fs.go index 446b36e1..2da97689 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -16,3 +16,33 @@ func List(ctx context.Context, account driver.Driver, path string) ([]driver.Fil func Get(ctx context.Context, account driver.Driver, path string) (driver.FileInfo, error) { return account.Get(ctx, path) } + +// Link get link, if is a url. show have an expiry time +func Link(ctx context.Context, account driver.Driver, path string, args driver.LinkArgs) (*driver.Link, error) { + return account.Link(ctx, path, args) +} + +func MakeDir(ctx context.Context, account driver.Driver, path string) error { + return account.MakeDir(ctx, path) +} + +func Move(ctx context.Context, account driver.Driver, srcPath, dstPath string) error { + return account.Move(ctx, srcPath, dstPath) +} + +func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string) error { + return account.Rename(ctx, srcPath, dstName) +} + +// Copy Just copy file[s] in an account +func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) error { + return account.Copy(ctx, srcPath, dstPath) +} + +func Remove(ctx context.Context, account driver.Driver, path string) error { + return account.Remove(ctx, path) +} + +func Put(ctx context.Context, account driver.Driver, parentPath string, file driver.FileStream) error { + return account.Put(ctx, parentPath, file) +}