style: 修改文件代码结构

This commit is contained in:
zhengkunwang223 2022-12-14 15:39:13 +08:00 committed by zhengkunwang223
parent e99badb738
commit 306e5c5b9e
5 changed files with 148 additions and 151 deletions

View file

@ -3,6 +3,8 @@ package v1
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
@ -19,7 +21,7 @@ import (
) )
func (b *BaseApi) ListFiles(c *gin.Context) { func (b *BaseApi) ListFiles(c *gin.Context) {
var req dto.FileOption var req request.FileOption
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -33,7 +35,7 @@ func (b *BaseApi) ListFiles(c *gin.Context) {
} }
func (b *BaseApi) GetFileTree(c *gin.Context) { func (b *BaseApi) GetFileTree(c *gin.Context) {
var req dto.FileOption var req request.FileOption
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -47,7 +49,7 @@ func (b *BaseApi) GetFileTree(c *gin.Context) {
} }
func (b *BaseApi) CreateFile(c *gin.Context) { func (b *BaseApi) CreateFile(c *gin.Context) {
var req dto.FileCreate var req request.FileCreate
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -61,7 +63,7 @@ func (b *BaseApi) CreateFile(c *gin.Context) {
} }
func (b *BaseApi) DeleteFile(c *gin.Context) { func (b *BaseApi) DeleteFile(c *gin.Context) {
var req dto.FileDelete var req request.FileDelete
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -75,7 +77,7 @@ func (b *BaseApi) DeleteFile(c *gin.Context) {
} }
func (b *BaseApi) BatchDeleteFile(c *gin.Context) { func (b *BaseApi) BatchDeleteFile(c *gin.Context) {
var req dto.FileBatchDelete var req request.FileBatchDelete
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -89,7 +91,7 @@ func (b *BaseApi) BatchDeleteFile(c *gin.Context) {
} }
func (b *BaseApi) ChangeFileMode(c *gin.Context) { func (b *BaseApi) ChangeFileMode(c *gin.Context) {
var req dto.FileCreate var req request.FileCreate
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -103,7 +105,7 @@ func (b *BaseApi) ChangeFileMode(c *gin.Context) {
} }
func (b *BaseApi) CompressFile(c *gin.Context) { func (b *BaseApi) CompressFile(c *gin.Context) {
var req dto.FileCompress var req request.FileCompress
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -117,7 +119,7 @@ func (b *BaseApi) CompressFile(c *gin.Context) {
} }
func (b *BaseApi) DeCompressFile(c *gin.Context) { func (b *BaseApi) DeCompressFile(c *gin.Context) {
var req dto.FileDeCompress var req request.FileDeCompress
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -131,7 +133,7 @@ func (b *BaseApi) DeCompressFile(c *gin.Context) {
} }
func (b *BaseApi) GetContent(c *gin.Context) { func (b *BaseApi) GetContent(c *gin.Context) {
var req dto.FileOption var req request.FileOption
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -145,7 +147,7 @@ func (b *BaseApi) GetContent(c *gin.Context) {
} }
func (b *BaseApi) SaveContent(c *gin.Context) { func (b *BaseApi) SaveContent(c *gin.Context) {
var req dto.FileEdit var req request.FileEdit
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -191,7 +193,7 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
} }
func (b *BaseApi) ChangeFileName(c *gin.Context) { func (b *BaseApi) ChangeFileName(c *gin.Context) {
var req dto.FileRename var req request.FileRename
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -204,7 +206,7 @@ func (b *BaseApi) ChangeFileName(c *gin.Context) {
} }
func (b *BaseApi) WgetFile(c *gin.Context) { func (b *BaseApi) WgetFile(c *gin.Context) {
var req dto.FileWget var req request.FileWget
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -214,13 +216,13 @@ func (b *BaseApi) WgetFile(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return return
} }
helper.SuccessWithData(c, dto.FileWgetRes{ helper.SuccessWithData(c, response.FileWgetRes{
Key: key, Key: key,
}) })
} }
func (b *BaseApi) MoveFile(c *gin.Context) { func (b *BaseApi) MoveFile(c *gin.Context) {
var req dto.FileMove var req request.FileMove
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -233,7 +235,7 @@ func (b *BaseApi) MoveFile(c *gin.Context) {
} }
func (b *BaseApi) Download(c *gin.Context) { func (b *BaseApi) Download(c *gin.Context) {
var req dto.FileDownload var req request.FileDownload
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -247,7 +249,7 @@ func (b *BaseApi) Download(c *gin.Context) {
} }
func (b *BaseApi) Size(c *gin.Context) { func (b *BaseApi) Size(c *gin.Context) {
var req dto.DirSizeReq var req request.DirSizeReq
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -303,7 +305,7 @@ func (b *BaseApi) Ws(c *gin.Context) {
} }
func (b *BaseApi) Keys(c *gin.Context) { func (b *BaseApi) Keys(c *gin.Context) {
res := &dto.FileProcessKeys{} res := &response.FileProcessKeys{}
keys, err := global.CACHE.PrefixScanKey("file-wget-") keys, err := global.CACHE.PrefixScanKey("file-wget-")
if err != nil { if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)

View file

@ -1,109 +0,0 @@
package dto
import (
"github.com/1Panel-dev/1Panel/backend/utils/files"
)
type FileOption struct {
files.FileOption
}
type FileInfo struct {
files.FileInfo
}
type FileTree struct {
ID string `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Children []FileTree `json:"children"`
}
type FileCreate struct {
Path string
Content string
IsDir bool
Mode int64
IsLink bool
IsSymlink bool
LinkPath string
}
type FileDelete struct {
Path string
IsDir bool
}
type FileBatchDelete struct {
IsDir bool
Paths []string
}
type FileCompress struct {
Files []string
Dst string
Type string
Name string
Replace bool
}
type FileDeCompress struct {
Dst string
Type string
Path string
}
type FileEdit struct {
Path string
Content string
}
type FileRename struct {
OldName string
NewName string
}
type FileWget struct {
Url string `json:"url" validate:"required"`
Path string `json:"path" validate:"required"`
Name string `json:"name" validate:"required"`
}
type FileMove struct {
Type string `json:"type" validate:"required"`
OldPaths []string `json:"oldPaths" validate:"required"`
NewPath string `json:"newPath" validate:"required"`
}
type FileDownload struct {
Paths []string `json:"paths" validate:"required"`
Type string `json:"type" validate:"required"`
Name string `json:"name" validate:"required"`
}
type DirSizeReq struct {
Path string `json:"path" validate:"required"`
}
type DirSizeRes struct {
Size float64 `json:"size" validate:"required"`
}
type FileProcess struct {
Total uint64 `json:"total"`
Written uint64 `json:"written"`
Percent float64 `json:"percent"`
Name string `json:"name"`
}
type FileProcessReq struct {
Key string `json:"key"`
}
type FileProcessKeys struct {
Keys []string `json:"keys"`
}
type FileWgetRes struct {
Key string
}

View file

@ -0,0 +1,77 @@
package request
import "github.com/1Panel-dev/1Panel/backend/utils/files"
type FileOption struct {
files.FileOption
}
type FileCreate struct {
Path string `json:"path" validate:"required"`
Content string `json:"content"`
IsDir bool `json:"isDir"`
Mode int64 `json:"mode" validate:"required"`
IsLink bool `json:"isLink"`
IsSymlink bool `json:"isSymlink "`
LinkPath string `json:"linkPath"`
}
type FileDelete struct {
Path string `json:"path" validate:"required"`
IsDir bool `json:"isDir"`
}
type FileBatchDelete struct {
Paths []string `json:"paths" validate:"required"`
IsDir bool `json:"isDir"`
}
type FileCompress struct {
Files []string `json:"files" validate:"required"`
Dst string `json:"dst" validate:"required"`
Type string `json:"type" validate:"required"`
Name string `json:"name" validate:"required"`
Replace bool `json:"replace"`
}
type FileDeCompress struct {
Dst string `json:"dst" validate:"required"`
Type string `json:"type" validate:"required"`
Path string `json:"path" validate:"required"`
}
type FileEdit struct {
Path string `json:"path" validate:"required"`
Content string `json:"content" validate:"required"`
}
type FileRename struct {
OldName string `json:"oldName" validate:"required"`
NewName string `json:"newName" validate:"required"`
}
type FileWget struct {
Url string `json:"url" validate:"required"`
Path string `json:"path" validate:"required"`
Name string `json:"name" validate:"required"`
}
type FileMove struct {
Type string `json:"type" validate:"required"`
OldPaths []string `json:"oldPaths" validate:"required"`
NewPath string `json:"newPath" validate:"required"`
}
type FileDownload struct {
Paths []string `json:"paths" validate:"required"`
Type string `json:"type" validate:"required"`
Name string `json:"name" validate:"required"`
}
type DirSizeReq struct {
Path string `json:"path" validate:"required"`
}
type FileProcessReq struct {
Key string `json:"key"`
}

View file

@ -0,0 +1,26 @@
package response
import "github.com/1Panel-dev/1Panel/backend/utils/files"
type FileInfo struct {
files.FileInfo
}
type FileTree struct {
ID string `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Children []FileTree `json:"children"`
}
type DirSizeRes struct {
Size float64 `json:"size" validate:"required"`
}
type FileProcessKeys struct {
Keys []string `json:"keys"`
}
type FileWgetRes struct {
Key string `json:"key"`
}

View file

@ -2,13 +2,14 @@ package service
import ( import (
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
@ -19,8 +20,8 @@ import (
type FileService struct { type FileService struct {
} }
func (f FileService) GetFileList(op dto.FileOption) (dto.FileInfo, error) { func (f FileService) GetFileList(op request.FileOption) (response.FileInfo, error) {
var fileInfo dto.FileInfo var fileInfo response.FileInfo
if _, err := os.Stat(op.Path); err != nil && os.IsNotExist(err) { if _, err := os.Stat(op.Path); err != nil && os.IsNotExist(err) {
return fileInfo, nil return fileInfo, nil
} }
@ -32,20 +33,20 @@ func (f FileService) GetFileList(op dto.FileOption) (dto.FileInfo, error) {
return fileInfo, nil return fileInfo, nil
} }
func (f FileService) GetFileTree(op dto.FileOption) ([]dto.FileTree, error) { func (f FileService) GetFileTree(op request.FileOption) ([]response.FileTree, error) {
var treeArray []dto.FileTree var treeArray []response.FileTree
info, err := files.NewFileInfo(op.FileOption) info, err := files.NewFileInfo(op.FileOption)
if err != nil { if err != nil {
return nil, err return nil, err
} }
node := dto.FileTree{ node := response.FileTree{
ID: common.GetUuid(), ID: common.GetUuid(),
Name: info.Name, Name: info.Name,
Path: info.Path, Path: info.Path,
} }
for _, v := range info.Items { for _, v := range info.Items {
if v.IsDir { if v.IsDir {
node.Children = append(node.Children, dto.FileTree{ node.Children = append(node.Children, response.FileTree{
ID: common.GetUuid(), ID: common.GetUuid(),
Name: v.Name, Name: v.Name,
Path: v.Path, Path: v.Path,
@ -55,7 +56,7 @@ func (f FileService) GetFileTree(op dto.FileOption) ([]dto.FileTree, error) {
return append(treeArray, node), nil return append(treeArray, node), nil
} }
func (f FileService) Create(op dto.FileCreate) error { func (f FileService) Create(op request.FileCreate) error {
fo := files.NewFileOp() fo := files.NewFileOp()
if fo.Stat(op.Path) { if fo.Stat(op.Path) {
@ -72,7 +73,7 @@ func (f FileService) Create(op dto.FileCreate) error {
} }
} }
func (f FileService) Delete(op dto.FileDelete) error { func (f FileService) Delete(op request.FileDelete) error {
fo := files.NewFileOp() fo := files.NewFileOp()
if op.IsDir { if op.IsDir {
return fo.DeleteDir(op.Path) return fo.DeleteDir(op.Path)
@ -81,7 +82,7 @@ func (f FileService) Delete(op dto.FileDelete) error {
} }
} }
func (f FileService) BatchDelete(op dto.FileBatchDelete) error { func (f FileService) BatchDelete(op request.FileBatchDelete) error {
fo := files.NewFileOp() fo := files.NewFileOp()
if op.IsDir { if op.IsDir {
for _, file := range op.Paths { for _, file := range op.Paths {
@ -99,12 +100,12 @@ func (f FileService) BatchDelete(op dto.FileBatchDelete) error {
return nil return nil
} }
func (f FileService) ChangeMode(op dto.FileCreate) error { func (f FileService) ChangeMode(op request.FileCreate) error {
fo := files.NewFileOp() fo := files.NewFileOp()
return fo.Chmod(op.Path, fs.FileMode(op.Mode)) return fo.Chmod(op.Path, fs.FileMode(op.Mode))
} }
func (f FileService) Compress(c dto.FileCompress) error { func (f FileService) Compress(c request.FileCompress) error {
fo := files.NewFileOp() fo := files.NewFileOp()
if !c.Replace && fo.Stat(filepath.Join(c.Dst, c.Name)) { if !c.Replace && fo.Stat(filepath.Join(c.Dst, c.Name)) {
return errors.New("file is exist") return errors.New("file is exist")
@ -113,20 +114,20 @@ func (f FileService) Compress(c dto.FileCompress) error {
return fo.Compress(c.Files, c.Dst, c.Name, files.CompressType(c.Type)) return fo.Compress(c.Files, c.Dst, c.Name, files.CompressType(c.Type))
} }
func (f FileService) DeCompress(c dto.FileDeCompress) error { func (f FileService) DeCompress(c request.FileDeCompress) error {
fo := files.NewFileOp() fo := files.NewFileOp()
return fo.Decompress(c.Path, c.Dst, files.CompressType(c.Type)) return fo.Decompress(c.Path, c.Dst, files.CompressType(c.Type))
} }
func (f FileService) GetContent(op dto.FileOption) (dto.FileInfo, error) { func (f FileService) GetContent(op request.FileOption) (response.FileInfo, error) {
info, err := files.NewFileInfo(op.FileOption) info, err := files.NewFileInfo(op.FileOption)
if err != nil { if err != nil {
return dto.FileInfo{}, err return response.FileInfo{}, err
} }
return dto.FileInfo{FileInfo: *info}, nil return response.FileInfo{FileInfo: *info}, nil
} }
func (f FileService) SaveContent(edit dto.FileEdit) error { func (f FileService) SaveContent(edit request.FileEdit) error {
info, err := files.NewFileInfo(files.FileOption{ info, err := files.NewFileInfo(files.FileOption{
Path: edit.Path, Path: edit.Path,
@ -140,18 +141,18 @@ func (f FileService) SaveContent(edit dto.FileEdit) error {
return fo.WriteFile(edit.Path, strings.NewReader(edit.Content), info.FileMode) return fo.WriteFile(edit.Path, strings.NewReader(edit.Content), info.FileMode)
} }
func (f FileService) ChangeName(re dto.FileRename) error { func (f FileService) ChangeName(req request.FileRename) error {
fo := files.NewFileOp() fo := files.NewFileOp()
return fo.Rename(re.OldName, re.NewName) return fo.Rename(req.OldName, req.NewName)
} }
func (f FileService) Wget(w dto.FileWget) (string, error) { func (f FileService) Wget(w request.FileWget) (string, error) {
fo := files.NewFileOp() fo := files.NewFileOp()
key := "file-wget-" + uuid.NewV4().String() key := "file-wget-" + uuid.NewV4().String()
return key, fo.DownloadFileWithProcess(w.Url, filepath.Join(w.Path, w.Name), key) return key, fo.DownloadFileWithProcess(w.Url, filepath.Join(w.Path, w.Name), key)
} }
func (f FileService) MvFile(m dto.FileMove) error { func (f FileService) MvFile(m request.FileMove) error {
fo := files.NewFileOp() fo := files.NewFileOp()
if m.Type == "cut" { if m.Type == "cut" {
return fo.Cut(m.OldPaths, m.NewPath) return fo.Cut(m.OldPaths, m.NewPath)
@ -177,7 +178,7 @@ func (f FileService) MvFile(m dto.FileMove) error {
return nil return nil
} }
func (f FileService) FileDownload(d dto.FileDownload) (string, error) { func (f FileService) FileDownload(d request.FileDownload) (string, error) {
tempPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().UnixNano())) tempPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().UnixNano()))
if err := os.MkdirAll(tempPath, os.ModePerm); err != nil { if err := os.MkdirAll(tempPath, os.ModePerm); err != nil {
return "", err return "", err
@ -190,11 +191,11 @@ func (f FileService) FileDownload(d dto.FileDownload) (string, error) {
return filePath, nil return filePath, nil
} }
func (f FileService) DirSize(req dto.DirSizeReq) (dto.DirSizeRes, error) { func (f FileService) DirSize(req request.DirSizeReq) (response.DirSizeRes, error) {
fo := files.NewFileOp() fo := files.NewFileOp()
size, err := fo.GetDirSize(req.Path) size, err := fo.GetDirSize(req.Path)
if err != nil { if err != nil {
return dto.DirSizeRes{}, err return response.DirSizeRes{}, err
} }
return dto.DirSizeRes{Size: size}, nil return response.DirSizeRes{Size: size}, nil
} }