mirror of https://github.com/Xhofe/alist
feat: extract get function
parent
ec89bb70c7
commit
77b0c69112
|
@ -6,8 +6,6 @@ import (
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Driver struct {
|
type Driver struct {
|
||||||
|
@ -37,23 +35,6 @@ func (d *Driver) GetAddition() driver.Additional {
|
||||||
return d.Addition
|
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) {
|
func (d *Driver) List(ctx context.Context, path string) ([]driver.FileInfo, error) {
|
||||||
//TODO implement me
|
//TODO implement me
|
||||||
panic("implement me")
|
panic("implement me")
|
||||||
|
|
|
@ -27,13 +27,25 @@ type Items struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type IRootFolderPath interface {
|
type IRootFolderPath interface {
|
||||||
GetRootFolder() string
|
GetRootFolderPath() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type IRootFolderId interface {
|
||||||
|
GetRootFolderId() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type RootFolderPath struct {
|
type RootFolderPath struct {
|
||||||
RootFolder string `json:"root_folder" help:"root folder path" default:"/"`
|
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
|
return r.RootFolder
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,9 +28,9 @@ type Other interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Reader interface {
|
type Reader interface {
|
||||||
Get(ctx context.Context, path string) (FileInfo, error)
|
|
||||||
List(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)
|
Link(ctx context.Context, path string, args LinkArgs) (*Link, error)
|
||||||
|
//Get(ctx context.Context, path string) (FileInfo, error) // maybe not need
|
||||||
}
|
}
|
||||||
|
|
||||||
type Writer interface {
|
type Writer interface {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/alist-org/alist/v3/internal/operations"
|
"github.com/alist-org/alist/v3/internal/operations"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
stdpath "path"
|
||||||
)
|
)
|
||||||
|
|
||||||
// List files
|
// 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) {
|
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)
|
account, actualPath, err := operations.GetAccountAndActualPath(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.WithMessage(err, "failed get account")
|
return nil, errors.WithMessage(err, "failed get account")
|
||||||
|
|
|
@ -24,3 +24,8 @@ func (f File) ModTime() time.Time {
|
||||||
func (f File) IsDir() bool {
|
func (f File) IsDir() bool {
|
||||||
return f.IsFolder
|
return f.IsFolder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FileWithId struct {
|
||||||
|
Id string
|
||||||
|
File
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,10 @@ package operations
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/alist-org/alist/v3/internal/driver"
|
"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
|
// 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) {
|
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
|
// Link get link, if is a url. show have an expiry time
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
func ActualPath(account driver.Additional, rawPath string) string {
|
func ActualPath(account driver.Additional, rawPath string) string {
|
||||||
if i, ok := account.(driver.IRootFolderPath); ok {
|
if i, ok := account.(driver.IRootFolderPath); ok {
|
||||||
rawPath = path.Join(i.GetRootFolder(), rawPath)
|
rawPath = path.Join(i.GetRootFolderPath(), rawPath)
|
||||||
}
|
}
|
||||||
return utils.StandardizationPath(rawPath)
|
return utils.StandardizationPath(rawPath)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,3 +10,7 @@ func StandardizationPath(path string) string {
|
||||||
}
|
}
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PathEqual(path1, path2 string) bool {
|
||||||
|
return StandardizationPath(path1) == StandardizationPath(path2)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue