From 77b0c691127c5878eecd6e943c3318d933df0aed Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Sat, 11 Jun 2022 14:43:03 +0800 Subject: [PATCH] feat: extract get function --- drivers/local/driver.go | 19 ------------------- internal/driver/addition.go | 16 ++++++++++++++-- internal/driver/driver.go | 2 +- internal/fs/read.go | 7 +++++++ internal/model/file.go | 5 +++++ internal/operations/fs.go | 35 ++++++++++++++++++++++++++++++++++- internal/operations/path.go | 2 +- pkg/utils/path.go | 4 ++++ 8 files changed, 66 insertions(+), 24 deletions(-) diff --git a/drivers/local/driver.go b/drivers/local/driver.go index 1e5a328c..aeb2142a 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -6,8 +6,6 @@ import ( "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/pkg/utils" "github.com/pkg/errors" - "os" - "path/filepath" ) type Driver struct { @@ -37,23 +35,6 @@ func (d *Driver) GetAddition() driver.Additional { return d.Addition } -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) - } - f, err := os.Stat(fullPath) - if err != nil { - return nil, err - } - return model.File{ - Name: f.Name(), - Size: uint64(f.Size()), - Modified: f.ModTime(), - IsFolder: f.IsDir(), - }, nil -} - func (d *Driver) List(ctx context.Context, path string) ([]driver.FileInfo, error) { //TODO implement me panic("implement me") diff --git a/internal/driver/addition.go b/internal/driver/addition.go index eecf87ab..993e92b3 100644 --- a/internal/driver/addition.go +++ b/internal/driver/addition.go @@ -27,13 +27,25 @@ type Items struct { } type IRootFolderPath interface { - GetRootFolder() string + GetRootFolderPath() string +} + +type IRootFolderId interface { + GetRootFolderId() string } type RootFolderPath struct { RootFolder string `json:"root_folder" help:"root folder path" default:"/"` } -func (r RootFolderPath) GetRootFolder() string { +type RootFolderId struct { + RootFolder string `json:"root_folder" help:"root folder id"` +} + +func (r RootFolderPath) GetRootFolderPath() string { + return r.RootFolder +} + +func (r RootFolderId) GetRootFolderId() string { return r.RootFolder } diff --git a/internal/driver/driver.go b/internal/driver/driver.go index 6ec870c8..3f7f85ef 100644 --- a/internal/driver/driver.go +++ b/internal/driver/driver.go @@ -28,9 +28,9 @@ type Other interface { } type Reader interface { - Get(ctx context.Context, path string) (FileInfo, error) List(ctx context.Context, path string) ([]FileInfo, error) Link(ctx context.Context, path string, args LinkArgs) (*Link, error) + //Get(ctx context.Context, path string) (FileInfo, error) // maybe not need } type Writer interface { diff --git a/internal/fs/read.go b/internal/fs/read.go index 1796aaf2..8354e462 100644 --- a/internal/fs/read.go +++ b/internal/fs/read.go @@ -6,6 +6,7 @@ import ( "github.com/alist-org/alist/v3/internal/operations" "github.com/pkg/errors" log "github.com/sirupsen/logrus" + stdpath "path" ) // List files @@ -37,6 +38,12 @@ func List(ctx context.Context, path string) ([]driver.FileInfo, error) { } func Get(ctx context.Context, path string) (driver.FileInfo, error) { + virtualFiles := operations.GetAccountVirtualFilesByPath(path) + for _, f := range virtualFiles { + if f.GetName() == stdpath.Base(path) { + return f, nil + } + } account, actualPath, err := operations.GetAccountAndActualPath(path) if err != nil { return nil, errors.WithMessage(err, "failed get account") diff --git a/internal/model/file.go b/internal/model/file.go index f5b4d258..74313212 100644 --- a/internal/model/file.go +++ b/internal/model/file.go @@ -24,3 +24,8 @@ func (f File) ModTime() time.Time { func (f File) IsDir() bool { return f.IsFolder } + +type FileWithId struct { + Id string + File +} diff --git a/internal/operations/fs.go b/internal/operations/fs.go index 2da97689..c403b791 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -3,6 +3,10 @@ package operations import ( "context" "github.com/alist-org/alist/v3/internal/driver" + "github.com/alist-org/alist/v3/internal/model" + "github.com/alist-org/alist/v3/pkg/utils" + "github.com/pkg/errors" + stdpath "path" ) // In order to facilitate adding some other things before and after file operations @@ -14,7 +18,36 @@ func List(ctx context.Context, account driver.Driver, path string) ([]driver.Fil } func Get(ctx context.Context, account driver.Driver, path string) (driver.FileInfo, error) { - return account.Get(ctx, path) + if r, ok := account.GetAddition().(driver.RootFolderId); ok && utils.PathEqual(path, "/") { + return model.FileWithId{ + Id: r.GetRootFolderId(), + File: model.File{ + Name: "root", + Size: 0, + Modified: account.GetAccount().Modified, + IsFolder: true, + }, + }, nil + } + if r, ok := account.GetAddition().(driver.IRootFolderPath); ok && utils.PathEqual(path, r.GetRootFolderPath()) { + return model.File{ + Name: "root", + Size: 0, + Modified: account.GetAccount().Modified, + IsFolder: true, + }, nil + } + dir, name := stdpath.Split(path) + files, err := List(ctx, account, dir) + if err != nil { + return nil, errors.WithMessage(err, "failed get parent list") + } + for _, f := range files { + if f.GetName() == name { + return f, nil + } + } + return nil, errors.WithStack(driver.ErrorObjectNotFound) } // Link get link, if is a url. show have an expiry time diff --git a/internal/operations/path.go b/internal/operations/path.go index 445e9af7..c5e94121 100644 --- a/internal/operations/path.go +++ b/internal/operations/path.go @@ -11,7 +11,7 @@ import ( func ActualPath(account driver.Additional, rawPath string) string { if i, ok := account.(driver.IRootFolderPath); ok { - rawPath = path.Join(i.GetRootFolder(), rawPath) + rawPath = path.Join(i.GetRootFolderPath(), rawPath) } return utils.StandardizationPath(rawPath) } diff --git a/pkg/utils/path.go b/pkg/utils/path.go index e47a76dc..a3fa9ed5 100644 --- a/pkg/utils/path.go +++ b/pkg/utils/path.go @@ -10,3 +10,7 @@ func StandardizationPath(path string) string { } return path } + +func PathEqual(path1, path2 string) bool { + return StandardizationPath(path1) == StandardizationPath(path2) +}