Skip to content

Commit

Permalink
feat(fs): list files
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 10, 2022
1 parent c5e5666 commit 122b7ba
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
12 changes: 5 additions & 7 deletions internal/operations/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -156,15 +155,15 @@ 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
}
// not prefixed with `prefix`
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
}
Expand Down Expand Up @@ -201,7 +200,6 @@ func GetBalancedAccount(path string) driver.Driver {
} else {
balanceMap.Store(virtualPath, i)
}
log.Debugln("use: ", i)
return accounts[i]
}
}
32 changes: 32 additions & 0 deletions internal/operations/fs.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions internal/operations/fsutil.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions internal/operations/path.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 122b7ba

Please sign in to comment.