Skip to content

Commit

Permalink
fix: local relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 27, 2022
1 parent 7c0b86a commit 74973bc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
6 changes: 6 additions & 0 deletions drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions internal/bootstrap/data/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
}
1 change: 0 additions & 1 deletion internal/fs/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/operations/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/utils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package utils

import (
"path/filepath"
"runtime"
"strings"
)

// StandardizePath convert path like '/' '/root' '/a/b'
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, "/") {
Expand Down
2 changes: 2 additions & 0 deletions server/controllers/fslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 74973bc

Please sign in to comment.