feat: add root prefix before operate

refactor/fs
Noah Hsu 2022-06-10 20:20:45 +08:00
parent 354dee67dc
commit cd7e9974df
9 changed files with 49 additions and 11 deletions

View File

@ -37,7 +37,7 @@ func (d *Driver) GetAddition() driver.Additional {
return d.Addition 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) fullPath := filepath.Join(d.RootFolder, path)
if !utils.Exists(fullPath) { if !utils.Exists(fullPath) {
return nil, errors.WithStack(driver.ErrorObjectNotFound) return nil, errors.WithStack(driver.ErrorObjectNotFound)

View File

@ -6,7 +6,7 @@ import (
) )
type Addition struct { type Addition struct {
RootFolder string `json:"root_folder" help:"root folder path" default:"/"` driver.RootFolderPath
} }
var config = driver.Config{ var config = driver.Config{

View File

@ -25,3 +25,15 @@ type Items struct {
Main []Item `json:"main"` Main []Item `json:"main"`
Additional []Item `json:"additional"` 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
}

View File

@ -28,7 +28,7 @@ type Other interface {
} }
type Reader 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) List(ctx context.Context, path string) ([]FileInfo, error)
Link(ctx context.Context, args LinkArgs) (*Link, error) Link(ctx context.Context, args LinkArgs) (*Link, error)
} }

View File

@ -12,5 +12,5 @@ func Get(ctx context.Context, path string) (driver.FileInfo, error) {
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "failed get account") return nil, errors.WithMessage(err, "failed get account")
} }
return account.File(ctx, actualPath) return operations.Get(ctx, account, actualPath)
} }

View File

@ -11,7 +11,6 @@ import (
// List files // List files
// TODO: hide // TODO: hide
// TODO: sort // TODO: sort
// TODO: cache, and prevent cache breakdown
func List(ctx context.Context, path string) ([]driver.FileInfo, error) { func List(ctx context.Context, path string) ([]driver.FileInfo, error) {
account, actualPath, err := operations.GetAccountAndActualPath(path) account, actualPath, err := operations.GetAccountAndActualPath(path)
virtualFiles := operations.GetAccountVirtualFilesByPath(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") return nil, errors.WithMessage(err, "failed get account")
} }
files, err := account.List(ctx, actualPath) files, err := operations.List(ctx, account, actualPath)
if err != nil { if err != nil {
log.Errorf("%+v", err) log.Errorf("%+v", err)
if len(virtualFiles) != 0 { if len(virtualFiles) != 0 {

18
internal/operations/fs.go Normal file
View File

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

View File

@ -5,18 +5,27 @@ import (
"github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"path"
"strings" "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 // GetAccountAndActualPath Get the corresponding account, and remove the virtual path prefix in path
func GetAccountAndActualPath(path string) (driver.Driver, string, error) { func GetAccountAndActualPath(rawPath string) (driver.Driver, string, error) {
path = utils.StandardizationPath(path) rawPath = utils.StandardizationPath(rawPath)
account := GetBalancedAccount(path) account := GetBalancedAccount(rawPath)
if account == nil { 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) log.Debugln("use account: ", account.GetAccount().VirtualPath)
virtualPath := utils.GetActualVirtualPath(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 return account, actualPath, nil
} }