Skip to content

Commit

Permalink
feat: virtual driver
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jul 19, 2022
1 parent 872e7cf commit 90a5c17
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package drivers

import (
_ "github.com/alist-org/alist/v3/drivers/local"
_ "github.com/alist-org/alist/v3/drivers/virtual"
)
100 changes: 100 additions & 0 deletions drivers/virtual/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package virtual

import (
"context"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/pkg/utils/random"
"github.com/pkg/errors"
"io"
"time"
)

type Virtual struct {
model.Storage
Addition
}

func (d *Virtual) Config() driver.Config {
return config
}

func (d *Virtual) Init(ctx context.Context, storage model.Storage) error {
d.Storage = storage
err := utils.Json.UnmarshalFromString(storage.Addition, &d.Addition)
if err != nil {
return errors.Wrap(err, "error while unmarshal addition")
}
return nil
}

func (d *Virtual) Drop(ctx context.Context) error {
return nil
}

func (d *Virtual) GetStorage() model.Storage {
return d.Storage
}

func (d *Virtual) GetAddition() driver.Additional {
return d.Addition
}

func (d *Virtual) List(ctx context.Context, dir model.Obj) ([]model.Obj, error) {
var res []model.Obj
for i := 0; i < d.NumFile; i++ {
res = append(res, model.Object{
Name: random.String(10),
Size: random.RangeInt64(d.MinFileSize, d.MaxFileSize),
IsFolder: false,
Modified: time.Now(),
})
}
for i := 0; i < d.NumFolder; i++ {
res = append(res, model.Object{
Name: random.String(10),
Size: 0,
IsFolder: true,
Modified: time.Now(),
})
}
return res, nil
}

func (d *Virtual) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
return &model.Link{
Data: io.NopCloser(io.LimitReader(random.Rand, file.GetSize())),
}, nil
}

func (d *Virtual) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
return nil
}

func (d *Virtual) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
return nil
}

func (d *Virtual) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
return nil
}

func (d *Virtual) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
return 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) error {
return nil
}

func (d *Virtual) Other(ctx context.Context, data interface{}) (interface{}, error) {
return nil, errs.NotSupport
}

var _ driver.Driver = (*Virtual)(nil)
29 changes: 29 additions & 0 deletions drivers/virtual/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package virtual

import (
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/operations"
)

type Addition struct {
driver.RootFolderPath
NumFile int `json:"num_file" type:"number" default:"30" required:"true"`
NumFolder int `json:"num_folder" type:"number" default:"30" required:"true"`
MaxFileSize int64 `json:"max_file_size" type:"number" default:"1073741824" required:"true"`
MinFileSize int64 `json:"min_file_size" type:"number" default:"1048576" required:"true"`
}

var config = driver.Config{
Name: "Virtual",
OnlyLocal: true,
LocalSort: true,
//NoCache: true,
}

func New() driver.Driver {
return &Virtual{}
}

func init() {
operations.RegisterDriver(config, New)
}
4 changes: 4 additions & 0 deletions pkg/utils/random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func Token() string {
return uuid.NewString() + String(64)
}

func RangeInt64(left, right int64) int64 {
return rand.Int63n(left+right) - left
}

func init() {
s := rand.NewSource(time.Now().UnixNano())
Rand = rand.New(s)
Expand Down

0 comments on commit 90a5c17

Please sign in to comment.