mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-12-01 21:40:25 +08:00
feat: 机器文件优化&sql新增引号包裹
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
@@ -31,15 +32,18 @@ type MachineFile interface {
|
||||
Delete(id uint64)
|
||||
|
||||
// 获取文件关联的机器信息,主要用于记录日志使用
|
||||
GetMachine(fileId uint64) *machine.Info
|
||||
// GetMachine(fileId uint64) *machine.Info
|
||||
|
||||
// 检查文件路径,并返回机器id
|
||||
GetMachineCli(fileId uint64, path ...string) *machine.Cli
|
||||
|
||||
/** sftp 相关操作 **/
|
||||
|
||||
// 创建目录
|
||||
MkDir(fid uint64, path string)
|
||||
MkDir(fid uint64, path string) (*machine.Info, error)
|
||||
|
||||
// 创建文件
|
||||
CreateFile(fid uint64, path string)
|
||||
CreateFile(fid uint64, path string) (*machine.Info, error)
|
||||
|
||||
// 读取目录
|
||||
ReadDir(fid uint64, path string) []fs.FileInfo
|
||||
@@ -51,22 +55,22 @@ type MachineFile interface {
|
||||
FileStat(fid uint64, path string) string
|
||||
|
||||
// 读取文件内容
|
||||
ReadFile(fileId uint64, path string) *sftp.File
|
||||
ReadFile(fileId uint64, path string) (*sftp.File, *machine.Info, error)
|
||||
|
||||
// 写文件
|
||||
WriteFileContent(fileId uint64, path string, content []byte)
|
||||
WriteFileContent(fileId uint64, path string, content []byte) (*machine.Info, error)
|
||||
|
||||
// 文件上传
|
||||
UploadFile(fileId uint64, path, filename string, reader io.Reader)
|
||||
UploadFile(fileId uint64, path, filename string, reader io.Reader) (*machine.Info, error)
|
||||
|
||||
// 移除文件
|
||||
RemoveFile(fileId uint64, path ...string)
|
||||
RemoveFile(fileId uint64, path ...string) (*machine.Info, error)
|
||||
|
||||
Copy(fileId uint64, toPath string, paths ...string) *machine.Info
|
||||
Copy(fileId uint64, toPath string, paths ...string) (*machine.Info, error)
|
||||
|
||||
Mv(fileId uint64, toPath string, paths ...string) *machine.Info
|
||||
Mv(fileId uint64, toPath string, paths ...string) (*machine.Info, error)
|
||||
|
||||
Rename(fileId uint64, oldname string, newname string) error
|
||||
Rename(fileId uint64, oldname string, newname string) (*machine.Info, error)
|
||||
}
|
||||
|
||||
func newMachineFileApp(machineFileRepo repository.MachineFile, machineRepo repository.Machine) MachineFile {
|
||||
@@ -111,20 +115,19 @@ func (m *machineFileAppImpl) Delete(id uint64) {
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) ReadDir(fid uint64, path string) []fs.FileInfo {
|
||||
machineId := m.checkAndReturnMid(fid, path)
|
||||
mcli := m.GetMachineCli(fid, path)
|
||||
if !strings.HasSuffix(path, "/") {
|
||||
path = path + "/"
|
||||
}
|
||||
|
||||
sftpCli := m.getSftpCli(machineId)
|
||||
fis, err := sftpCli.ReadDir(path)
|
||||
fis, err := mcli.GetSftpCli().ReadDir(path)
|
||||
biz.ErrIsNilAppendErr(err, "读取目录失败: %s")
|
||||
return fis
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) GetDirSize(fid uint64, path string) string {
|
||||
machineId := m.checkAndReturnMid(fid, path)
|
||||
res, err := GetMachineApp().GetCli(machineId).Run(fmt.Sprintf("du -sh %s", path))
|
||||
mcli := m.GetMachineCli(fid, path)
|
||||
res, err := mcli.Run(fmt.Sprintf("du -sh %s", path))
|
||||
if err != nil {
|
||||
// 若存在目录为空,则可能会返回如下内容。最后一行即为真正目录内容所占磁盘空间大小
|
||||
//du: cannot access ‘/proc/19087/fd/3’: No such file or directory\n
|
||||
@@ -145,121 +148,116 @@ func (m *machineFileAppImpl) GetDirSize(fid uint64, path string) string {
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) FileStat(fid uint64, path string) string {
|
||||
machineId := m.checkAndReturnMid(fid, path)
|
||||
res, err := GetMachineApp().GetCli(machineId).Run(fmt.Sprintf("stat -L %s", path))
|
||||
mcli := m.GetMachineCli(fid, path)
|
||||
res, err := mcli.Run(fmt.Sprintf("stat -L %s", path))
|
||||
biz.ErrIsNil(err, res)
|
||||
return res
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) MkDir(fid uint64, path string) {
|
||||
machineId := m.checkAndReturnMid(fid, path)
|
||||
func (m *machineFileAppImpl) MkDir(fid uint64, path string) (*machine.Info, error) {
|
||||
mcli := m.GetMachineCli(fid, path)
|
||||
if !strings.HasSuffix(path, "/") {
|
||||
path = path + "/"
|
||||
}
|
||||
|
||||
sftpCli := m.getSftpCli(machineId)
|
||||
err := sftpCli.MkdirAll(path)
|
||||
biz.ErrIsNilAppendErr(err, "创建目录失败: %s")
|
||||
err := mcli.GetSftpCli().MkdirAll(path)
|
||||
return mcli.GetMachine(), err
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) CreateFile(fid uint64, path string) {
|
||||
machineId := m.checkAndReturnMid(fid, path)
|
||||
sftpCli := m.getSftpCli(machineId)
|
||||
file, err := sftpCli.Create(path)
|
||||
func (m *machineFileAppImpl) CreateFile(fid uint64, path string) (*machine.Info, error) {
|
||||
mcli := m.GetMachineCli(fid, path)
|
||||
file, err := mcli.GetSftpCli().Create(path)
|
||||
biz.ErrIsNilAppendErr(err, "创建文件失败: %s")
|
||||
defer file.Close()
|
||||
return mcli.GetMachine(), err
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) ReadFile(fileId uint64, path string) *sftp.File {
|
||||
machineId := m.checkAndReturnMid(fileId, path)
|
||||
sftpCli := m.getSftpCli(machineId)
|
||||
func (m *machineFileAppImpl) ReadFile(fileId uint64, path string) (*sftp.File, *machine.Info, error) {
|
||||
mcli := m.GetMachineCli(fileId, path)
|
||||
// 读取文件内容
|
||||
fc, err := sftpCli.Open(path)
|
||||
biz.ErrIsNilAppendErr(err, "打开文件失败: %s")
|
||||
return fc
|
||||
fc, err := mcli.GetSftpCli().Open(path)
|
||||
return fc, mcli.GetMachine(), err
|
||||
}
|
||||
|
||||
// 写文件内容
|
||||
func (m *machineFileAppImpl) WriteFileContent(fileId uint64, path string, content []byte) {
|
||||
machineId := m.checkAndReturnMid(fileId, path)
|
||||
|
||||
sftpCli := m.getSftpCli(machineId)
|
||||
f, err := sftpCli.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE|os.O_RDWR)
|
||||
biz.ErrIsNilAppendErr(err, "打开文件失败: %s")
|
||||
func (m *machineFileAppImpl) WriteFileContent(fileId uint64, path string, content []byte) (*machine.Info, error) {
|
||||
mcli := m.GetMachineCli(fileId, path)
|
||||
mi := mcli.GetMachine()
|
||||
f, err := mcli.GetSftpCli().OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE|os.O_RDWR)
|
||||
if err != nil {
|
||||
return mi, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fi, _ := f.Stat()
|
||||
biz.IsTrue(!fi.IsDir(), "该路径不是文件")
|
||||
f.Write(content)
|
||||
return mi, err
|
||||
}
|
||||
|
||||
// 上传文件
|
||||
func (m *machineFileAppImpl) UploadFile(fileId uint64, path, filename string, reader io.Reader) {
|
||||
machineId := m.checkAndReturnMid(fileId, path)
|
||||
func (m *machineFileAppImpl) UploadFile(fileId uint64, path, filename string, reader io.Reader) (*machine.Info, error) {
|
||||
mcli := m.GetMachineCli(fileId, path)
|
||||
mi := mcli.GetMachine()
|
||||
if !strings.HasSuffix(path, "/") {
|
||||
path = path + "/"
|
||||
}
|
||||
|
||||
sftpCli := m.getSftpCli(machineId)
|
||||
createfile, err := sftpCli.Create(path + filename)
|
||||
biz.ErrIsNilAppendErr(err, "创建文件失败: %s")
|
||||
createfile, err := mcli.GetSftpCli().Create(path + filename)
|
||||
if err != nil {
|
||||
return mi, err
|
||||
}
|
||||
defer createfile.Close()
|
||||
|
||||
io.Copy(createfile, reader)
|
||||
return mi, err
|
||||
}
|
||||
|
||||
// 删除文件
|
||||
func (m *machineFileAppImpl) RemoveFile(fileId uint64, path ...string) {
|
||||
machineId := m.checkAndReturnMid(fileId, path...)
|
||||
func (m *machineFileAppImpl) RemoveFile(fileId uint64, path ...string) (*machine.Info, error) {
|
||||
mcli := m.GetMachineCli(fileId, path...)
|
||||
minfo := mcli.GetMachine()
|
||||
|
||||
// 优先使用命令删除(速度快),sftp需要递归遍历删除子文件等
|
||||
mcli := GetMachineApp().GetCli(machineId)
|
||||
res, err := mcli.Run(fmt.Sprintf("rm -rf %s", strings.Join(path, " ")))
|
||||
if err == nil {
|
||||
return
|
||||
return minfo, nil
|
||||
}
|
||||
logx.Errorf("使用命令rm删除文件失败: %s", res)
|
||||
|
||||
sftpCli := m.getSftpCli(machineId)
|
||||
sftpCli := mcli.GetSftpCli()
|
||||
for _, p := range path {
|
||||
err := sftpCli.RemoveAll(p)
|
||||
biz.ErrIsNilAppendErr(err, "删除文件失败: %s")
|
||||
err = sftpCli.RemoveAll(p)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
return minfo, err
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) Copy(fileId uint64, toPath string, paths ...string) *machine.Info {
|
||||
mid := m.checkAndReturnMid(fileId, paths...)
|
||||
mcli := GetMachineApp().GetCli(mid)
|
||||
func (m *machineFileAppImpl) Copy(fileId uint64, toPath string, paths ...string) (*machine.Info, error) {
|
||||
mcli := m.GetMachineCli(fileId, paths...)
|
||||
mi := mcli.GetMachine()
|
||||
res, err := mcli.Run(fmt.Sprintf("cp -r %s %s", strings.Join(paths, " "), toPath))
|
||||
biz.ErrIsNil(err, "文件拷贝失败: %s", res)
|
||||
return mcli.GetMachine()
|
||||
if err != nil {
|
||||
return mi, errors.New(res)
|
||||
}
|
||||
return mi, err
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) Mv(fileId uint64, toPath string, paths ...string) *machine.Info {
|
||||
mid := m.checkAndReturnMid(fileId, paths...)
|
||||
mcli := GetMachineApp().GetCli(mid)
|
||||
func (m *machineFileAppImpl) Mv(fileId uint64, toPath string, paths ...string) (*machine.Info, error) {
|
||||
mcli := m.GetMachineCli(fileId, paths...)
|
||||
mi := mcli.GetMachine()
|
||||
res, err := mcli.Run(fmt.Sprintf("mv %s %s", strings.Join(paths, " "), toPath))
|
||||
biz.ErrIsNil(err, "文件移动失败: %s", res)
|
||||
return mcli.GetMachine()
|
||||
if err != nil {
|
||||
return mi, errors.New(res)
|
||||
}
|
||||
return mi, err
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) Rename(fileId uint64, oldname string, newname string) error {
|
||||
mid := m.checkAndReturnMid(fileId, newname)
|
||||
sftpCli := m.getSftpCli(mid)
|
||||
return sftpCli.Rename(oldname, newname)
|
||||
func (m *machineFileAppImpl) Rename(fileId uint64, oldname string, newname string) (*machine.Info, error) {
|
||||
mcli := m.GetMachineCli(fileId, newname)
|
||||
return mcli.GetMachine(), mcli.GetSftpCli().Rename(oldname, newname)
|
||||
}
|
||||
|
||||
// 获取sftp client
|
||||
func (m *machineFileAppImpl) getSftpCli(machineId uint64) *sftp.Client {
|
||||
return GetMachineApp().GetCli(machineId).GetSftpCli()
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) GetMachine(fileId uint64) *machine.Info {
|
||||
return GetMachineApp().GetCli(m.GetById(fileId).MachineId).GetMachine()
|
||||
}
|
||||
|
||||
// 校验并返回实际可访问的文件path
|
||||
func (m *machineFileAppImpl) checkAndReturnMid(fid uint64, inputPath ...string) uint64 {
|
||||
// 获取文件机器cli
|
||||
func (m *machineFileAppImpl) GetMachineCli(fid uint64, inputPath ...string) *machine.Cli {
|
||||
biz.IsTrue(fid != 0, "文件id不能为空")
|
||||
mf := m.GetById(fid)
|
||||
biz.NotNil(mf, "文件不存在")
|
||||
@@ -267,5 +265,5 @@ func (m *machineFileAppImpl) checkAndReturnMid(fid uint64, inputPath ...string)
|
||||
// 接口传入的地址需为配置路径的子路径
|
||||
biz.IsTrue(strings.HasPrefix(path, mf.Path), "无权访问该目录或文件: %s", path)
|
||||
}
|
||||
return mf.MachineId
|
||||
return GetMachineApp().GetCli(mf.MachineId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user