Skip to content

Commit

Permalink
feat: add root prefix before operate
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 10, 2022
1 parent 354dee6 commit cd7e997
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion drivers/local/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type Addition struct {
RootFolder string `json:"root_folder" help:"root folder path" default:"/"`
driver.RootFolderPath
}

var config = driver.Config{
Expand Down
12 changes: 12 additions & 0 deletions internal/driver/addition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/fs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions internal/fs/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions internal/operations/fs.go
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 14 additions & 5 deletions internal/operations/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit cd7e997

Please sign in to comment.