From 74973bc5b5e0b10cdada8d8f89652e7a33fd8236 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Mon, 27 Jun 2022 20:37:05 +0800 Subject: [PATCH] fix: local relative path --- drivers/local/driver.go | 6 ++++++ internal/bootstrap/data/dev.go | 14 ++++++++++++++ internal/fs/list.go | 1 - internal/operations/fs.go | 2 +- pkg/utils/path.go | 5 ++--- server/controllers/fslist.go | 2 ++ 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/drivers/local/driver.go b/drivers/local/driver.go index 016a529b1ac..4a053bc1635 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -34,6 +34,12 @@ func (d *Driver) Init(ctx context.Context, account model.Account) error { err = errors.Errorf("root folder %s not exists", d.RootFolder) d.SetStatus(err.Error()) } else { + if !filepath.IsAbs(d.RootFolder) { + d.RootFolder, err = filepath.Abs(d.RootFolder) + if err != nil { + return errors.Wrap(err, "error while get abs path") + } + } d.SetStatus("OK") } operations.MustSaveDriverAccount(d) diff --git a/internal/bootstrap/data/dev.go b/internal/bootstrap/data/dev.go index dfc9b969d82..cb73a11b44f 100644 --- a/internal/bootstrap/data/dev.go +++ b/internal/bootstrap/data/dev.go @@ -2,6 +2,7 @@ package data import ( "context" + "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/operations" log "github.com/sirupsen/logrus" @@ -18,4 +19,17 @@ func initDevData() { if err != nil { log.Fatalf("failed to create account: %+v", err) } + err = db.CreateUser(&model.User{ + Username: "Noah", + Password: "hsu", + BasePath: "/data", + ReadOnly: false, + Webdav: false, + Role: 0, + IgnoreHide: false, + IgnorePassword: false, + }) + if err != nil { + log.Fatalf("failed to create user: %+v", err) + } } diff --git a/internal/fs/list.go b/internal/fs/list.go index 58ffe51a071..e60f307f76a 100644 --- a/internal/fs/list.go +++ b/internal/fs/list.go @@ -10,7 +10,6 @@ import ( ) // List files -// TODO: sort func list(ctx context.Context, path string) ([]model.Obj, error) { meta := ctx.Value("meta").(*model.Meta) user := ctx.Value("user").(*model.User) diff --git a/internal/operations/fs.go b/internal/operations/fs.go index 8273363c2e8..8869184badc 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -87,7 +87,7 @@ func Get(ctx context.Context, account driver.Driver, path string) (model.Obj, er } // not root folder dir, name := stdpath.Split(path) - files, err := List(ctx, account, dir) + files, err := List(ctx, account, utils.StandardizePath(dir)) if err != nil { return nil, errors.WithMessage(err, "failed get parent list") } diff --git a/pkg/utils/path.go b/pkg/utils/path.go index 2133d38171e..d74c90394ed 100644 --- a/pkg/utils/path.go +++ b/pkg/utils/path.go @@ -2,7 +2,6 @@ package utils import ( "path/filepath" - "runtime" "strings" ) @@ -10,11 +9,11 @@ import ( func StandardizePath(path string) string { path = strings.TrimSuffix(path, "/") // windows abs path - if filepath.IsAbs(path) && runtime.GOOS == "windows" { + if filepath.IsAbs(path) { return path } // relative path with prefix '..' - if strings.HasPrefix(path, "..") { + if strings.HasPrefix(path, ".") { return path } if !strings.HasPrefix(path, "/") { diff --git a/server/controllers/fslist.go b/server/controllers/fslist.go index 8dbe480968e..0d0c8f6cc53 100644 --- a/server/controllers/fslist.go +++ b/server/controllers/fslist.go @@ -7,6 +7,7 @@ import ( "github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/server/common" "github.com/gin-gonic/gin" + stdpath "path" "time" ) @@ -36,6 +37,7 @@ func List(c *gin.Context) { } req.Validate() user := c.MustGet("user").(*model.User) + req.Path = stdpath.Join(user.BasePath, req.Path) meta, _ := db.GetNearestMeta(req.Path) c.Set("meta", meta) if !canAccess(user, meta, req.Path, req.Password) {