1Panel/backend/app/service/file.go

285 lines
7.4 KiB
Go
Raw Normal View History

2022-08-24 03:10:50 +00:00
package service
import (
"fmt"
2022-12-01 02:36:49 +00:00
"io/fs"
"os"
"path/filepath"
"strings"
"time"
2023-03-02 05:54:07 +00:00
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/pkg/errors"
2022-08-24 03:10:50 +00:00
)
type FileService struct {
}
2023-03-28 10:00:06 +00:00
type IFileService interface {
GetFileList(op request.FileOption) (response.FileInfo, error)
SearchUploadWithPage(req request.SearchUploadWithPage) (int64, interface{}, error)
GetFileTree(op request.FileOption) ([]response.FileTree, error)
Create(op request.FileCreate) error
Delete(op request.FileDelete) error
BatchDelete(op request.FileBatchDelete) error
ChangeMode(op request.FileCreate) error
Compress(c request.FileCompress) error
DeCompress(c request.FileDeCompress) error
GetContent(op request.FileContentReq) (response.FileInfo, error)
2023-03-28 10:00:06 +00:00
SaveContent(edit request.FileEdit) error
FileDownload(d request.FileDownload) (string, error)
DirSize(req request.DirSizeReq) (response.DirSizeRes, error)
ChangeName(req request.FileRename) error
Wget(w request.FileWget) (string, error)
MvFile(m request.FileMove) error
ChangeOwner(req request.FileRoleUpdate) error
2023-03-28 10:00:06 +00:00
}
func NewIFileService() IFileService {
return &FileService{}
}
func (f *FileService) GetFileList(op request.FileOption) (response.FileInfo, error) {
2022-12-14 07:39:13 +00:00
var fileInfo response.FileInfo
2022-12-01 02:36:49 +00:00
if _, err := os.Stat(op.Path); err != nil && os.IsNotExist(err) {
return fileInfo, nil
}
2022-08-24 03:10:50 +00:00
info, err := files.NewFileInfo(op.FileOption)
if err != nil {
return fileInfo, err
}
fileInfo.FileInfo = *info
return fileInfo, nil
}
2022-08-24 09:34:21 +00:00
2023-03-28 10:00:06 +00:00
func (f *FileService) SearchUploadWithPage(req request.SearchUploadWithPage) (int64, interface{}, error) {
2023-03-02 05:54:07 +00:00
var (
files []response.UploadInfo
backData []response.UploadInfo
2023-03-02 05:54:07 +00:00
)
_ = filepath.Walk(req.Path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !info.IsDir() {
files = append(files, response.UploadInfo{
CreatedAt: info.ModTime().Format("2006-01-02 15:04:05"),
Size: int(info.Size()),
Name: info.Name(),
})
}
return nil
})
total, start, end := len(files), (req.Page-1)*req.PageSize, req.Page*req.PageSize
if start > total {
backData = make([]response.UploadInfo, 0)
2023-03-02 05:54:07 +00:00
} else {
if end >= total {
end = total
}
backData = files[start:end]
2023-03-02 05:54:07 +00:00
}
return int64(total), backData, nil
2023-03-02 05:54:07 +00:00
}
2023-03-28 10:00:06 +00:00
func (f *FileService) GetFileTree(op request.FileOption) ([]response.FileTree, error) {
2022-12-14 07:39:13 +00:00
var treeArray []response.FileTree
2022-08-24 09:34:21 +00:00
info, err := files.NewFileInfo(op.FileOption)
if err != nil {
return nil, err
}
2022-12-14 07:39:13 +00:00
node := response.FileTree{
ID: common.GetUuid(),
2022-08-24 09:34:21 +00:00
Name: info.Name,
Path: info.Path,
}
for _, v := range info.Items {
if v.IsDir {
2022-12-14 07:39:13 +00:00
node.Children = append(node.Children, response.FileTree{
ID: common.GetUuid(),
2022-08-24 09:34:21 +00:00
Name: v.Name,
Path: v.Path,
})
}
}
return append(treeArray, node), nil
}
2023-03-28 10:00:06 +00:00
func (f *FileService) Create(op request.FileCreate) error {
2022-08-25 09:54:52 +00:00
fo := files.NewFileOp()
if fo.Stat(op.Path) {
return buserr.New(constant.ErrFileIsExit)
}
2022-08-25 09:54:52 +00:00
if op.IsDir {
return fo.CreateDir(op.Path, fs.FileMode(op.Mode))
} else {
if op.IsLink {
2023-03-12 03:09:38 +00:00
if !fo.Stat(op.LinkPath) {
return buserr.New(constant.ErrLinkPathNotFound)
}
return fo.LinkFile(op.LinkPath, op.Path, op.IsSymlink)
} else {
return fo.CreateFile(op.Path)
}
2022-08-25 09:54:52 +00:00
}
}
2023-03-28 10:00:06 +00:00
func (f *FileService) Delete(op request.FileDelete) error {
2022-08-25 10:48:03 +00:00
fo := files.NewFileOp()
if op.ForceDelete {
if op.IsDir {
return fo.DeleteDir(op.Path)
} else {
return fo.DeleteFile(op.Path)
}
2022-08-25 10:48:03 +00:00
}
if err := NewIRecycleBinService().Create(request.RecycleBinCreate{SourcePath: op.Path}); err != nil {
return err
}
return favoriteRepo.Delete(favoriteRepo.WithByPath(op.Path))
2022-08-25 10:48:03 +00:00
}
2023-03-28 10:00:06 +00:00
func (f *FileService) BatchDelete(op request.FileBatchDelete) error {
2022-12-01 02:36:49 +00:00
fo := files.NewFileOp()
if op.IsDir {
for _, file := range op.Paths {
if err := fo.DeleteDir(file); err != nil {
return err
}
}
} else {
for _, file := range op.Paths {
if err := fo.DeleteFile(file); err != nil {
return err
}
}
}
return nil
}
2023-03-28 10:00:06 +00:00
func (f *FileService) ChangeMode(op request.FileCreate) error {
fo := files.NewFileOp()
if op.Sub {
return fo.ChmodR(op.Path, op.Mode)
} else {
return fo.Chmod(op.Path, fs.FileMode(op.Mode))
}
}
func (f *FileService) ChangeOwner(req request.FileRoleUpdate) error {
fo := files.NewFileOp()
return fo.ChownR(req.Path, req.User, req.Group, req.Sub)
}
2023-03-28 10:00:06 +00:00
func (f *FileService) Compress(c request.FileCompress) error {
2022-08-30 09:59:59 +00:00
fo := files.NewFileOp()
if !c.Replace && fo.Stat(filepath.Join(c.Dst, c.Name)) {
return buserr.New(constant.ErrFileIsExit)
2022-08-30 09:59:59 +00:00
}
return fo.Compress(c.Files, c.Dst, c.Name, files.CompressType(c.Type))
}
2023-03-28 10:00:06 +00:00
func (f *FileService) DeCompress(c request.FileDeCompress) error {
2022-08-31 05:59:02 +00:00
fo := files.NewFileOp()
return fo.Decompress(c.Path, c.Dst, files.CompressType(c.Type))
}
func (f *FileService) GetContent(op request.FileContentReq) (response.FileInfo, error) {
info, err := files.NewFileInfo(files.FileOption{
Path: op.Path,
})
2022-09-01 11:02:33 +00:00
if err != nil {
2022-12-14 07:39:13 +00:00
return response.FileInfo{}, err
2022-09-01 11:02:33 +00:00
}
2022-12-14 07:39:13 +00:00
return response.FileInfo{FileInfo: *info}, nil
2022-09-01 11:02:33 +00:00
}
2023-03-28 10:00:06 +00:00
func (f *FileService) SaveContent(edit request.FileEdit) error {
2022-09-01 11:02:33 +00:00
info, err := files.NewFileInfo(files.FileOption{
2022-09-06 09:48:49 +00:00
Path: edit.Path,
2022-09-01 11:02:33 +00:00
Expand: false,
})
if err != nil {
return err
}
fo := files.NewFileOp()
2022-09-06 09:48:49 +00:00
return fo.WriteFile(edit.Path, strings.NewReader(edit.Content), info.FileMode)
2022-09-01 11:02:33 +00:00
}
2023-03-28 10:00:06 +00:00
func (f *FileService) ChangeName(req request.FileRename) error {
2022-09-03 14:22:40 +00:00
fo := files.NewFileOp()
2022-12-14 07:39:13 +00:00
return fo.Rename(req.OldName, req.NewName)
2022-09-03 14:22:40 +00:00
}
2023-03-28 10:00:06 +00:00
func (f *FileService) Wget(w request.FileWget) (string, error) {
2022-09-05 08:25:26 +00:00
fo := files.NewFileOp()
2023-03-11 11:55:37 +00:00
key := "file-wget-" + common.GetUuid()
2022-10-14 06:48:55 +00:00
return key, fo.DownloadFileWithProcess(w.Url, filepath.Join(w.Path, w.Name), key)
2022-09-05 08:25:26 +00:00
}
2023-03-28 10:00:06 +00:00
func (f *FileService) MvFile(m request.FileMove) error {
2022-09-06 02:35:35 +00:00
fo := files.NewFileOp()
2023-02-09 06:31:05 +00:00
if !fo.Stat(m.NewPath) {
return buserr.New(constant.ErrPathNotFound)
}
2023-03-10 03:44:58 +00:00
for _, path := range m.OldPaths {
if path == m.NewPath || strings.Contains(m.NewPath, path) {
return buserr.New(constant.ErrMovePathFailed)
}
}
2022-09-06 09:48:49 +00:00
if m.Type == "cut" {
return fo.Cut(m.OldPaths, m.NewPath)
2022-09-06 02:35:35 +00:00
}
2022-09-06 07:46:46 +00:00
var errs []error
2022-09-06 09:48:49 +00:00
if m.Type == "copy" {
for _, src := range m.OldPaths {
if err := fo.Copy(src, m.NewPath); err != nil {
2022-09-06 07:46:46 +00:00
errs = append(errs, err)
2022-09-06 09:48:49 +00:00
global.LOG.Errorf("copy file [%s] to [%s] failed, err: %s", src, m.NewPath, err.Error())
2022-09-06 07:46:46 +00:00
}
}
}
var errString string
for _, err := range errs {
errString += err.Error() + "\n"
}
if errString != "" {
return errors.New(errString)
}
2022-09-06 02:35:35 +00:00
return nil
}
2023-03-28 10:00:06 +00:00
func (f *FileService) FileDownload(d request.FileDownload) (string, error) {
filePath := d.Paths[0]
if d.Compress {
tempPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().UnixNano()))
if err := os.MkdirAll(tempPath, os.ModePerm); err != nil {
return "", err
}
fo := files.NewFileOp()
if err := fo.Compress(d.Paths, tempPath, d.Name, files.CompressType(d.Type)); err != nil {
return "", err
}
filePath = filepath.Join(tempPath, d.Name)
2022-09-06 09:48:49 +00:00
}
return filePath, nil
}
2023-03-28 10:00:06 +00:00
func (f *FileService) DirSize(req request.DirSizeReq) (response.DirSizeRes, error) {
fo := files.NewFileOp()
size, err := fo.GetDirSize(req.Path)
if err != nil {
2022-12-14 07:39:13 +00:00
return response.DirSizeRes{}, err
}
2022-12-14 07:39:13 +00:00
return response.DirSizeRes{Size: size}, nil
}