Modify: add recursive options for list method

Test: local.List
This commit is contained in:
HFO4
2020-04-27 10:31:34 +08:00
parent 26d4d34837
commit 36e5b31f73
10 changed files with 42 additions and 14 deletions

View File

@@ -24,7 +24,7 @@ type Driver struct {
}
// List 递归列取给定物理路径下所有文件
func (handler Driver) List(ctx context.Context, path string) ([]response.Object, error) {
func (handler Driver) List(ctx context.Context, path string, recursive bool) ([]response.Object, error) {
var res []response.Object
// 取得起始路径
@@ -58,6 +58,11 @@ func (handler Driver) List(ctx context.Context, path string) ([]response.Object,
LastModify: info.ModTime(),
})
// 如果非递归,则不步入目录
if !recursive && info.IsDir() {
return filepath.SkipDir
}
return nil
})

View File

@@ -2,7 +2,6 @@ package local
import (
"context"
"fmt"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/auth"
"github.com/HFO4/cloudreve/pkg/conf"
@@ -233,10 +232,32 @@ func TestHandler_Token(t *testing.T) {
}
func TestDriver_List(t *testing.T) {
//asserts := assert.New(t)
asserts := assert.New(t)
handler := Driver{}
ctx := context.Background()
res, err := handler.List(ctx, "KKV")
fmt.Println(res, err)
// 创建测试目录结构
for _, path := range []string{
"test/TestDriver_List/parent.txt",
"test/TestDriver_List/parent_folder2/sub2.txt",
"test/TestDriver_List/parent_folder1/sub_folder/sub1.txt",
"test/TestDriver_List/parent_folder1/sub_folder/sub2.txt",
} {
f, _ := util.CreatNestedFile(util.RelativePath(path))
f.Close()
}
// 非递归列出
{
res, err := handler.List(ctx, "test/TestDriver_List", false)
asserts.NoError(err)
asserts.Len(res, 3)
}
// 递归列出
{
res, err := handler.List(ctx, "test/TestDriver_List", true)
asserts.NoError(err)
asserts.Len(res, 7)
}
}