Skip to content

Commit

Permalink
feat: extract get function
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 11, 2022
1 parent ec89bb7 commit 77b0c69
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 24 deletions.
19 changes: 0 additions & 19 deletions drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
16 changes: 14 additions & 2 deletions internal/driver/addition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions internal/fs/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions internal/model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ func (f File) ModTime() time.Time {
func (f File) IsDir() bool {
return f.IsFolder
}

type FileWithId struct {
Id string
File
}
35 changes: 34 additions & 1 deletion internal/operations/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/operations/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ func StandardizationPath(path string) string {
}
return path
}

func PathEqual(path1, path2 string) bool {
return StandardizationPath(path1) == StandardizationPath(path2)
}

0 comments on commit 77b0c69

Please sign in to comment.