Skip to content

Commit

Permalink
feat: improve driver
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 7, 2022
1 parent 0d93a6a commit 677047c
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 27 deletions.
43 changes: 28 additions & 15 deletions drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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")
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions drivers/local/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ var config = driver.Config{
func New() driver.Driver {
return &Driver{}
}

func init() {
driver.RegisterDriver(config, New)
}
9 changes: 5 additions & 4 deletions internal/driver/config.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 8 additions & 3 deletions internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/driver/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
13 changes: 11 additions & 2 deletions internal/driver/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ import (
)

type FileInfo interface {
GetSize() uint64
GetName() string
GetModTime() time.Time
GetSize() int64
ModTime() time.Time
IsDir() bool
}

type FileStream interface {
io.ReadCloser
FileInfo
GetMimetype() string
}

type URL interface {
URL() string
}

type Thumbnail interface {
Thumbnail() string
}
6 changes: 3 additions & 3 deletions internal/driver/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 26 additions & 0 deletions internal/model/file.go
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.

0 comments on commit 677047c

Please sign in to comment.