Skip to content

Commit 677047c

Browse files
committed
feat: improve driver
1 parent 0d93a6a commit 677047c

File tree

9 files changed

+88
-27
lines changed

9 files changed

+88
-27
lines changed

drivers/local/driver.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package local
22

33
import (
44
"context"
5-
"errors"
5+
"fmt"
66
"github.com/alist-org/alist/v3/internal/driver"
77
"github.com/alist-org/alist/v3/internal/model"
88
"github.com/alist-org/alist/v3/pkg/utils"
9+
"os"
10+
"path/filepath"
911
)
1012

1113
type Driver struct {
@@ -18,32 +20,42 @@ func (d Driver) Config() driver.Config {
1820
}
1921

2022
func (d *Driver) Init(ctx context.Context, account model.Account) error {
23+
d.Account = account
2124
addition := d.Account.Addition
2225
err := utils.Json.UnmarshalFromString(addition, d.Addition)
2326
if err != nil {
24-
return errors.New("error")
27+
return fmt.Errorf("error while unmarshal addition: %w", err)
2528
}
2629
return nil
2730
}
2831

2932
func (d *Driver) Update(ctx context.Context, account model.Account) error {
30-
//TODO implement me
31-
panic("implement me")
33+
return d.Init(ctx, account)
3234
}
3335

3436
func (d *Driver) Drop(ctx context.Context) error {
35-
//TODO implement me
36-
panic("implement me")
37+
return nil
3738
}
3839

3940
func (d *Driver) GetAccount() model.Account {
40-
//TODO implement me
41-
panic("implement me")
41+
return d.Account
4242
}
4343

44-
func (d *Driver) File(ctx context.Context, path string) (*driver.FileInfo, error) {
45-
//TODO implement me
46-
panic("implement me")
44+
func (d *Driver) File(ctx context.Context, path string) (driver.FileInfo, error) {
45+
fullPath := filepath.Join(d.RootFolder, path)
46+
if !utils.Exists(fullPath) {
47+
return nil, driver.ErrorObjectNotFound
48+
}
49+
f, err := os.Stat(fullPath)
50+
if err != nil {
51+
return nil, err
52+
}
53+
return model.File{
54+
Name: f.Name(),
55+
Size: uint64(f.Size()),
56+
Modified: f.ModTime(),
57+
IsFolder: f.IsDir(),
58+
}, nil
4759
}
4860

4961
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,
5668
panic("implement me")
5769
}
5870

71+
func (d Driver) Other(ctx context.Context, data interface{}) (interface{}, error) {
72+
//TODO implement me
73+
panic("implement me")
74+
}
75+
5976
func (d *Driver) MakeDir(ctx context.Context, path string) error {
6077
//TODO implement me
6178
panic("implement me")
@@ -87,7 +104,3 @@ func (d *Driver) Put(ctx context.Context, stream driver.FileStream, parentPath s
87104
}
88105

89106
var _ driver.Driver = (*Driver)(nil)
90-
91-
func init() {
92-
driver.RegisterDriver(config.Name, New)
93-
}

drivers/local/meta.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ var config = driver.Config{
1515
func New() driver.Driver {
1616
return &Driver{}
1717
}
18+
19+
func init() {
20+
driver.RegisterDriver(config, New)
21+
}

internal/driver/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package driver
22

33
type Config struct {
4-
Name string
5-
LocalSort bool
6-
OnlyLocal bool
7-
OnlyProxy bool
4+
Name string
5+
LocalSort bool
6+
OnlyLocal bool
7+
OnlyProxy bool
8+
NoNeedSetLink bool
89
}

internal/driver/driver.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import (
66
)
77

88
type Driver interface {
9-
Other
9+
Meta
1010
Reader
1111
Writer
12+
Other
1213
}
1314

14-
type Other interface {
15+
type Meta interface {
1516
Config() Config
1617
Init(ctx context.Context, account model.Account) error
1718
Update(ctx context.Context, account model.Account) error
@@ -20,8 +21,12 @@ type Other interface {
2021
GetAccount() model.Account
2122
}
2223

24+
type Other interface {
25+
Other(ctx context.Context, data interface{}) (interface{}, error)
26+
}
27+
2328
type Reader interface {
24-
File(ctx context.Context, path string) (*FileInfo, error)
29+
File(ctx context.Context, path string) (FileInfo, error)
2530
List(ctx context.Context, path string) ([]FileInfo, error)
2631
Link(ctx context.Context, args LinkArgs) (*Link, error)
2732
}

internal/driver/error.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ import "errors"
55
var (
66
ErrorDirNotFound = errors.New("directory not found")
77
ErrorObjectNotFound = errors.New("object not found")
8+
ErrNotImplement = errors.New("not implement")
9+
ErrNotSupport = errors.New("not support")
10+
ErrRelativePath = errors.New("access using relative path is not allowed")
811
)

internal/driver/file.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@ import (
66
)
77

88
type FileInfo interface {
9+
GetSize() uint64
910
GetName() string
10-
GetModTime() time.Time
11-
GetSize() int64
11+
ModTime() time.Time
12+
IsDir() bool
1213
}
1314

1415
type FileStream interface {
1516
io.ReadCloser
1617
FileInfo
1718
GetMimetype() string
1819
}
20+
21+
type URL interface {
22+
URL() string
23+
}
24+
25+
type Thumbnail interface {
26+
Thumbnail() string
27+
}

internal/driver/manage.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type New func() Driver
88

99
var driversMap = map[string]New{}
1010

11-
func RegisterDriver(name string, new New) {
12-
log.Infof("register driver: [%s]", name)
13-
driversMap[name] = new
11+
func RegisterDriver(config Config, driver New) {
12+
log.Infof("register driver: [%s]", config.Name)
13+
driversMap[config.Name] = driver
1414
}

internal/model/file.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package model
2+
3+
import "time"
4+
5+
type File struct {
6+
Name string
7+
Size uint64
8+
Modified time.Time
9+
IsFolder bool
10+
}
11+
12+
func (f File) GetName() string {
13+
return f.Name
14+
}
15+
16+
func (f File) GetSize() uint64 {
17+
return f.Size
18+
}
19+
20+
func (f File) ModTime() time.Time {
21+
return f.Modified
22+
}
23+
24+
func (f File) IsDir() bool {
25+
return f.IsFolder
26+
}

0 commit comments

Comments
 (0)