Cloudreve/service/explorer/directory.go

63 lines
1.5 KiB
Go
Raw Normal View History

package explorer
import (
"context"
"github.com/HFO4/cloudreve/pkg/filesystem"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
)
2019-11-24 13:06:15 +08:00
// DirectoryService 创建新目录服务
type DirectoryService struct {
2019-11-30 15:09:56 +08:00
Path string `uri:"path" json:"path" binding:"required,min=1,max=65535"`
}
2019-11-30 15:09:56 +08:00
// ListDirectory 列出目录内容
2019-11-24 13:06:15 +08:00
func (service *DirectoryService) ListDirectory(c *gin.Context) serializer.Response {
// 创建文件系统
2019-11-24 13:06:15 +08:00
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
}
defer fs.Recycle()
2019-11-24 13:06:15 +08:00
// 上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// 获取子项目
objects, err := fs.List(ctx, service.Path, nil)
if err != nil {
return serializer.Err(serializer.CodeCreateFolderFailed, err.Error(), err)
}
return serializer.Response{
Code: 0,
Data: objects,
}
}
2019-11-27 19:16:13 +08:00
// CreateDirectory 创建目录
2019-11-24 13:06:15 +08:00
func (service *DirectoryService) CreateDirectory(c *gin.Context) serializer.Response {
// 创建文件系统
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
}
defer fs.Recycle()
// 上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// 创建目录
err = fs.CreateDirectory(ctx, service.Path)
if err != nil {
return serializer.Err(serializer.CodeCreateFolderFailed, err.Error(), err)
}
return serializer.Response{
Code: 0,
}
}