From 5d579cdadcbff49d043dc43cfd398d9354dbcbc8 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Tue, 28 Apr 2020 10:02:53 +0800 Subject: [PATCH] Feat: slave side list file --- pkg/filesystem/driver/remote/handler.go | 41 ++++++++++++++++++++++++- pkg/filesystem/response/common.go | 12 ++++---- pkg/serializer/slave.go | 6 ++++ routers/controllers/slave.go | 11 +++++++ routers/router.go | 2 ++ service/explorer/file.go | 24 +++++++++++++++ 6 files changed, 89 insertions(+), 7 deletions(-) diff --git a/pkg/filesystem/driver/remote/handler.go b/pkg/filesystem/driver/remote/handler.go index 664855e..4b584ec 100644 --- a/pkg/filesystem/driver/remote/handler.go +++ b/pkg/filesystem/driver/remote/handler.go @@ -27,8 +27,45 @@ type Driver struct { AuthInstance auth.Auth } +// List 列取文件 func (handler Driver) List(ctx context.Context, path string, recursive bool) ([]response.Object, error) { - panic("implement me") + var res []response.Object + + reqBody := serializer.ListRequest{ + Path: path, + Recursive: recursive, + } + reqBodyEncoded, err := json.Marshal(reqBody) + if err != nil { + return res, err + } + + // 发送列表请求 + bodyReader := strings.NewReader(string(reqBodyEncoded)) + signTTL := model.GetIntSetting("slave_api_timeout", 60) + resp, err := handler.Client.Request( + "POST", + handler.getAPIUrl("list"), + bodyReader, + request.WithCredential(handler.AuthInstance, int64(signTTL)), + ).CheckHTTPResponse(200).DecodeResponse() + if err != nil { + return res, err + } + + // 处理列取结果 + if resp.Code != 0 { + return res, errors.New(resp.Error) + } + + if resStr, ok := resp.Data.(string); ok { + err = json.Unmarshal([]byte(resStr), &res) + if err != nil { + return res, err + } + } + + return res, nil } // getAPIUrl 获取接口请求地址 @@ -44,6 +81,8 @@ func (handler Driver) getAPIUrl(scope string, routes ...string) string { controller, _ = url.Parse("/api/v3/slave/delete") case "thumb": controller, _ = url.Parse("/api/v3/slave/thumb") + case "list": + controller, _ = url.Parse("/api/v3/slave/list") default: controller = serverURL } diff --git a/pkg/filesystem/response/common.go b/pkg/filesystem/response/common.go index da060e0..a6c9a1d 100644 --- a/pkg/filesystem/response/common.go +++ b/pkg/filesystem/response/common.go @@ -23,10 +23,10 @@ type RSCloser interface { // Object 列出文件、目录时返回的对象 type Object struct { - Name string - RelativePath string - Source string - Size uint64 - IsDir bool - LastModify time.Time + Name string `json:"name"` + RelativePath string `json:"relative_path"` + Source string `json:"source"` + Size uint64 `json:"size"` + IsDir bool `json:"is_dir"` + LastModify time.Time `json:"last_modify"` } diff --git a/pkg/serializer/slave.go b/pkg/serializer/slave.go index 8a90d6b..e23e809 100644 --- a/pkg/serializer/slave.go +++ b/pkg/serializer/slave.go @@ -4,3 +4,9 @@ package serializer type RemoteDeleteRequest struct { Files []string `json:"files"` } + +// ListRequest 远程策略列文件请求正文 +type ListRequest struct { + Path string `json:"path"` + Recursive bool `json:"recursive"` +} diff --git a/routers/controllers/slave.go b/routers/controllers/slave.go index c5535ec..29c115a 100644 --- a/routers/controllers/slave.go +++ b/routers/controllers/slave.go @@ -158,3 +158,14 @@ func SlavePing(c *gin.Context) { c.JSON(200, ErrorResponse(err)) } } + +// SlaveList 从机列出文件 +func SlaveList(c *gin.Context) { + var service explorer.SlaveListService + if err := c.ShouldBindJSON(&service); err == nil { + res := service.List(c) + c.JSON(200, res) + } else { + c.JSON(200, ErrorResponse(err)) + } +} diff --git a/routers/router.go b/routers/router.go index 53232b8..195fb92 100644 --- a/routers/router.go +++ b/routers/router.go @@ -49,6 +49,8 @@ func InitSlaveRouter() *gin.Engine { v3.GET("thumb/:path", controllers.SlaveThumb) // 删除文件 v3.POST("delete", controllers.SlaveDelete) + // 列出文件 + v3.POST("list", controllers.SlaveList) } return r } diff --git a/service/explorer/file.go b/service/explorer/file.go index 4bd9e34..507b49c 100644 --- a/service/explorer/file.go +++ b/service/explorer/file.go @@ -56,6 +56,30 @@ type SlaveFilesService struct { Files []string `json:"files" binding:"required,gt=0"` } +// SlaveListService 从机列表服务 +type SlaveListService struct { + Path string `json:"path" binding:"required,min=1,max=65535"` + Recursive bool `json:"recursive"` +} + +// List 列出从机上的文件 +func (service *SlaveListService) List(c *gin.Context) serializer.Response { + // 创建文件系统 + fs, err := filesystem.NewAnonymousFileSystem() + if err != nil { + return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + } + defer fs.Recycle() + + objects, err := fs.Handler.List(context.Background(), service.Path, service.Recursive) + if err != nil { + return serializer.Err(serializer.CodeIOFailed, "无法列取文件", err) + } + + res, _ := json.Marshal(objects) + return serializer.Response{Data: string(res)} +} + // DownloadArchived 下載已打包的多文件 func (service *DownloadService) DownloadArchived(ctx context.Context, c *gin.Context) serializer.Response { // 创建文件系统