From cd7e9974dffa28642adad957250402ceea977cae Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Fri, 10 Jun 2022 20:20:45 +0800 Subject: [PATCH] feat: add root prefix before operate --- drivers/local/driver.go | 2 +- drivers/local/meta.go | 2 +- internal/driver/addition.go | 12 ++++++++++++ internal/driver/driver.go | 2 +- internal/fs/get.go | 2 +- internal/fs/list.go | 3 +-- internal/fs/{fsutil.go => util.go} | 0 internal/operations/fs.go | 18 ++++++++++++++++++ internal/operations/path.go | 19 ++++++++++++++----- 9 files changed, 49 insertions(+), 11 deletions(-) rename internal/fs/{fsutil.go => util.go} (100%) create mode 100644 internal/operations/fs.go diff --git a/drivers/local/driver.go b/drivers/local/driver.go index 7beda430..dda37e82 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -37,7 +37,7 @@ func (d *Driver) GetAddition() driver.Additional { return d.Addition } -func (d *Driver) File(ctx context.Context, path string) (driver.FileInfo, error) { +func (d *Driver) Get(ctx context.Context, path string) (driver.FileInfo, error) { fullPath := filepath.Join(d.RootFolder, path) if !utils.Exists(fullPath) { return nil, errors.WithStack(driver.ErrorObjectNotFound) diff --git a/drivers/local/meta.go b/drivers/local/meta.go index 39efdf4b..e1d8e3f8 100644 --- a/drivers/local/meta.go +++ b/drivers/local/meta.go @@ -6,7 +6,7 @@ import ( ) type Addition struct { - RootFolder string `json:"root_folder" help:"root folder path" default:"/"` + driver.RootFolderPath } var config = driver.Config{ diff --git a/internal/driver/addition.go b/internal/driver/addition.go index 304ebdd2..eecf87ab 100644 --- a/internal/driver/addition.go +++ b/internal/driver/addition.go @@ -25,3 +25,15 @@ type Items struct { Main []Item `json:"main"` Additional []Item `json:"additional"` } + +type IRootFolderPath interface { + GetRootFolder() string +} + +type RootFolderPath struct { + RootFolder string `json:"root_folder" help:"root folder path" default:"/"` +} + +func (r RootFolderPath) GetRootFolder() string { + return r.RootFolder +} diff --git a/internal/driver/driver.go b/internal/driver/driver.go index 9eec0c87..2e992635 100644 --- a/internal/driver/driver.go +++ b/internal/driver/driver.go @@ -28,7 +28,7 @@ type Other interface { } type Reader interface { - File(ctx context.Context, path string) (FileInfo, error) + Get(ctx context.Context, path string) (FileInfo, error) List(ctx context.Context, path string) ([]FileInfo, error) Link(ctx context.Context, args LinkArgs) (*Link, error) } diff --git a/internal/fs/get.go b/internal/fs/get.go index f6274b3a..e06e7e12 100644 --- a/internal/fs/get.go +++ b/internal/fs/get.go @@ -12,5 +12,5 @@ func Get(ctx context.Context, path string) (driver.FileInfo, error) { if err != nil { return nil, errors.WithMessage(err, "failed get account") } - return account.File(ctx, actualPath) + return operations.Get(ctx, account, actualPath) } diff --git a/internal/fs/list.go b/internal/fs/list.go index 369b3c6d..64ea98e7 100644 --- a/internal/fs/list.go +++ b/internal/fs/list.go @@ -11,7 +11,6 @@ import ( // List files // TODO: hide // TODO: sort -// TODO: cache, and prevent cache breakdown func List(ctx context.Context, path string) ([]driver.FileInfo, error) { account, actualPath, err := operations.GetAccountAndActualPath(path) virtualFiles := operations.GetAccountVirtualFilesByPath(path) @@ -21,7 +20,7 @@ func List(ctx context.Context, path string) ([]driver.FileInfo, error) { } return nil, errors.WithMessage(err, "failed get account") } - files, err := account.List(ctx, actualPath) + files, err := operations.List(ctx, account, actualPath) if err != nil { log.Errorf("%+v", err) if len(virtualFiles) != 0 { diff --git a/internal/fs/fsutil.go b/internal/fs/util.go similarity index 100% rename from internal/fs/fsutil.go rename to internal/fs/util.go diff --git a/internal/operations/fs.go b/internal/operations/fs.go new file mode 100644 index 00000000..446b36e1 --- /dev/null +++ b/internal/operations/fs.go @@ -0,0 +1,18 @@ +package operations + +import ( + "context" + "github.com/alist-org/alist/v3/internal/driver" +) + +// In order to facilitate adding some other things before and after file operations + +// List files in storage, not contains virtual file +// TODO: cache, and prevent cache breakdown +func List(ctx context.Context, account driver.Driver, path string) ([]driver.FileInfo, error) { + return account.List(ctx, path) +} + +func Get(ctx context.Context, account driver.Driver, path string) (driver.FileInfo, error) { + return account.Get(ctx, path) +} diff --git a/internal/operations/path.go b/internal/operations/path.go index 5346bac6..445e9af7 100644 --- a/internal/operations/path.go +++ b/internal/operations/path.go @@ -5,18 +5,27 @@ import ( "github.com/alist-org/alist/v3/pkg/utils" "github.com/pkg/errors" log "github.com/sirupsen/logrus" + "path" "strings" ) +func ActualPath(account driver.Additional, rawPath string) string { + if i, ok := account.(driver.IRootFolderPath); ok { + rawPath = path.Join(i.GetRootFolder(), rawPath) + } + return utils.StandardizationPath(rawPath) +} + // GetAccountAndActualPath Get the corresponding account, and remove the virtual path prefix in path -func GetAccountAndActualPath(path string) (driver.Driver, string, error) { - path = utils.StandardizationPath(path) - account := GetBalancedAccount(path) +func GetAccountAndActualPath(rawPath string) (driver.Driver, string, error) { + rawPath = utils.StandardizationPath(rawPath) + account := GetBalancedAccount(rawPath) if account == nil { - return nil, "", errors.Errorf("can't find account with path: %s", path) + return nil, "", errors.Errorf("can't find account with rawPath: %s", rawPath) } log.Debugln("use account: ", account.GetAccount().VirtualPath) virtualPath := utils.GetActualVirtualPath(account.GetAccount().VirtualPath) - actualPath := utils.StandardizationPath(strings.TrimPrefix(path, virtualPath)) + actualPath := strings.TrimPrefix(rawPath, virtualPath) + actualPath = ActualPath(account.GetAddition(), actualPath) return account, actualPath, nil }