feat: improve driver

refactor/fs
Noah Hsu 2022-06-07 22:02:41 +08:00
parent 0d93a6aa41
commit 677047c80b
9 changed files with 88 additions and 27 deletions

View File

@ -2,10 +2,12 @@ package local
import (
"context"
"errors"
"fmt"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"os"
"path/filepath"
)
type Driver struct {
@ -18,32 +20,42 @@ func (d Driver) Config() driver.Config {
}
func (d *Driver) Init(ctx context.Context, account model.Account) error {
d.Account = account
addition := d.Account.Addition
err := utils.Json.UnmarshalFromString(addition, d.Addition)
if err != nil {
return errors.New("error")
return fmt.Errorf("error while unmarshal addition: %w", err)
}
return nil
}
func (d *Driver) Update(ctx context.Context, account model.Account) error {
//TODO implement me
panic("implement me")
return d.Init(ctx, account)
}
func (d *Driver) Drop(ctx context.Context) error {
//TODO implement me
panic("implement me")
return nil
}
func (d *Driver) GetAccount() model.Account {
//TODO implement me
panic("implement me")
return d.Account
}
func (d *Driver) File(ctx context.Context, path string) (*driver.FileInfo, error) {
//TODO implement me
panic("implement me")
func (d *Driver) File(ctx context.Context, path string) (driver.FileInfo, error) {
fullPath := filepath.Join(d.RootFolder, path)
if !utils.Exists(fullPath) {
return nil, 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) {
@ -56,6 +68,11 @@ func (d *Driver) Link(ctx context.Context, args driver.LinkArgs) (*driver.Link,
panic("implement me")
}
func (d Driver) Other(ctx context.Context, data interface{}) (interface{}, error) {
//TODO implement me
panic("implement me")
}
func (d *Driver) MakeDir(ctx context.Context, path string) error {
//TODO implement me
panic("implement me")
@ -87,7 +104,3 @@ func (d *Driver) Put(ctx context.Context, stream driver.FileStream, parentPath s
}
var _ driver.Driver = (*Driver)(nil)
func init() {
driver.RegisterDriver(config.Name, New)
}

View File

@ -15,3 +15,7 @@ var config = driver.Config{
func New() driver.Driver {
return &Driver{}
}
func init() {
driver.RegisterDriver(config, New)
}

View File

@ -1,8 +1,9 @@
package driver
type Config struct {
Name string
LocalSort bool
OnlyLocal bool
OnlyProxy bool
Name string
LocalSort bool
OnlyLocal bool
OnlyProxy bool
NoNeedSetLink bool
}

View File

@ -6,12 +6,13 @@ import (
)
type Driver interface {
Other
Meta
Reader
Writer
Other
}
type Other interface {
type Meta interface {
Config() Config
Init(ctx context.Context, account model.Account) error
Update(ctx context.Context, account model.Account) error
@ -20,8 +21,12 @@ type Other interface {
GetAccount() model.Account
}
type Other interface {
Other(ctx context.Context, data interface{}) (interface{}, error)
}
type Reader interface {
File(ctx context.Context, path string) (*FileInfo, error)
File(ctx context.Context, path string) (FileInfo, error)
List(ctx context.Context, path string) ([]FileInfo, error)
Link(ctx context.Context, args LinkArgs) (*Link, error)
}

View File

@ -5,4 +5,7 @@ import "errors"
var (
ErrorDirNotFound = errors.New("directory not found")
ErrorObjectNotFound = errors.New("object not found")
ErrNotImplement = errors.New("not implement")
ErrNotSupport = errors.New("not support")
ErrRelativePath = errors.New("access using relative path is not allowed")
)

View File

@ -6,9 +6,10 @@ import (
)
type FileInfo interface {
GetSize() uint64
GetName() string
GetModTime() time.Time
GetSize() int64
ModTime() time.Time
IsDir() bool
}
type FileStream interface {
@ -16,3 +17,11 @@ type FileStream interface {
FileInfo
GetMimetype() string
}
type URL interface {
URL() string
}
type Thumbnail interface {
Thumbnail() string
}

View File

@ -8,7 +8,7 @@ type New func() Driver
var driversMap = map[string]New{}
func RegisterDriver(name string, new New) {
log.Infof("register driver: [%s]", name)
driversMap[name] = new
func RegisterDriver(config Config, driver New) {
log.Infof("register driver: [%s]", config.Name)
driversMap[config.Name] = driver
}

26
internal/model/file.go Normal file
View File

@ -0,0 +1,26 @@
package model
import "time"
type File struct {
Name string
Size uint64
Modified time.Time
IsFolder bool
}
func (f File) GetName() string {
return f.Name
}
func (f File) GetSize() uint64 {
return f.Size
}
func (f File) ModTime() time.Time {
return f.Modified
}
func (f File) IsDir() bool {
return f.IsFolder
}