Files
mayfly-go/server/internal/machine/application/machine_file.go

340 lines
8.9 KiB
Go
Raw Normal View History

package application
import (
"context"
"errors"
"fmt"
2021-05-08 20:50:34 +08:00
"io"
"io/fs"
2022-09-09 18:26:08 +08:00
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
2023-10-30 17:34:56 +08:00
"mayfly-go/internal/machine/mcm"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/model"
2021-05-08 20:50:34 +08:00
"os"
"strings"
"github.com/pkg/sftp"
)
type MachineFile interface {
// 分页获取机器文件信息列表
GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
// 根据条件获取
GetMachineFile(condition *entity.MachineFile, cols ...string) error
// 根据id获取
GetById(id uint64, cols ...string) *entity.MachineFile
Save(ctx context.Context, entity *entity.MachineFile) error
Delete(ctx context.Context, id uint64) error
2021-05-08 20:50:34 +08:00
2022-11-18 17:52:30 +08:00
// 获取文件关联的机器信息,主要用于记录日志使用
2023-10-30 17:34:56 +08:00
// GetMachine(fileId uint64) *mcm.Info
// 检查文件路径并返回机器id
2023-10-30 17:34:56 +08:00
GetMachineCli(fileId uint64, path ...string) (*mcm.Cli, error)
2022-11-18 17:52:30 +08:00
2021-05-08 20:50:34 +08:00
/** sftp 相关操作 **/
// 创建目录
2023-10-30 17:34:56 +08:00
MkDir(fid uint64, path string) (*mcm.MachineInfo, error)
// 创建文件
2023-10-30 17:34:56 +08:00
CreateFile(fid uint64, path string) (*mcm.MachineInfo, error)
2021-05-08 20:50:34 +08:00
// 读取目录
ReadDir(fid uint64, path string) ([]fs.FileInfo, error)
2021-05-08 20:50:34 +08:00
2023-07-05 00:26:00 +08:00
// 获取指定目录内容大小
GetDirSize(fid uint64, path string) (string, error)
2023-07-05 00:26:00 +08:00
// 获取文件stat
FileStat(fid uint64, path string) (string, error)
2023-07-05 00:26:00 +08:00
2021-05-08 20:50:34 +08:00
// 读取文件内容
2023-10-30 17:34:56 +08:00
ReadFile(fileId uint64, path string) (*sftp.File, *mcm.MachineInfo, error)
2021-05-08 20:50:34 +08:00
// 写文件
2023-10-30 17:34:56 +08:00
WriteFileContent(fileId uint64, path string, content []byte) (*mcm.MachineInfo, error)
2021-05-08 20:50:34 +08:00
// 文件上传
2023-10-30 17:34:56 +08:00
UploadFile(fileId uint64, path, filename string, reader io.Reader) (*mcm.MachineInfo, error)
2021-05-08 20:50:34 +08:00
// 移除文件
2023-10-30 17:34:56 +08:00
RemoveFile(fileId uint64, path ...string) (*mcm.MachineInfo, error)
2023-10-30 17:34:56 +08:00
Copy(fileId uint64, toPath string, paths ...string) (*mcm.MachineInfo, error)
2023-10-30 17:34:56 +08:00
Mv(fileId uint64, toPath string, paths ...string) (*mcm.MachineInfo, error)
2023-10-30 17:34:56 +08:00
Rename(fileId uint64, oldname string, newname string) (*mcm.MachineInfo, error)
}
func newMachineFileApp(machineFileRepo repository.MachineFile, machineApp Machine) MachineFile {
return &machineFileAppImpl{machineApp: machineApp, machineFileRepo: machineFileRepo}
2022-09-09 18:26:08 +08:00
}
type machineFileAppImpl struct {
machineFileRepo repository.MachineFile
machineApp Machine
}
// 分页获取机器脚本信息列表
func (m *machineFileAppImpl) GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return m.machineFileRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
// 根据条件获取
func (m *machineFileAppImpl) GetMachineFile(condition *entity.MachineFile, cols ...string) error {
return m.machineFileRepo.GetBy(condition, cols...)
}
// 根据id获取
func (m *machineFileAppImpl) GetById(id uint64, cols ...string) *entity.MachineFile {
mf := new(entity.MachineFile)
if err := m.machineFileRepo.GetById(mf, id, cols...); err == nil {
return mf
}
return nil
}
2021-05-08 20:50:34 +08:00
// 保存机器文件配置
func (m *machineFileAppImpl) Save(ctx context.Context, mf *entity.MachineFile) error {
_, err := m.machineApp.GetById(new(entity.Machine), mf.MachineId, "Name")
if err != nil {
return errorx.NewBiz("该机器不存在")
}
if mf.Id != 0 {
return m.machineFileRepo.UpdateById(ctx, mf)
}
return m.machineFileRepo.Insert(ctx, mf)
}
func (m *machineFileAppImpl) Delete(ctx context.Context, id uint64) error {
return m.machineFileRepo.DeleteById(ctx, id)
}
2021-05-08 20:50:34 +08:00
func (m *machineFileAppImpl) ReadDir(fid uint64, path string) ([]fs.FileInfo, error) {
2021-05-08 20:50:34 +08:00
if !strings.HasSuffix(path, "/") {
path = path + "/"
}
_, sftpCli, err := m.GetMachineSftpCli(fid, path)
if err != nil {
return nil, err
}
return sftpCli.ReadDir(path)
2021-05-08 20:50:34 +08:00
}
func (m *machineFileAppImpl) GetDirSize(fid uint64, path string) (string, error) {
mcli, err := m.GetMachineCli(fid, path)
if err != nil {
return "", err
}
res, err := mcli.Run(fmt.Sprintf("du -sh %s", path))
2023-07-05 00:26:00 +08:00
if err != nil {
// 若存在目录为空,则可能会返回如下内容。最后一行即为真正目录内容所占磁盘空间大小
//du: cannot access /proc/19087/fd/3: No such file or directory\n
//du: cannot access /proc/19087/fdinfo/3: No such file or directory\n
//18G /\n
if res == "" {
return "", errorx.NewBiz("获取目录大小失败: %s", err.Error())
2023-07-05 00:26:00 +08:00
}
strs := strings.Split(res, "\n")
res = strs[len(strs)-2]
if !strings.Contains(res, "\t") {
return "", errorx.NewBiz(res)
2023-07-05 00:26:00 +08:00
}
}
// 返回 32K\t/tmp\n
return strings.Split(res, "\t")[0], nil
2023-07-05 00:26:00 +08:00
}
func (m *machineFileAppImpl) FileStat(fid uint64, path string) (string, error) {
mcli, err := m.GetMachineCli(fid, path)
if err != nil {
return "", err
}
return mcli.Run(fmt.Sprintf("stat -L %s", path))
2023-07-05 00:26:00 +08:00
}
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) MkDir(fid uint64, path string) (*mcm.MachineInfo, error) {
if !strings.HasSuffix(path, "/") {
path = path + "/"
}
mi, sftpCli, err := m.GetMachineSftpCli(fid, path)
if err != nil {
return nil, err
}
sftpCli.MkdirAll(path)
return mi, err
}
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) CreateFile(fid uint64, path string) (*mcm.MachineInfo, error) {
mi, sftpCli, err := m.GetMachineSftpCli(fid, path)
if err != nil {
return nil, err
}
file, err := sftpCli.Create(path)
if err != nil {
return nil, errorx.NewBiz("创建文件失败: %s", err.Error())
}
defer file.Close()
return mi, err
}
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) ReadFile(fileId uint64, path string) (*sftp.File, *mcm.MachineInfo, error) {
mi, sftpCli, err := m.GetMachineSftpCli(fileId, path)
if err != nil {
return nil, nil, err
}
2021-05-08 20:50:34 +08:00
// 读取文件内容
fc, err := sftpCli.Open(path)
return fc, mi, err
2021-05-08 20:50:34 +08:00
}
// 写文件内容
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) WriteFileContent(fileId uint64, path string, content []byte) (*mcm.MachineInfo, error) {
mi, sftpCli, err := m.GetMachineSftpCli(fileId, path)
if err != nil {
return nil, err
}
f, err := sftpCli.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE|os.O_RDWR)
if err != nil {
return mi, err
}
2021-05-08 20:50:34 +08:00
defer f.Close()
f.Write(content)
return mi, err
2021-05-08 20:50:34 +08:00
}
// 上传文件
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) UploadFile(fileId uint64, path, filename string, reader io.Reader) (*mcm.MachineInfo, error) {
2021-05-08 20:50:34 +08:00
if !strings.HasSuffix(path, "/") {
path = path + "/"
}
mi, sftpCli, err := m.GetMachineSftpCli(fileId, path)
if err != nil {
return nil, err
}
createfile, err := sftpCli.Create(path + filename)
if err != nil {
return mi, err
}
2021-05-08 20:50:34 +08:00
defer createfile.Close()
io.Copy(createfile, reader)
return mi, err
2021-05-08 20:50:34 +08:00
}
// 删除文件
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) RemoveFile(fileId uint64, path ...string) (*mcm.MachineInfo, error) {
mcli, err := m.GetMachineCli(fileId, path...)
if err != nil {
return nil, err
}
2023-10-30 17:34:56 +08:00
minfo := mcli.Info
// 优先使用命令删除速度快sftp需要递归遍历删除子文件等
res, err := mcli.Run(fmt.Sprintf("rm -rf %s", strings.Join(path, " ")))
if err == nil {
return minfo, nil
}
logx.Errorf("使用命令rm删除文件失败: %s", res)
2021-05-08 20:50:34 +08:00
sftpCli, err := mcli.GetSftpCli()
if err != nil {
return minfo, err
}
for _, p := range path {
err = sftpCli.RemoveAll(p)
if err != nil {
break
}
2021-05-08 20:50:34 +08:00
}
return minfo, err
}
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) Copy(fileId uint64, toPath string, paths ...string) (*mcm.MachineInfo, error) {
mcli, err := m.GetMachineCli(fileId, paths...)
if err != nil {
return nil, err
}
2023-10-30 17:34:56 +08:00
mi := mcli.Info
res, err := mcli.Run(fmt.Sprintf("cp -r %s %s", strings.Join(paths, " "), toPath))
if err != nil {
return mi, errors.New(res)
}
return mi, err
}
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) Mv(fileId uint64, toPath string, paths ...string) (*mcm.MachineInfo, error) {
mcli, err := m.GetMachineCli(fileId, paths...)
if err != nil {
return nil, err
}
2023-10-30 17:34:56 +08:00
mi := mcli.Info
res, err := mcli.Run(fmt.Sprintf("mv %s %s", strings.Join(paths, " "), toPath))
if err != nil {
return mi, errorx.NewBiz(res)
}
return mi, err
2021-05-08 20:50:34 +08:00
}
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) Rename(fileId uint64, oldname string, newname string) (*mcm.MachineInfo, error) {
mi, sftpCli, err := m.GetMachineSftpCli(fileId, newname)
if err != nil {
return nil, err
}
return mi, sftpCli.Rename(oldname, newname)
2022-11-18 17:52:30 +08:00
}
// 获取文件机器cli
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) GetMachineCli(fid uint64, inputPath ...string) (*mcm.Cli, error) {
2022-11-18 17:52:30 +08:00
mf := m.GetById(fid)
if mf == nil {
return nil, errorx.NewBiz("文件不存在")
}
for _, path := range inputPath {
2021-05-08 20:50:34 +08:00
// 接口传入的地址需为配置路径的子路径
if !strings.HasPrefix(path, mf.Path) {
return nil, errorx.NewBiz("无权访问该目录或文件: %s", path)
}
}
return m.machineApp.GetCli(mf.MachineId)
}
// 获取文件机器 sftp cli
2023-10-30 17:34:56 +08:00
func (m *machineFileAppImpl) GetMachineSftpCli(fid uint64, inputPath ...string) (*mcm.MachineInfo, *sftp.Client, error) {
mcli, err := m.GetMachineCli(fid, inputPath...)
if err != nil {
return nil, nil, err
2021-05-08 20:50:34 +08:00
}
sftpCli, err := mcli.GetSftpCli()
if err != nil {
return nil, nil, err
}
2023-10-30 17:34:56 +08:00
return mcli.Info, sftpCli, nil
2021-05-08 20:50:34 +08:00
}