diff --git a/assets b/assets index 6d23b07..538bd95 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 6d23b07b7ae655d1ecba23c21a0482950587b4f0 +Subproject commit 538bd95a7ba1712bf8b079375d1aa419d803273a diff --git a/routers/controllers/file.go b/routers/controllers/file.go index c7bb701..92fdf4b 100644 --- a/routers/controllers/file.go +++ b/routers/controllers/file.go @@ -353,3 +353,14 @@ func SearchFile(c *gin.Context) { c.JSON(200, ErrorResponse(err)) } } + +// CreateFile 创建空白文件 +func CreateFile(c *gin.Context) { + var service explorer.SingleFileService + if err := c.ShouldBindJSON(&service); err == nil { + res := service.Create(c) + c.JSON(200, res) + } else { + c.JSON(200, ErrorResponse(err)) + } +} diff --git a/routers/router.go b/routers/router.go index 9e824cf..ad0da4b 100644 --- a/routers/router.go +++ b/routers/router.go @@ -445,6 +445,8 @@ func InitMasterRouter() *gin.Engine { file.GET("upload/credential", controllers.GetUploadCredential) // 更新文件 file.PUT("update/:id", controllers.PutContent) + // 创建空白文件 + file.POST("create", controllers.CreateFile) // 创建文件下载会话 file.PUT("download/:id", controllers.CreateDownloadSession) // 预览文件 diff --git a/service/explorer/file.go b/service/explorer/file.go index 507b49c..164fbfe 100644 --- a/service/explorer/file.go +++ b/service/explorer/file.go @@ -13,15 +13,18 @@ import ( "github.com/HFO4/cloudreve/pkg/serializer" "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "io/ioutil" "net/http" "net/url" + "path" "strconv" + "strings" "time" ) // SingleFileService 对单文件进行操作的五福,path为文件完整路径 type SingleFileService struct { - Path string `uri:"path" binding:"required,min=1,max=65535"` + Path string `uri:"path" json:"path" binding:"required,min=1,max=65535"` } // FileIDService 通过文件ID对文件进行操作的服务 @@ -62,6 +65,39 @@ type SlaveListService struct { Recursive bool `json:"recursive"` } +// New 创建新文件 +func (service *SingleFileService) Create(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() + + // 给文件系统分配钩子 + fs.Use("BeforeUpload", filesystem.HookValidateFile) + fs.Use("AfterUpload", filesystem.GenericAfterUpload) + + // 上传空文件 + err = fs.Upload(ctx, local.FileStream{ + File: ioutil.NopCloser(strings.NewReader("")), + Size: 0, + VirtualPath: path.Dir(service.Path), + Name: path.Base(service.Path), + }) + if err != nil { + return serializer.Err(serializer.CodeUploadFailed, err.Error(), err) + } + + return serializer.Response{ + Code: 0, + } +} + // List 列出从机上的文件 func (service *SlaveListService) List(c *gin.Context) serializer.Response { // 创建文件系统