Skip to content

Commit

Permalink
chore(virtual): implement the driver interface with result
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 20, 2023
1 parent 57eea4d commit f0981a0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
51 changes: 29 additions & 22 deletions drivers/virtual/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,10 @@ func (d *Virtual) GetAddition() driver.Additional {
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, &model.Object{
Name: random.String(10),
Size: random.RangeInt64(d.MinFileSize, d.MaxFileSize),
IsFolder: false,
Modified: time.Now(),
})
res = append(res, d.genObj(false))
}
for i := 0; i < d.NumFolder; i++ {
res = append(res, &model.Object{
Name: random.String(10),
Size: 0,
IsFolder: true,
Modified: time.Now(),
})
res = append(res, d.genObj(true))
}
return res, nil
}
Expand Down Expand Up @@ -78,28 +68,45 @@ func (d *Virtual) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
}, nil
}

func (d *Virtual) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
return 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) error {
return 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) error {
return 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) error {
return 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) 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)
22 changes: 22 additions & 0 deletions drivers/virtual/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package virtual

import (
"time"

"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils/random"
)

func (d *Virtual) genObj(dir bool) model.Obj {
obj := &model.Object{
Name: random.String(10),
Size: 0,
IsFolder: true,
Modified: time.Now(),
}
if !dir {
obj.Size = random.RangeInt64(d.MinFileSize, d.MaxFileSize)
obj.IsFolder = false
}
return obj
}

0 comments on commit f0981a0

Please sign in to comment.