1Panel/backend/app/api/v1/file.go

157 lines
4.5 KiB
Go
Raw Normal View History

2022-08-24 11:10:50 +08:00
package v1
import (
2022-09-03 18:41:52 +08:00
"fmt"
2022-08-24 11:10:50 +08:00
"github.com/1Panel-dev/1Panel/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/app/dto"
"github.com/1Panel-dev/1Panel/constant"
2022-09-03 18:41:52 +08:00
"github.com/1Panel-dev/1Panel/global"
2022-08-24 11:10:50 +08:00
"github.com/gin-gonic/gin"
2022-09-03 18:41:52 +08:00
"path"
2022-08-24 11:10:50 +08:00
)
func (b *BaseApi) ListFiles(c *gin.Context) {
var req dto.FileOption
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
files, err := fileService.GetFileList(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, files)
}
2022-08-24 17:34:21 +08:00
func (b *BaseApi) GetFileTree(c *gin.Context) {
var req dto.FileOption
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
tree, err := fileService.GetFileTree(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, tree)
}
2022-08-25 17:54:52 +08:00
func (b *BaseApi) CreateFile(c *gin.Context) {
var req dto.FileCreate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
err := fileService.Create(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
2022-08-25 18:48:03 +08:00
func (b *BaseApi) DeleteFile(c *gin.Context) {
var req dto.FileDelete
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
err := fileService.Delete(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
func (b *BaseApi) ChangeFileMode(c *gin.Context) {
var req dto.FileCreate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
err := fileService.ChangeMode(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
2022-08-30 17:59:59 +08:00
func (b *BaseApi) CompressFile(c *gin.Context) {
var req dto.FileCompress
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
err := fileService.Compress(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
2022-08-31 13:59:02 +08:00
func (b *BaseApi) DeCompressFile(c *gin.Context) {
var req dto.FileDeCompress
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
err := fileService.DeCompress(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
2022-09-01 19:02:33 +08:00
func (b *BaseApi) GetContent(c *gin.Context) {
var req dto.FileOption
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
info, err := fileService.GetContent(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, info)
}
func (b *BaseApi) SaveContent(c *gin.Context) {
var req dto.FileEdit
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := fileService.SaveContent(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
2022-09-03 18:41:52 +08:00
func (b *BaseApi) UploadFiles(c *gin.Context) {
form, err := c.MultipartForm()
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
files := form.File["file"]
paths := form.Value["path"]
success := 0
for _, file := range files {
err := c.SaveUploadedFile(file, path.Join(paths[0], file.Filename))
if err != nil {
global.LOG.Errorf("upload [%s] file failed, err: %v", file.Filename, err)
continue
}
success++
}
helper.SuccessWithMsg(c, fmt.Sprintf("%d files upload success", success))
}