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

177 lines
4.0 KiB
Go
Raw Normal View History

2022-08-24 11:10:50 +08:00
package service
import (
"crypto/rand"
"fmt"
2022-08-24 11:10:50 +08:00
"github.com/1Panel-dev/1Panel/app/dto"
2022-09-06 15:46:46 +08:00
"github.com/1Panel-dev/1Panel/global"
2022-08-24 11:10:50 +08:00
"github.com/1Panel-dev/1Panel/utils/files"
"github.com/pkg/errors"
"io"
2022-08-25 17:54:52 +08:00
"io/fs"
2022-09-06 17:48:49 +08:00
"os"
2022-08-30 17:59:59 +08:00
"path/filepath"
2022-09-01 19:02:33 +08:00
"strings"
2022-09-06 17:48:49 +08:00
"time"
2022-08-24 11:10:50 +08:00
)
type FileService struct {
}
func (f FileService) GetFileList(op dto.FileOption) (dto.FileInfo, error) {
var fileInfo dto.FileInfo
info, err := files.NewFileInfo(op.FileOption)
if err != nil {
return fileInfo, err
}
fileInfo.FileInfo = *info
return fileInfo, nil
}
2022-08-24 17:34:21 +08:00
func (f FileService) GetFileTree(op dto.FileOption) ([]dto.FileTree, error) {
var treeArray []dto.FileTree
info, err := files.NewFileInfo(op.FileOption)
if err != nil {
return nil, err
}
node := dto.FileTree{
ID: getUuid(),
2022-08-24 17:34:21 +08:00
Name: info.Name,
Path: info.Path,
}
for _, v := range info.Items {
if v.IsDir {
node.Children = append(node.Children, dto.FileTree{
ID: getUuid(),
2022-08-24 17:34:21 +08:00
Name: v.Name,
Path: v.Path,
})
}
}
return append(treeArray, node), nil
}
2022-08-25 17:54:52 +08:00
func (f FileService) Create(op dto.FileCreate) error {
fo := files.NewFileOp()
if fo.Stat(op.Path) {
return errors.New("file is exist")
}
2022-08-25 17:54:52 +08:00
if op.IsDir {
return fo.CreateDir(op.Path, fs.FileMode(op.Mode))
} else {
if op.IsLink {
return fo.LinkFile(op.LinkPath, op.Path, op.IsSymlink)
} else {
return fo.CreateFile(op.Path)
}
2022-08-25 17:54:52 +08:00
}
}
2022-08-25 18:48:03 +08:00
func (f FileService) Delete(op dto.FileDelete) error {
fo := files.NewFileOp()
if op.IsDir {
return fo.DeleteDir(op.Path)
} else {
return fo.DeleteFile(op.Path)
}
}
func (f FileService) ChangeMode(op dto.FileCreate) error {
fo := files.NewFileOp()
return fo.Chmod(op.Path, fs.FileMode(op.Mode))
}
2022-08-30 17:59:59 +08:00
func (f FileService) Compress(c dto.FileCompress) error {
fo := files.NewFileOp()
if !c.Replace && fo.Stat(filepath.Join(c.Dst, c.Name)) {
return errors.New("file is exist")
}
return fo.Compress(c.Files, c.Dst, c.Name, files.CompressType(c.Type))
}
2022-08-31 13:59:02 +08:00
func (f FileService) DeCompress(c dto.FileDeCompress) error {
fo := files.NewFileOp()
return fo.Decompress(c.Path, c.Dst, files.CompressType(c.Type))
}
2022-09-06 17:48:49 +08:00
func (f FileService) GetContent(op dto.FileOption) (dto.FileInfo, error) {
info, err := files.NewFileInfo(op.FileOption)
2022-09-01 19:02:33 +08:00
if err != nil {
return dto.FileInfo{}, err
}
2022-09-09 11:20:02 +08:00
return dto.FileInfo{FileInfo: *info}, nil
2022-09-01 19:02:33 +08:00
}
2022-09-06 17:48:49 +08:00
func (f FileService) SaveContent(edit dto.FileEdit) error {
2022-09-01 19:02:33 +08:00
info, err := files.NewFileInfo(files.FileOption{
2022-09-06 17:48:49 +08:00
Path: edit.Path,
2022-09-01 19:02:33 +08:00
Expand: false,
})
if err != nil {
return err
}
fo := files.NewFileOp()
2022-09-06 17:48:49 +08:00
return fo.WriteFile(edit.Path, strings.NewReader(edit.Content), info.FileMode)
2022-09-01 19:02:33 +08:00
}
2022-09-06 17:48:49 +08:00
func (f FileService) ChangeName(re dto.FileRename) error {
2022-09-03 22:22:40 +08:00
fo := files.NewFileOp()
2022-09-06 17:48:49 +08:00
return fo.Rename(re.OldName, re.NewName)
2022-09-03 22:22:40 +08:00
}
2022-09-06 17:48:49 +08:00
func (f FileService) Wget(w dto.FileWget) error {
2022-09-05 16:25:26 +08:00
fo := files.NewFileOp()
2022-09-06 17:48:49 +08:00
return fo.DownloadFile(w.Url, filepath.Join(w.Path, w.Name))
2022-09-05 16:25:26 +08:00
}
2022-09-06 17:48:49 +08:00
func (f FileService) MvFile(m dto.FileMove) error {
2022-09-06 10:35:35 +08:00
fo := files.NewFileOp()
2022-09-06 17:48:49 +08:00
if m.Type == "cut" {
return fo.Cut(m.OldPaths, m.NewPath)
2022-09-06 10:35:35 +08:00
}
2022-09-06 15:46:46 +08:00
var errs []error
2022-09-06 17:48:49 +08:00
if m.Type == "copy" {
for _, src := range m.OldPaths {
if err := fo.Copy(src, m.NewPath); err != nil {
2022-09-06 15:46:46 +08:00
errs = append(errs, err)
2022-09-06 17:48:49 +08:00
global.LOG.Errorf("copy file [%s] to [%s] failed, err: %s", src, m.NewPath, err.Error())
2022-09-06 15:46:46 +08:00
}
}
}
var errString string
for _, err := range errs {
errString += err.Error() + "\n"
}
if errString != "" {
return errors.New(errString)
}
2022-09-06 10:35:35 +08:00
return nil
}
2022-09-06 17:48:49 +08:00
func (f FileService) FileDownload(d dto.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
}
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)
return filePath, nil
}
func getUuid() string {
b := make([]byte, 16)
2022-09-09 11:20:02 +08:00
_, _ = io.ReadFull(rand.Reader, b)
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}