From 122b7baa739f44d5e3f5bb1221d1718ce9965bf6 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Fri, 10 Jun 2022 17:18:27 +0800 Subject: [PATCH] feat(fs): list files --- internal/operations/account.go | 12 +++++------- internal/operations/fs.go | 32 ++++++++++++++++++++++++++++++++ internal/operations/fsutil.go | 14 ++++++++++++++ internal/operations/path.go | 22 ++++++++++++++++++++++ 4 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 internal/operations/fsutil.go create mode 100644 internal/operations/path.go diff --git a/internal/operations/account.go b/internal/operations/account.go index f49f3aea31c..6d03ed04812 100644 --- a/internal/operations/account.go +++ b/internal/operations/account.go @@ -7,7 +7,6 @@ import ( "github.com/alist-org/alist/v3/internal/store" "github.com/alist-org/alist/v3/pkg/utils" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "sort" "strings" "sync" @@ -132,10 +131,10 @@ func GetAccountsByPath(path string) []driver.Driver { return accounts } -// GetAccountFilesByPath Obtain the virtual file generated by the account according to the path +// GetAccountVirtualFilesByPath Obtain the virtual file generated by the account according to the path // for example, there are: /a/b,/a/c,/a/d/e,/a/b.balance1,/av -// GetAccountFilesByPath(/a) => b,c,d -func GetAccountFilesByPath(prefix string) []driver.FileInfo { +// GetAccountVirtualFilesByPath(/a) => b,c,d +func GetAccountVirtualFilesByPath(prefix string) []driver.FileInfo { files := make([]driver.FileInfo, 0) accounts := make([]driver.Driver, len(accountsMap)) i := 0 @@ -156,7 +155,7 @@ func GetAccountFilesByPath(prefix string) []driver.FileInfo { if utils.IsBalance(v.GetAccount().VirtualPath) { continue } - full := utils.StandardizationPath(v.GetAccount().VirtualPath) + full := v.GetAccount().VirtualPath if len(full) <= len(prefix) { continue } @@ -164,7 +163,7 @@ func GetAccountFilesByPath(prefix string) []driver.FileInfo { if !strings.HasPrefix(full, prefix+"/") && prefix != "/" { continue } - name := strings.Split(strings.TrimPrefix(strings.TrimPrefix(full, prefix), "/"), "/")[0] + name := strings.Split(strings.TrimPrefix(full, prefix), "/")[0] if _, ok := set[name]; ok { continue } @@ -201,7 +200,6 @@ func GetBalancedAccount(path string) driver.Driver { } else { balanceMap.Store(virtualPath, i) } - log.Debugln("use: ", i) return accounts[i] } } diff --git a/internal/operations/fs.go b/internal/operations/fs.go index 19c01a9d10b..c1089cafabf 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -1 +1,33 @@ package operations + +import ( + "context" + "github.com/alist-org/alist/v3/internal/driver" + "github.com/pkg/errors" + log "github.com/sirupsen/logrus" +) + +func List(ctx context.Context, path string) ([]driver.FileInfo, error) { + account, actualPath, err := GetAccountAndActualPath(path) + virtualFiles := GetAccountVirtualFilesByPath(path) + if err != nil { + if len(virtualFiles) != 0 { + return virtualFiles, nil + } + return nil, errors.WithMessage(err, "failed get account") + } + files, err := account.List(ctx, actualPath) + if err != nil { + log.Errorf("%+v", err) + if len(virtualFiles) != 0 { + return virtualFiles, nil + } + return nil, errors.WithMessage(err, "failed get files") + } + for _, accountFile := range virtualFiles { + if !containsByName(files, accountFile) { + files = append(files, accountFile) + } + } + return files, nil +} diff --git a/internal/operations/fsutil.go b/internal/operations/fsutil.go new file mode 100644 index 00000000000..94ed3d195cb --- /dev/null +++ b/internal/operations/fsutil.go @@ -0,0 +1,14 @@ +package operations + +import ( + "github.com/alist-org/alist/v3/internal/driver" +) + +func containsByName(files []driver.FileInfo, file driver.FileInfo) bool { + for _, f := range files { + if f.GetName() == file.GetName() { + return true + } + } + return false +} diff --git a/internal/operations/path.go b/internal/operations/path.go new file mode 100644 index 00000000000..5346bac681e --- /dev/null +++ b/internal/operations/path.go @@ -0,0 +1,22 @@ +package operations + +import ( + "github.com/alist-org/alist/v3/internal/driver" + "github.com/alist-org/alist/v3/pkg/utils" + "github.com/pkg/errors" + log "github.com/sirupsen/logrus" + "strings" +) + +// 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) + if account == nil { + return nil, "", errors.Errorf("can't find account with path: %s", path) + } + log.Debugln("use account: ", account.GetAccount().VirtualPath) + virtualPath := utils.GetActualVirtualPath(account.GetAccount().VirtualPath) + actualPath := utils.StandardizationPath(strings.TrimPrefix(path, virtualPath)) + return account, actualPath, nil +}