2022-08-24 11:10:50 +08:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-12-01 10:36:49 +08:00
|
|
|
"errors"
|
2022-09-03 18:41:52 +08:00
|
|
|
"fmt"
|
2022-12-14 15:39:13 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
2022-11-02 16:28:54 +08:00
|
|
|
"io/ioutil"
|
2022-10-12 13:42:58 +08:00
|
|
|
"net/http"
|
2022-12-01 10:36:49 +08:00
|
|
|
"os"
|
2022-10-12 13:42:58 +08:00
|
|
|
"path"
|
2022-12-01 10:36:49 +08:00
|
|
|
"strings"
|
2022-10-12 13:42:58 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
websocket2 "github.com/1Panel-dev/1Panel/backend/utils/websocket"
|
2022-08-24 11:10:50 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-14 19:09:39 +08:00
|
|
|
"github.com/gorilla/websocket"
|
2022-08-24 11:10:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *BaseApi) ListFiles(c *gin.Context) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileOption
|
2022-08-24 11:10:50 +08:00
|
|
|
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) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileOption
|
2022-08-24 17:34:21 +08:00
|
|
|
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) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileCreate
|
2022-08-25 17:54:52 +08:00
|
|
|
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) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileDelete
|
2022-08-25 18:48:03 +08:00
|
|
|
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)
|
|
|
|
}
|
2022-08-29 15:26:36 +08:00
|
|
|
|
2022-12-01 10:36:49 +08:00
|
|
|
func (b *BaseApi) BatchDeleteFile(c *gin.Context) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileBatchDelete
|
2022-12-01 10:36:49 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := fileService.BatchDelete(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:26:36 +08:00
|
|
|
func (b *BaseApi) ChangeFileMode(c *gin.Context) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileCreate
|
2022-08-29 15:26:36 +08:00
|
|
|
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) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileCompress
|
2022-08-30 17:59:59 +08:00
|
|
|
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) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileDeCompress
|
2022-08-31 13:59:02 +08:00
|
|
|
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) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileOption
|
2022-09-01 19:02:33 +08:00
|
|
|
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) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileEdit
|
2022-09-01 19:02:33 +08:00
|
|
|
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"]
|
2022-12-01 10:36:49 +08:00
|
|
|
if len(paths) == 0 || !strings.Contains(paths[0], "/") {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error paths in request"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dir := paths[0][:strings.LastIndex(paths[0], "/")]
|
|
|
|
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, fmt.Errorf("mkdir %s failed, err: %v", dir, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-03 18:41:52 +08:00
|
|
|
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))
|
|
|
|
}
|
2022-09-03 22:22:40 +08:00
|
|
|
|
2022-09-06 17:48:49 +08:00
|
|
|
func (b *BaseApi) ChangeFileName(c *gin.Context) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileRename
|
2022-09-03 22:22:40 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := fileService.ChangeName(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
2022-09-05 16:25:26 +08:00
|
|
|
|
2022-09-06 17:48:49 +08:00
|
|
|
func (b *BaseApi) WgetFile(c *gin.Context) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileWget
|
2022-09-05 16:25:26 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-09-14 19:09:39 +08:00
|
|
|
key, err := fileService.Wget(req)
|
|
|
|
if err != nil {
|
2022-09-05 16:25:26 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
2022-12-14 15:39:13 +08:00
|
|
|
helper.SuccessWithData(c, response.FileWgetRes{
|
2022-09-14 19:09:39 +08:00
|
|
|
Key: key,
|
|
|
|
})
|
2022-09-05 16:25:26 +08:00
|
|
|
}
|
2022-09-06 10:35:35 +08:00
|
|
|
|
2022-09-06 17:48:49 +08:00
|
|
|
func (b *BaseApi) MoveFile(c *gin.Context) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileMove
|
2022-09-06 10:35:35 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := fileService.MvFile(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
2022-09-06 17:48:49 +08:00
|
|
|
|
|
|
|
func (b *BaseApi) Download(c *gin.Context) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.FileDownload
|
2022-09-06 17:48:49 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
filePath, err := fileService.FileDownload(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.File(filePath)
|
|
|
|
}
|
2022-09-09 18:10:41 +08:00
|
|
|
|
|
|
|
func (b *BaseApi) Size(c *gin.Context) {
|
2022-12-14 15:39:13 +08:00
|
|
|
var req request.DirSizeReq
|
2022-09-09 18:10:41 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res, err := fileService.DirSize(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, res)
|
|
|
|
}
|
2022-09-14 19:09:39 +08:00
|
|
|
|
2022-10-12 13:42:58 +08:00
|
|
|
func (b *BaseApi) LoadFromFile(c *gin.Context) {
|
|
|
|
var req dto.FilePath
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(req.Path)
|
2022-10-12 13:42:58 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
helper.SuccessWithData(c, string(content))
|
2022-10-12 13:42:58 +08:00
|
|
|
}
|
|
|
|
|
2022-09-14 19:09:39 +08:00
|
|
|
var wsUpgrade = websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var WsManager = websocket2.Manager{
|
|
|
|
Group: make(map[string]*websocket2.Client),
|
|
|
|
Register: make(chan *websocket2.Client, 128),
|
|
|
|
UnRegister: make(chan *websocket2.Client, 128),
|
|
|
|
ClientCount: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) Ws(c *gin.Context) {
|
|
|
|
ws, err := wsUpgrade.Upgrade(c.Writer, c.Request, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wsClient := websocket2.NewWsClient("13232", ws)
|
|
|
|
go wsClient.Read()
|
|
|
|
go wsClient.Write()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) Keys(c *gin.Context) {
|
2022-12-14 15:39:13 +08:00
|
|
|
res := &response.FileProcessKeys{}
|
2022-09-14 19:09:39 +08:00
|
|
|
keys, err := global.CACHE.PrefixScanKey("file-wget-")
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.Keys = keys
|
|
|
|
helper.SuccessWithData(c, res)
|
|
|
|
}
|