2022-08-24 03:10:50 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-08-24 09:58:58 +00:00
|
|
|
"fmt"
|
2024-05-22 10:32:47 +00:00
|
|
|
"io"
|
2022-12-01 02:36:49 +00:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
2024-05-06 05:51:55 +00:00
|
|
|
"os/exec"
|
2023-11-23 03:00:08 +00:00
|
|
|
"path"
|
2022-12-01 02:36:49 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2024-06-12 06:32:37 +00:00
|
|
|
"unicode/utf8"
|
2022-12-01 02:36:49 +00:00
|
|
|
|
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"
|
2024-05-22 10:32:47 +00:00
|
|
|
"golang.org/x/net/html/charset"
|
2024-06-28 06:16:57 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2024-05-22 10:32:47 +00:00
|
|
|
"golang.org/x/text/encoding/simplifiedchinese"
|
|
|
|
"golang.org/x/text/transform"
|
2023-03-02 05:54:07 +00:00
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2022-08-29 07:26:36 +00:00
|
|
|
"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
|
|
|
|
Compress(c request.FileCompress) error
|
|
|
|
DeCompress(c request.FileDeCompress) error
|
2023-10-27 07:27:25 +00:00
|
|
|
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
|
2023-04-26 14:18:16 +00:00
|
|
|
ChangeOwner(req request.FileRoleUpdate) error
|
2023-11-02 10:38:50 +00:00
|
|
|
ChangeMode(op request.FileCreate) error
|
|
|
|
BatchChangeModeAndOwner(op request.FileRoleReq) error
|
2023-11-23 03:00:08 +00:00
|
|
|
ReadLogByLine(req request.FileReadByLineReq) (*response.FileLineContent, error)
|
2023-03-28 10:00:06 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 06:16:57 +00:00
|
|
|
var filteredPaths = []string{
|
|
|
|
"/.1panel_clash",
|
|
|
|
}
|
|
|
|
|
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
|
2024-09-10 03:46:08 +00:00
|
|
|
data, err := os.Stat(op.Path)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
2022-12-01 02:36:49 +00:00
|
|
|
return fileInfo, nil
|
|
|
|
}
|
2024-09-10 03:46:08 +00:00
|
|
|
if !data.IsDir() {
|
|
|
|
op.FileOption.Path = filepath.Dir(op.FileOption.Path)
|
|
|
|
}
|
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 (
|
2023-03-06 10:12:31 +00:00
|
|
|
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{
|
2024-06-28 06:04:08 +00:00
|
|
|
CreatedAt: info.ModTime().Format(constant.DateTimeLayout),
|
2023-03-02 05:54:07 +00:00
|
|
|
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 {
|
2023-03-06 10:12:31 +00:00
|
|
|
backData = make([]response.UploadInfo, 0)
|
2023-03-02 05:54:07 +00:00
|
|
|
} else {
|
|
|
|
if end >= total {
|
|
|
|
end = total
|
|
|
|
}
|
2023-03-06 10:12:31 +00:00
|
|
|
backData = files[start:end]
|
2023-03-02 05:54:07 +00:00
|
|
|
}
|
2023-03-06 10:12:31 +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
|
2024-06-28 06:16:57 +00:00
|
|
|
if _, err := os.Stat(op.Path); err != nil && os.IsNotExist(err) {
|
|
|
|
return treeArray, nil
|
|
|
|
}
|
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{
|
2024-06-28 06:16:57 +00:00
|
|
|
ID: common.GetUuid(),
|
|
|
|
Name: info.Name,
|
|
|
|
Path: info.Path,
|
|
|
|
IsDir: info.IsDir,
|
|
|
|
Extension: info.Extension,
|
2022-08-24 09:34:21 +00:00
|
|
|
}
|
2024-06-28 06:16:57 +00:00
|
|
|
err = f.buildFileTree(&node, info.Items, op, 2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-24 09:34:21 +00:00
|
|
|
}
|
|
|
|
return append(treeArray, node), nil
|
|
|
|
}
|
2022-08-24 09:58:58 +00:00
|
|
|
|
2024-06-28 06:16:57 +00:00
|
|
|
func shouldFilterPath(path string) bool {
|
|
|
|
cleanedPath := filepath.Clean(path)
|
|
|
|
for _, filteredPath := range filteredPaths {
|
|
|
|
cleanedFilteredPath := filepath.Clean(filteredPath)
|
|
|
|
if cleanedFilteredPath == cleanedPath || strings.HasPrefix(cleanedPath, cleanedFilteredPath+"/") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 递归构建文件树(只取当前目录以及当前目录下的第一层子节点)
|
|
|
|
func (f *FileService) buildFileTree(node *response.FileTree, items []*files.FileInfo, op request.FileOption, level int) error {
|
|
|
|
for _, v := range items {
|
|
|
|
if shouldFilterPath(v.Path) {
|
2024-08-20 03:38:47 +00:00
|
|
|
global.LOG.Infof("File Tree: Skipping %s due to filter\n", v.Path)
|
2024-06-28 06:16:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
childNode := response.FileTree{
|
|
|
|
ID: common.GetUuid(),
|
|
|
|
Name: v.Name,
|
|
|
|
Path: v.Path,
|
|
|
|
IsDir: v.IsDir,
|
|
|
|
Extension: v.Extension,
|
|
|
|
}
|
|
|
|
if level > 1 && v.IsDir {
|
|
|
|
if err := f.buildChildNode(&childNode, v, op, level); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Children = append(node.Children, childNode)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileService) buildChildNode(childNode *response.FileTree, fileInfo *files.FileInfo, op request.FileOption, level int) error {
|
|
|
|
op.Path = fileInfo.Path
|
|
|
|
subInfo, err := files.NewFileInfo(op.FileOption)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsPermission(err) || errors.Is(err, unix.EACCES) {
|
2024-08-20 03:38:47 +00:00
|
|
|
global.LOG.Infof("File Tree: Skipping %s due to permission denied\n", fileInfo.Path)
|
2024-06-28 06:16:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
global.LOG.Errorf("File Tree: Skipping %s due to error: %s\n", fileInfo.Path, err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.buildFileTree(childNode, subInfo.Items, op, level-1)
|
|
|
|
}
|
|
|
|
|
2023-03-28 10:00:06 +00:00
|
|
|
func (f *FileService) Create(op request.FileCreate) error {
|
2024-04-11 10:32:11 +00:00
|
|
|
if files.IsInvalidChar(op.Path) {
|
|
|
|
return buserr.New("ErrInvalidChar")
|
|
|
|
}
|
2022-08-25 09:54:52 +00:00
|
|
|
fo := files.NewFileOp()
|
2022-08-29 07:26:36 +00:00
|
|
|
if fo.Stat(op.Path) {
|
2024-07-13 13:58:02 +00:00
|
|
|
return buserr.New(constant.ErrFileIsExist)
|
2022-08-29 07:26:36 +00:00
|
|
|
}
|
2024-04-09 07:34:08 +00:00
|
|
|
mode := op.Mode
|
|
|
|
if mode == 0 {
|
|
|
|
fileInfo, err := os.Stat(filepath.Dir(op.Path))
|
|
|
|
if err == nil {
|
|
|
|
mode = int64(fileInfo.Mode().Perm())
|
2022-08-31 08:00:51 +00:00
|
|
|
} else {
|
2024-04-09 07:34:08 +00:00
|
|
|
mode = 0755
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if op.IsDir {
|
2024-04-18 14:40:07 +00:00
|
|
|
return fo.CreateDirWithMode(op.Path, fs.FileMode(mode))
|
2024-04-09 07:34:08 +00:00
|
|
|
}
|
|
|
|
if op.IsLink {
|
|
|
|
if !fo.Stat(op.LinkPath) {
|
|
|
|
return buserr.New(constant.ErrLinkPathNotFound)
|
2022-08-31 08:00:51 +00:00
|
|
|
}
|
2024-04-09 07:34:08 +00:00
|
|
|
return fo.LinkFile(op.LinkPath, op.Path, op.IsSymlink)
|
2022-08-25 09:54:52 +00:00
|
|
|
}
|
2024-04-09 07:34:08 +00:00
|
|
|
return fo.CreateFileWithMode(op.Path, fs.FileMode(mode))
|
2022-08-25 09:54:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 10:00:06 +00:00
|
|
|
func (f *FileService) Delete(op request.FileDelete) error {
|
2024-11-13 07:10:00 +00:00
|
|
|
if op.IsDir {
|
|
|
|
excludeDir := global.CONF.System.DataDir
|
2024-11-13 14:12:01 +00:00
|
|
|
if filepath.Base(op.Path) == ".1panel_clash" || op.Path == excludeDir {
|
2024-11-13 07:10:00 +00:00
|
|
|
return buserr.New(constant.ErrPathNotDelete)
|
|
|
|
}
|
2024-11-13 06:52:02 +00:00
|
|
|
}
|
2022-08-25 10:48:03 +00:00
|
|
|
fo := files.NewFileOp()
|
2023-12-04 06:56:09 +00:00
|
|
|
recycleBinStatus, _ := settingRepo.Get(settingRepo.WithByKey("FileRecycleBin"))
|
|
|
|
if recycleBinStatus.Value == "disable" {
|
|
|
|
op.ForceDelete = true
|
|
|
|
}
|
2023-10-18 09:02:24 +00:00
|
|
|
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
|
|
|
}
|
2023-10-23 09:53:24 +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 {
|
2022-08-29 07:26:36 +00:00
|
|
|
fo := files.NewFileOp()
|
2023-11-02 10:38:50 +00:00
|
|
|
return fo.ChmodR(op.Path, op.Mode, op.Sub)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileService) BatchChangeModeAndOwner(op request.FileRoleReq) error {
|
|
|
|
fo := files.NewFileOp()
|
|
|
|
for _, path := range op.Paths {
|
|
|
|
if !fo.Stat(path) {
|
|
|
|
return buserr.New(constant.ErrPathNotFound)
|
|
|
|
}
|
|
|
|
if err := fo.ChownR(path, op.User, op.Group, op.Sub); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := fo.ChmodR(path, op.Mode, op.Sub); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-27 05:54:15 +00:00
|
|
|
}
|
2023-11-02 10:38:50 +00:00
|
|
|
return nil
|
|
|
|
|
2022-08-29 07:26:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 14:18:16 +00:00
|
|
|
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)) {
|
2024-07-13 13:58:02 +00:00
|
|
|
return buserr.New(constant.ErrFileIsExist)
|
2022-08-30 09:59:59 +00:00
|
|
|
}
|
2024-06-04 08:48:31 +00:00
|
|
|
return fo.Compress(c.Files, c.Dst, c.Name, files.CompressType(c.Type), c.Secret)
|
2022-08-30 09:59:59 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2024-06-27 04:14:52 +00:00
|
|
|
if c.Type == "tar" && len(c.Secret) != 0 {
|
|
|
|
c.Type = "tar.gz"
|
|
|
|
}
|
2024-06-04 08:48:31 +00:00
|
|
|
return fo.Decompress(c.Path, c.Dst, files.CompressType(c.Type), c.Secret)
|
2022-08-31 05:59:02 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 07:27:25 +00:00
|
|
|
func (f *FileService) GetContent(op request.FileContentReq) (response.FileInfo, error) {
|
|
|
|
info, err := files.NewFileInfo(files.FileOption{
|
2024-07-05 10:13:08 +00:00
|
|
|
Path: op.Path,
|
|
|
|
Expand: true,
|
|
|
|
IsDetail: op.IsDetail,
|
2023-10-27 07:27:25 +00:00
|
|
|
})
|
2024-05-29 10:43:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return response.FileInfo{}, err
|
|
|
|
}
|
2024-05-22 10:32:47 +00:00
|
|
|
|
|
|
|
content := []byte(info.Content)
|
|
|
|
if len(content) > 1024 {
|
2024-06-12 06:32:37 +00:00
|
|
|
content = content[:1024]
|
2024-05-22 10:32:47 +00:00
|
|
|
}
|
2024-06-12 06:32:37 +00:00
|
|
|
if !utf8.Valid(content) {
|
|
|
|
_, decodeName, _ := charset.DetermineEncoding(content, "")
|
|
|
|
if decodeName == "windows-1252" {
|
|
|
|
reader := strings.NewReader(info.Content)
|
|
|
|
item := transform.NewReader(reader, simplifiedchinese.GBK.NewDecoder())
|
|
|
|
contents, err := io.ReadAll(item)
|
|
|
|
if err != nil {
|
|
|
|
return response.FileInfo{}, err
|
|
|
|
}
|
|
|
|
info.Content = string(contents)
|
2024-05-22 10:32:47 +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 {
|
2024-04-11 10:32:11 +00:00
|
|
|
if files.IsInvalidChar(req.NewName) {
|
|
|
|
return buserr.New("ErrInvalidChar")
|
|
|
|
}
|
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()
|
2023-12-14 06:42:11 +00:00
|
|
|
return key, fo.DownloadFileWithProcess(w.Url, filepath.Join(w.Path, w.Name), key, w.IgnoreCertificate)
|
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-12-05 08:00:10 +00:00
|
|
|
for _, oldPath := range m.OldPaths {
|
|
|
|
if !fo.Stat(oldPath) {
|
|
|
|
return buserr.WithName(constant.ErrFileNotFound, oldPath)
|
|
|
|
}
|
|
|
|
if oldPath == m.NewPath || strings.Contains(m.NewPath, filepath.Clean(oldPath)+"/") {
|
2023-03-10 03:44:58 +00:00
|
|
|
return buserr.New(constant.ErrMovePathFailed)
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 09:48:49 +00:00
|
|
|
if m.Type == "cut" {
|
2023-11-01 10:27:19 +00:00
|
|
|
return fo.Cut(m.OldPaths, m.NewPath, m.Name, m.Cover)
|
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 {
|
2023-11-01 10:27:19 +00:00
|
|
|
if err := fo.CopyAndReName(src, m.NewPath, m.Name, m.Cover); 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) {
|
2023-03-06 10:12:31 +00:00
|
|
|
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()
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := fo.Compress(d.Paths, tempPath, d.Name, files.CompressType(d.Type), ""); err != nil {
|
2023-03-06 10:12:31 +00:00
|
|
|
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) {
|
2024-05-06 05:51:55 +00:00
|
|
|
var (
|
|
|
|
res response.DirSizeRes
|
|
|
|
)
|
|
|
|
if req.Path == "/proc" {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
cmd := exec.Command("du", "-s", req.Path)
|
|
|
|
output, err := cmd.Output()
|
|
|
|
if err == nil {
|
|
|
|
fields := strings.Fields(string(output))
|
|
|
|
if len(fields) == 2 {
|
|
|
|
var cmdSize int64
|
|
|
|
_, err = fmt.Sscanf(fields[0], "%d", &cmdSize)
|
|
|
|
if err == nil {
|
|
|
|
res.Size = float64(cmdSize * 1024)
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 10:10:41 +00:00
|
|
|
fo := files.NewFileOp()
|
|
|
|
size, err := fo.GetDirSize(req.Path)
|
|
|
|
if err != nil {
|
2024-05-06 05:51:55 +00:00
|
|
|
return res, err
|
2022-09-09 10:10:41 +00:00
|
|
|
}
|
2024-05-06 05:51:55 +00:00
|
|
|
res.Size = size
|
|
|
|
return res, nil
|
2022-09-09 10:10:41 +00:00
|
|
|
}
|
2023-11-23 03:00:08 +00:00
|
|
|
|
|
|
|
func (f *FileService) ReadLogByLine(req request.FileReadByLineReq) (*response.FileLineContent, error) {
|
|
|
|
logFilePath := ""
|
|
|
|
switch req.Type {
|
|
|
|
case constant.TypeWebsite:
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nginx, err := getNginxFull(&website)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sitePath := path.Join(nginx.SiteDir, "sites", website.Alias)
|
|
|
|
logFilePath = path.Join(sitePath, "log", req.Name)
|
|
|
|
case constant.TypePhp:
|
|
|
|
php, err := runtimeRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logFilePath = php.GetLogPath()
|
|
|
|
case constant.TypeSSL:
|
|
|
|
ssl, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logFilePath = ssl.GetLogPath()
|
|
|
|
case constant.TypeSystem:
|
|
|
|
fileName := ""
|
2023-12-04 09:20:10 +00:00
|
|
|
if len(req.Name) == 0 || req.Name == time.Now().Format("2006-01-02") {
|
2023-11-23 03:00:08 +00:00
|
|
|
fileName = "1Panel.log"
|
|
|
|
} else {
|
|
|
|
fileName = "1Panel-" + req.Name + ".log"
|
|
|
|
}
|
|
|
|
logFilePath = path.Join(global.CONF.System.DataDir, "log", fileName)
|
|
|
|
if _, err := os.Stat(logFilePath); err != nil {
|
|
|
|
fileGzPath := path.Join(global.CONF.System.DataDir, "log", fileName+".gz")
|
|
|
|
if _, err := os.Stat(fileGzPath); err != nil {
|
|
|
|
return nil, buserr.New("ErrHttpReqNotFound")
|
|
|
|
}
|
|
|
|
if err := handleGunzip(fileGzPath); err != nil {
|
|
|
|
return nil, fmt.Errorf("handle ungzip file %s failed, err: %v", fileGzPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "image-pull", "image-push", "image-build", "compose-create":
|
|
|
|
logFilePath = path.Join(global.CONF.System.TmpDir, fmt.Sprintf("docker_logs/%s", req.Name))
|
|
|
|
}
|
|
|
|
|
2024-06-17 14:24:50 +00:00
|
|
|
lines, isEndOfFile, total, err := files.ReadFileByLine(logFilePath, req.Page, req.PageSize, req.Latest)
|
2023-11-23 03:00:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-19 10:04:43 +00:00
|
|
|
if req.Latest && req.Page == 1 && len(lines) < 1000 && total > 1 {
|
|
|
|
preLines, _, _, err := files.ReadFileByLine(logFilePath, total-1, req.PageSize, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lines = append(preLines, lines...)
|
|
|
|
}
|
|
|
|
|
2023-11-23 03:00:08 +00:00
|
|
|
res := &response.FileLineContent{
|
|
|
|
Content: strings.Join(lines, "\n"),
|
|
|
|
End: isEndOfFile,
|
|
|
|
Path: logFilePath,
|
2024-06-17 14:24:50 +00:00
|
|
|
Total: total,
|
2024-08-19 10:04:43 +00:00
|
|
|
Lines: lines,
|
2023-11-23 03:00:08 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|