From 505b126888cf3b312db70c3cdd841ba51cdc0702 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Tue, 28 Jun 2022 22:13:47 +0800 Subject: [PATCH] chore: optional get func for driver --- drivers/local/driver.go | 15 +++++++++++++++ internal/driver/driver.go | 5 ++++- internal/operations/fs.go | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/drivers/local/driver.go b/drivers/local/driver.go index 4a053bc1..64099b2c 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -76,6 +76,21 @@ func (d *Driver) List(ctx context.Context, dir model.Obj) ([]model.Obj, error) { return files, nil } +func (d *Driver) Get(ctx context.Context, path string) (model.Obj, error) { + f, err := os.Stat(path) + if err != nil { + return nil, errors.Wrapf(err, "error while stat %s", path) + } + file := model.Object{ + ID: path, + Name: f.Name(), + Modified: f.ModTime(), + Size: f.Size(), + IsFolder: f.IsDir(), + } + return &file, nil +} + func (d *Driver) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { fullPath := file.GetID() link := model.Link{ diff --git a/internal/driver/driver.go b/internal/driver/driver.go index c3707d29..629d67f9 100644 --- a/internal/driver/driver.go +++ b/internal/driver/driver.go @@ -35,7 +35,10 @@ type Reader interface { List(ctx context.Context, dir model.Obj) ([]model.Obj, error) // Link get url/filepath/reader of file Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) - //Get(ctx context.Context, path string) (FileInfo, error) // maybe not need +} + +type Getter interface { + Get(ctx context.Context, path string) (model.Obj, error) } type Writer interface { diff --git a/internal/operations/fs.go b/internal/operations/fs.go index 161d5c49..60779238 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -71,6 +71,9 @@ func isRoot(path, rootFolderPath string) bool { func Get(ctx context.Context, account driver.Driver, path string) (model.Obj, error) { path = utils.StandardizePath(path) log.Debugf("operations.Get %s", path) + if g, ok := account.(driver.Getter); ok { + return g.Get(ctx, path) + } // is root folder if r, ok := account.GetAddition().(driver.IRootFolderId); ok && utils.PathEqual(path, "/") { return model.Object{