feat: extract get function

refactor/fs
Noah Hsu 2022-06-11 14:43:03 +08:00
parent ec89bb70c7
commit 77b0c69112
8 changed files with 66 additions and 24 deletions

View File

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

View File

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

View File

@ -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 {

View File

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

View File

@ -24,3 +24,8 @@ func (f File) ModTime() time.Time {
func (f File) IsDir() bool {
return f.IsFolder
}
type FileWithId struct {
Id string
File
}

View File

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

View File

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

View File

@ -10,3 +10,7 @@ func StandardizationPath(path string) string {
}
return path
}
func PathEqual(path1, path2 string) bool {
return StandardizationPath(path1) == StandardizationPath(path2)
}