fix: local relative path

refactor/fs
Noah Hsu 2022-06-27 20:37:05 +08:00
parent 7c0b86a9cd
commit 74973bc5b5
6 changed files with 25 additions and 5 deletions

View File

@ -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) err = errors.Errorf("root folder %s not exists", d.RootFolder)
d.SetStatus(err.Error()) d.SetStatus(err.Error())
} else { } 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") d.SetStatus("OK")
} }
operations.MustSaveDriverAccount(d) operations.MustSaveDriverAccount(d)

View File

@ -2,6 +2,7 @@ package data
import ( import (
"context" "context"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations" "github.com/alist-org/alist/v3/internal/operations"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -18,4 +19,17 @@ func initDevData() {
if err != nil { if err != nil {
log.Fatalf("failed to create account: %+v", err) 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)
}
} }

View File

@ -10,7 +10,6 @@ import (
) )
// List files // List files
// TODO: sort
func list(ctx context.Context, path string) ([]model.Obj, error) { func list(ctx context.Context, path string) ([]model.Obj, error) {
meta := ctx.Value("meta").(*model.Meta) meta := ctx.Value("meta").(*model.Meta)
user := ctx.Value("user").(*model.User) user := ctx.Value("user").(*model.User)

View File

@ -87,7 +87,7 @@ func Get(ctx context.Context, account driver.Driver, path string) (model.Obj, er
} }
// not root folder // not root folder
dir, name := stdpath.Split(path) dir, name := stdpath.Split(path)
files, err := List(ctx, account, dir) files, err := List(ctx, account, utils.StandardizePath(dir))
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "failed get parent list") return nil, errors.WithMessage(err, "failed get parent list")
} }

View File

@ -2,7 +2,6 @@ package utils
import ( import (
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
) )
@ -10,11 +9,11 @@ import (
func StandardizePath(path string) string { func StandardizePath(path string) string {
path = strings.TrimSuffix(path, "/") path = strings.TrimSuffix(path, "/")
// windows abs path // windows abs path
if filepath.IsAbs(path) && runtime.GOOS == "windows" { if filepath.IsAbs(path) {
return path return path
} }
// relative path with prefix '..' // relative path with prefix '..'
if strings.HasPrefix(path, "..") { if strings.HasPrefix(path, ".") {
return path return path
} }
if !strings.HasPrefix(path, "/") { if !strings.HasPrefix(path, "/") {

View File

@ -7,6 +7,7 @@ import (
"github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
stdpath "path"
"time" "time"
) )
@ -36,6 +37,7 @@ func List(c *gin.Context) {
} }
req.Validate() req.Validate()
user := c.MustGet("user").(*model.User) user := c.MustGet("user").(*model.User)
req.Path = stdpath.Join(user.BasePath, req.Path)
meta, _ := db.GetNearestMeta(req.Path) meta, _ := db.GetNearestMeta(req.Path)
c.Set("meta", meta) c.Set("meta", meta)
if !canAccess(user, meta, req.Path, req.Password) { if !canAccess(user, meta, req.Path, req.Password) {