diff --git a/backend/app/api/v1/file.go b/backend/app/api/v1/file.go index ee0769772..c3fe34043 100644 --- a/backend/app/api/v1/file.go +++ b/backend/app/api/v1/file.go @@ -3,6 +3,8 @@ package v1 import ( "errors" "fmt" + "github.com/1Panel-dev/1Panel/backend/app/dto/request" + "github.com/1Panel-dev/1Panel/backend/app/dto/response" "io/ioutil" "net/http" "os" @@ -19,7 +21,7 @@ import ( ) func (b *BaseApi) ListFiles(c *gin.Context) { - var req dto.FileOption + var req request.FileOption if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -33,7 +35,7 @@ func (b *BaseApi) ListFiles(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -47,7 +49,7 @@ func (b *BaseApi) GetFileTree(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -61,7 +63,7 @@ func (b *BaseApi) CreateFile(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -75,7 +77,7 @@ func (b *BaseApi) DeleteFile(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -89,7 +91,7 @@ func (b *BaseApi) BatchDeleteFile(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -103,7 +105,7 @@ func (b *BaseApi) ChangeFileMode(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -117,7 +119,7 @@ func (b *BaseApi) CompressFile(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -131,7 +133,7 @@ func (b *BaseApi) DeCompressFile(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -145,7 +147,7 @@ func (b *BaseApi) GetContent(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -191,7 +193,7 @@ func (b *BaseApi) UploadFiles(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -204,7 +206,7 @@ func (b *BaseApi) ChangeFileName(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -214,13 +216,13 @@ func (b *BaseApi) WgetFile(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } - helper.SuccessWithData(c, dto.FileWgetRes{ + helper.SuccessWithData(c, response.FileWgetRes{ Key: key, }) } func (b *BaseApi) MoveFile(c *gin.Context) { - var req dto.FileMove + var req request.FileMove if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -233,7 +235,7 @@ func (b *BaseApi) MoveFile(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -247,7 +249,7 @@ func (b *BaseApi) Download(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 { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -303,7 +305,7 @@ func (b *BaseApi) Ws(c *gin.Context) { } func (b *BaseApi) Keys(c *gin.Context) { - res := &dto.FileProcessKeys{} + res := &response.FileProcessKeys{} keys, err := global.CACHE.PrefixScanKey("file-wget-") if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) diff --git a/backend/app/dto/file.go b/backend/app/dto/file.go deleted file mode 100644 index baad3adc0..000000000 --- a/backend/app/dto/file.go +++ /dev/null @@ -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 -} diff --git a/backend/app/dto/request/file.go b/backend/app/dto/request/file.go new file mode 100644 index 000000000..9ef2c4237 --- /dev/null +++ b/backend/app/dto/request/file.go @@ -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"` +} diff --git a/backend/app/dto/response/file.go b/backend/app/dto/response/file.go new file mode 100644 index 000000000..95aa9b29e --- /dev/null +++ b/backend/app/dto/response/file.go @@ -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"` +} diff --git a/backend/app/service/file.go b/backend/app/service/file.go index 344f3d48e..2bbbd497d 100644 --- a/backend/app/service/file.go +++ b/backend/app/service/file.go @@ -2,13 +2,14 @@ package service import ( "fmt" + "github.com/1Panel-dev/1Panel/backend/app/dto/request" + "github.com/1Panel-dev/1Panel/backend/app/dto/response" "io/fs" "os" "path/filepath" "strings" "time" - "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/files" @@ -19,8 +20,8 @@ import ( type FileService struct { } -func (f FileService) GetFileList(op dto.FileOption) (dto.FileInfo, error) { - var fileInfo dto.FileInfo +func (f FileService) GetFileList(op request.FileOption) (response.FileInfo, error) { + var fileInfo response.FileInfo if _, err := os.Stat(op.Path); err != nil && os.IsNotExist(err) { return fileInfo, nil } @@ -32,20 +33,20 @@ func (f FileService) GetFileList(op dto.FileOption) (dto.FileInfo, error) { return fileInfo, nil } -func (f FileService) GetFileTree(op dto.FileOption) ([]dto.FileTree, error) { - var treeArray []dto.FileTree +func (f FileService) GetFileTree(op request.FileOption) ([]response.FileTree, error) { + var treeArray []response.FileTree info, err := files.NewFileInfo(op.FileOption) if err != nil { return nil, err } - node := dto.FileTree{ + node := response.FileTree{ ID: common.GetUuid(), Name: info.Name, Path: info.Path, } for _, v := range info.Items { if v.IsDir { - node.Children = append(node.Children, dto.FileTree{ + node.Children = append(node.Children, response.FileTree{ ID: common.GetUuid(), Name: v.Name, Path: v.Path, @@ -55,7 +56,7 @@ func (f FileService) GetFileTree(op dto.FileOption) ([]dto.FileTree, error) { return append(treeArray, node), nil } -func (f FileService) Create(op dto.FileCreate) error { +func (f FileService) Create(op request.FileCreate) error { fo := files.NewFileOp() 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() if op.IsDir { 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() if op.IsDir { for _, file := range op.Paths { @@ -99,12 +100,12 @@ func (f FileService) BatchDelete(op dto.FileBatchDelete) error { return nil } -func (f FileService) ChangeMode(op dto.FileCreate) error { +func (f FileService) ChangeMode(op request.FileCreate) error { fo := files.NewFileOp() 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() if !c.Replace && fo.Stat(filepath.Join(c.Dst, c.Name)) { 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)) } -func (f FileService) DeCompress(c dto.FileDeCompress) error { +func (f FileService) DeCompress(c request.FileDeCompress) error { fo := files.NewFileOp() 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) 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{ 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) } -func (f FileService) ChangeName(re dto.FileRename) error { +func (f FileService) ChangeName(req request.FileRename) error { 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() key := "file-wget-" + uuid.NewV4().String() 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() if m.Type == "cut" { return fo.Cut(m.OldPaths, m.NewPath) @@ -177,7 +178,7 @@ func (f FileService) MvFile(m dto.FileMove) error { 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())) if err := os.MkdirAll(tempPath, os.ModePerm); err != nil { return "", err @@ -190,11 +191,11 @@ func (f FileService) FileDownload(d dto.FileDownload) (string, error) { 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() size, err := fo.GetDirSize(req.Path) if err != nil { - return dto.DirSizeRes{}, err + return response.DirSizeRes{}, err } - return dto.DirSizeRes{Size: size}, nil + return response.DirSizeRes{Size: size}, nil }