mirror of https://github.com/Xhofe/alist
feat: add root prefix before operate
parent
354dee67dc
commit
cd7e9974df
|
@ -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)
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
)
|
||||
|
||||
type Addition struct {
|
||||
RootFolder string `json:"root_folder" help:"root folder path" default:"/"`
|
||||
driver.RootFolderPath
|
||||
}
|
||||
|
||||
var config = driver.Config{
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue