-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
driver.go
112 lines (91 loc) · 2.39 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package virtual
import (
"context"
"io"
"time"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils/random"
)
type Virtual struct {
model.Storage
Addition
}
func (d *Virtual) Config() driver.Config {
return config
}
func (d *Virtual) Init(ctx context.Context) error {
return nil
}
func (d *Virtual) Drop(ctx context.Context) error {
return nil
}
func (d *Virtual) GetAddition() driver.Additional {
return &d.Addition
}
func (d *Virtual) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
var res []model.Obj
for i := 0; i < d.NumFile; i++ {
res = append(res, d.genObj(false))
}
for i := 0; i < d.NumFolder; i++ {
res = append(res, d.genObj(true))
}
return res, nil
}
type DummyMFile struct {
io.Reader
}
func (f DummyMFile) Read(p []byte) (n int, err error) {
return f.Reader.Read(p)
}
func (f DummyMFile) ReadAt(p []byte, off int64) (n int, err error) {
return f.Reader.Read(p)
}
func (f DummyMFile) Close() error {
return nil
}
func (DummyMFile) Seek(offset int64, whence int) (int64, error) {
return offset, nil
}
func (d *Virtual) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
return &model.Link{
MFile: DummyMFile{Reader: random.Rand},
}, nil
}
func (d *Virtual) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) {
dir := &model.Object{
Name: dirName,
Size: 0,
IsFolder: true,
Modified: time.Now(),
}
return dir, nil
}
func (d *Virtual) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
return srcObj, nil
}
func (d *Virtual) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) {
obj := &model.Object{
Name: newName,
Size: srcObj.GetSize(),
IsFolder: srcObj.IsDir(),
Modified: time.Now(),
}
return obj, nil
}
func (d *Virtual) Copy(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
return srcObj, nil
}
func (d *Virtual) Remove(ctx context.Context, obj model.Obj) error {
return nil
}
func (d *Virtual) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) {
file := &model.Object{
Name: stream.GetName(),
Size: stream.GetSize(),
Modified: time.Now(),
}
return file, nil
}
var _ driver.Driver = (*Virtual)(nil)