mirror of https://github.com/Xhofe/alist
				
				
				
			feat(fs): list files
							parent
							
								
									c5e5666b64
								
							
						
					
					
						commit
						122b7baa73
					
				| 
						 | 
				
			
			@ -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]
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue