mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2026-10-10 04:53:09 +08:00
fix(teldrive): use a valid root path for listings (#3108)
This commit is contained in:
@@ -55,10 +55,18 @@ func (d *Teldrive) Drop(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Teldrive) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
func (d *Teldrive) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
||||||
|
dirPath := dir.GetPath()
|
||||||
|
if dirPath == "" {
|
||||||
|
dirPath = d.GetRootPath()
|
||||||
|
}
|
||||||
|
if dirPath == "" {
|
||||||
|
dirPath = "/"
|
||||||
|
}
|
||||||
|
|
||||||
var firstResp ListResp
|
var firstResp ListResp
|
||||||
err := d.request(http.MethodGet, "/api/files", func(req *resty.Request) {
|
err := d.request(http.MethodGet, "/api/files", func(req *resty.Request) {
|
||||||
req.SetQueryParams(map[string]string{
|
req.SetQueryParams(map[string]string{
|
||||||
"path": dir.GetPath(),
|
"path": dirPath,
|
||||||
"limit": "500",
|
"limit": "500",
|
||||||
"page": "1",
|
"page": "1",
|
||||||
})
|
})
|
||||||
@@ -87,7 +95,7 @@ func (d *Teldrive) List(ctx context.Context, dir model.Obj, args model.ListArgs)
|
|||||||
var resp ListResp
|
var resp ListResp
|
||||||
err := d.request(http.MethodGet, "/api/files", func(req *resty.Request) {
|
err := d.request(http.MethodGet, "/api/files", func(req *resty.Request) {
|
||||||
req.SetQueryParams(map[string]string{
|
req.SetQueryParams(map[string]string{
|
||||||
"path": dir.GetPath(),
|
"path": dirPath,
|
||||||
"limit": "500",
|
"limit": "500",
|
||||||
"page": strconv.Itoa(page),
|
"page": strconv.Itoa(page),
|
||||||
})
|
})
|
||||||
@@ -114,7 +122,7 @@ func (d *Teldrive) List(ctx context.Context, dir model.Obj, args model.ListArgs)
|
|||||||
|
|
||||||
return utils.SliceConvert(allItems, func(src Object) (model.Obj, error) {
|
return utils.SliceConvert(allItems, func(src Object) (model.Obj, error) {
|
||||||
return &model.Object{
|
return &model.Object{
|
||||||
Path: path.Join(dir.GetPath(), src.Name),
|
Path: path.Join(dirPath, src.Name),
|
||||||
ID: src.ID,
|
ID: src.ID,
|
||||||
Name: src.Name,
|
Name: src.Name,
|
||||||
Size: func() int64 {
|
Size: func() int64 {
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/OpenListTeam/OpenList/v4/drivers/base"
|
"github.com/OpenListTeam/OpenList/v4/drivers/base"
|
||||||
|
"github.com/OpenListTeam/OpenList/v4/internal/driver"
|
||||||
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
)
|
)
|
||||||
@@ -36,3 +38,44 @@ func TestListEmptyDir(t *testing.T) {
|
|||||||
t.Fatalf("expected no entries for an empty dir, got %d", len(objs))
|
t.Fatalf("expected no entries for an empty dir, got %d", len(objs))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestListRootUsesConfiguredRootPath(t *testing.T) {
|
||||||
|
var (
|
||||||
|
mu sync.Mutex
|
||||||
|
paths []string
|
||||||
|
)
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
mu.Lock()
|
||||||
|
paths = append(paths, r.URL.Query().Get("path"))
|
||||||
|
mu.Unlock()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`{"items":[{"id":"child","name":"child","type":"folder"}],"meta":{"count":1,"totalPages":1,"currentPage":1}}`))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
oldClient := base.RestyClient
|
||||||
|
base.RestyClient = resty.New()
|
||||||
|
defer func() { base.RestyClient = oldClient }()
|
||||||
|
|
||||||
|
d := &Teldrive{
|
||||||
|
Addition: Addition{
|
||||||
|
RootPath: driver.RootPath{RootFolderPath: "/configured-root"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
d.Address = srv.URL
|
||||||
|
|
||||||
|
objs, err := d.List(context.Background(), &model.Object{}, model.ListArgs{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("List returned error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
if len(paths) != 1 || paths[0] != "/configured-root" {
|
||||||
|
t.Fatalf("expected request path %q, got %q", "/configured-root", paths)
|
||||||
|
}
|
||||||
|
if len(objs) != 1 || objs[0].GetPath() != "/configured-root/child" {
|
||||||
|
t.Fatalf("expected child path %q, got %#v", "/configured-root/child", objs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user