feat: fs and operations

This commit is contained in:
Noah Hsu
2022-06-10 21:00:51 +08:00
parent cd7e9974df
commit ec89bb70c7
8 changed files with 137 additions and 24 deletions

7
internal/fs/error.go Normal file
View File

@ -0,0 +1,7 @@
package fs
import "errors"
var (
ErrMoveBetwwenTwoAccounts = errors.New("can't move files between two account, try to copy")
)

View File

@ -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)
}

View File

@ -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)
}

77
internal/fs/write.go Normal file
View File

@ -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)
}