mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
refactor: 机器文件操作优化
This commit is contained in:
@@ -76,7 +76,6 @@ func (m *MachineFile) CreateFile(rc *req.Ctx) {
|
||||
m.MachineFileApp.CreateFile(fid, form.Path)
|
||||
rc.ReqParam = fmt.Sprintf("%s -> 创建文件: %s", mi.GetLogDesc(), path)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (m *MachineFile) ReadFileContent(rc *req.Ctx) {
|
||||
@@ -121,12 +120,10 @@ func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
|
||||
fisVO := make([]vo.MachineFileInfo, 0)
|
||||
for _, fi := range fis {
|
||||
fisVO = append(fisVO, vo.MachineFileInfo{
|
||||
Name: fi.Name(),
|
||||
Size: fi.Size(),
|
||||
Path: readPath + fi.Name(),
|
||||
Type: getFileType(fi.Mode()),
|
||||
Mode: fi.Mode().String(),
|
||||
ModTime: fi.ModTime().Format("2006-01-02 15:04:05"),
|
||||
Name: fi.Name(),
|
||||
Size: fi.Size(),
|
||||
Path: readPath + fi.Name(),
|
||||
Type: getFileType(fi.Mode()),
|
||||
})
|
||||
}
|
||||
sort.Sort(vo.MachineFileInfos(fisVO))
|
||||
@@ -134,6 +131,22 @@ func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
|
||||
rc.ReqParam = fmt.Sprintf("path: %s", readPath)
|
||||
}
|
||||
|
||||
func (m *MachineFile) GetDirSize(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
readPath := g.Query("path")
|
||||
|
||||
rc.ResData = m.MachineFileApp.GetDirSize(fid, readPath)
|
||||
}
|
||||
|
||||
func (m *MachineFile) GetFileStat(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
readPath := g.Query("path")
|
||||
|
||||
rc.ResData = m.MachineFileApp.FileStat(fid, readPath)
|
||||
}
|
||||
|
||||
func (m *MachineFile) WriteFileContent(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
|
||||
@@ -70,7 +70,9 @@ func (m *MachineScript) RunMachineScript(rc *req.Ctx) {
|
||||
res, err := cli.Run(script)
|
||||
// 记录请求参数
|
||||
rc.ReqParam = fmt.Sprintf("[machine: %s, scriptId: %d, name: %s]", cli.GetMachine().GetLogDesc(), scriptId, ms.Name)
|
||||
biz.ErrIsNilAppendErr(err, "执行命令失败:%s")
|
||||
if res == "" {
|
||||
biz.ErrIsNilAppendErr(err, "执行命令失败:%s")
|
||||
}
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
|
||||
@@ -52,12 +52,10 @@ type MachineFileVO struct {
|
||||
}
|
||||
|
||||
type MachineFileInfo struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Size int64 `json:"size"`
|
||||
Type string `json:"type"`
|
||||
Mode string `json:"mode"`
|
||||
ModTime string `json:"modTime"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Size int64 `json:"size"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type MachineFileInfos []MachineFileInfo
|
||||
|
||||
@@ -44,6 +44,12 @@ type MachineFile interface {
|
||||
// 读取目录
|
||||
ReadDir(fid uint64, path string) []fs.FileInfo
|
||||
|
||||
// 获取指定目录内容大小
|
||||
GetDirSize(fid uint64, path string) string
|
||||
|
||||
// 获取文件stat
|
||||
FileStat(fid uint64, path string) string
|
||||
|
||||
// 读取文件内容
|
||||
ReadFile(fileId uint64, path string) *sftp.File
|
||||
|
||||
@@ -110,6 +116,35 @@ func (m *machineFileAppImpl) ReadDir(fid uint64, path string) []fs.FileInfo {
|
||||
return fis
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) GetDirSize(fid uint64, path string) string {
|
||||
_, machineId := m.checkAndReturnPathMid(fid, path)
|
||||
res, err := GetMachineApp().GetCli(machineId).Run(fmt.Sprintf("du -sh %s", path))
|
||||
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 == "" {
|
||||
panic(biz.NewBizErr(fmt.Sprintf("获取目录大小失败: %s", err.Error())))
|
||||
}
|
||||
strs := strings.Split(res, "\n")
|
||||
res = strs[len(strs)-2]
|
||||
|
||||
if !strings.Contains(res, "\t") {
|
||||
panic(biz.NewBizErr(res))
|
||||
}
|
||||
}
|
||||
// 返回 32K\t/tmp\n
|
||||
return strings.Split(res, "\t")[0]
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) FileStat(fid uint64, path string) string {
|
||||
_, machineId := m.checkAndReturnPathMid(fid, path)
|
||||
res, err := GetMachineApp().GetCli(machineId).Run(fmt.Sprintf("stat -L %s", path))
|
||||
biz.ErrIsNil(err, res)
|
||||
return res
|
||||
}
|
||||
|
||||
func (m *machineFileAppImpl) MkDir(fid uint64, path string) {
|
||||
path, machineId := m.checkAndReturnPathMid(fid, path)
|
||||
if !strings.HasSuffix(path, "/") {
|
||||
|
||||
@@ -119,6 +119,7 @@ func (c *Cli) GetSession() (*ssh.Session, error) {
|
||||
|
||||
// 执行shell
|
||||
// @param shell shell脚本命令
|
||||
// @return 返回执行成功或错误的消息
|
||||
func (c *Cli) Run(shell string) (string, error) {
|
||||
session, err := c.GetSession()
|
||||
if err != nil {
|
||||
@@ -128,7 +129,7 @@ func (c *Cli) Run(shell string) (string, error) {
|
||||
defer session.Close()
|
||||
buf, err := session.CombinedOutput(shell)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return string(buf), err
|
||||
}
|
||||
return string(buf), nil
|
||||
}
|
||||
|
||||
@@ -52,6 +52,14 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
|
||||
Handle(mf.GetDirEntry)
|
||||
})
|
||||
|
||||
machineFile.GET(":machineId/files/:fileId/dir-size", func(c *gin.Context) {
|
||||
req.NewCtxWithGin(c).Handle(mf.GetDirSize)
|
||||
})
|
||||
|
||||
machineFile.GET(":machineId/files/:fileId/file-stat", func(c *gin.Context) {
|
||||
req.NewCtxWithGin(c).Handle(mf.GetFileStat)
|
||||
})
|
||||
|
||||
writeFile := req.NewLogInfo("机器-修改文件内容").WithSave(true)
|
||||
wfP := req.NewPermission("machine:file:write")
|
||||
machineFile.POST(":machineId/files/:fileId/write", func(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user