Feat: slave side list file
This commit is contained in:
parent
d5fc5745b4
commit
5d579cdadc
6 changed files with 89 additions and 7 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
// 创建文件系统
|
||||
|
|
Loading…
Add table
Reference in a new issue