Modify: add recursive options for list method

Test: local.List
pull/348/head
HFO4 5 years ago
parent 26d4d34837
commit 36e5b31f73

@ -46,7 +46,7 @@ type Driver struct {
HTTPClient request.Client HTTPClient request.Client
} }
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) {
panic("implement me") panic("implement me")
} }

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

@ -2,7 +2,6 @@ package local
import ( import (
"context" "context"
"fmt"
model "github.com/HFO4/cloudreve/models" model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/auth" "github.com/HFO4/cloudreve/pkg/auth"
"github.com/HFO4/cloudreve/pkg/conf" "github.com/HFO4/cloudreve/pkg/conf"
@ -233,10 +232,32 @@ func TestHandler_Token(t *testing.T) {
} }
func TestDriver_List(t *testing.T) { func TestDriver_List(t *testing.T) {
//asserts := assert.New(t) asserts := assert.New(t)
handler := Driver{} handler := Driver{}
ctx := context.Background() 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)
}
} }

@ -22,7 +22,7 @@ type Driver struct {
HTTPClient request.Client HTTPClient request.Client
} }
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) {
panic("implement me") panic("implement me")
} }

@ -42,7 +42,7 @@ type Driver struct {
HTTPClient request.Client HTTPClient request.Client
} }
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) {
panic("implement me") panic("implement me")
} }

@ -22,7 +22,7 @@ type Driver struct {
Policy *model.Policy Policy *model.Policy
} }
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) {
panic("implement me") panic("implement me")
} }

@ -27,7 +27,7 @@ type Driver struct {
AuthInstance auth.Auth AuthInstance auth.Auth
} }
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) {
panic("implement me") panic("implement me")
} }

@ -40,7 +40,7 @@ type Driver struct {
Policy *model.Policy Policy *model.Policy
} }
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) {
panic("implement me") panic("implement me")
} }

@ -65,8 +65,10 @@ type Handler interface {
// Token 获取有效期为ttl的上传凭证和签名同时回调会话有效期为sessionTTL // Token 获取有效期为ttl的上传凭证和签名同时回调会话有效期为sessionTTL
Token(ctx context.Context, ttl int64, callbackKey string) (serializer.UploadCredential, error) Token(ctx context.Context, ttl int64, callbackKey string) (serializer.UploadCredential, error)
// List 递归列取远程端path路径下文件、目录不包含path本身返回的对象路径以path作为起始根目录 // List 递归列取远程端path路径下文件、目录不包含path本身
List(ctx context.Context, path string) ([]response.Object, error) // 返回的对象路径以path作为起始根目录.
// recursive - 是否递归列出
List(ctx context.Context, path string, recursive bool) ([]response.Object, error)
} }
// FileSystem 管理文件的文件系统 // FileSystem 管理文件的文件系统

@ -26,7 +26,7 @@ type FileHeaderMock struct {
testMock.Mock testMock.Mock
} }
func (m FileHeaderMock) List(ctx context.Context, path string) ([]response.Object, error) { func (m FileHeaderMock) List(ctx context.Context, path string, recursive bool) ([]response.Object, error) {
panic("implement me") panic("implement me")
} }

Loading…
Cancel
Save