feat: 网元文件操作可删除本地临时文件

This commit is contained in:
TsMask
2024-08-21 16:31:04 +08:00
parent e4ad2088a1
commit deba4ca564
2 changed files with 27 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ package controller
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
@@ -30,7 +31,7 @@ type NeActionController struct {
neInfoService neService.INeInfo
}
// 发送文件到网元
// 发送文件从本地到网元
//
// POST /pushFile
func (s *NeActionController) PushFile(c *gin.Context) {
@@ -39,6 +40,7 @@ func (s *NeActionController) PushFile(c *gin.Context) {
NeType string `json:"neType" binding:"required"`
NeID string `json:"neId" binding:"required"`
UploadPath string `json:"uploadPath" binding:"required"`
DelTemp bool `json:"delTemp"` // 删除本地临时文件
}
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
@@ -70,14 +72,19 @@ func (s *NeActionController) PushFile(c *gin.Context) {
// 本地文件
localFilePath := file.ParseUploadFilePath(body.UploadPath)
// 网元端临时目录
sshClient.RunCMD("mkdir -p /tmp/omc && sudo chmod 777 -R /tmp/omc")
neFilePath := filepath.Join("/tmp/omc/push", filepath.Base(localFilePath))
sshClient.RunCMD("mkdir -p /tmp/omc/push && sudo chmod 777 -R /tmp/omc")
neFilePath := filepath.ToSlash(filepath.Join("/tmp/omc/push", filepath.Base(localFilePath)))
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(localFilePath, neFilePath); err != nil {
c.JSON(200, result.ErrMsg(fmt.Sprintf("%s : please check if scp remote copy is allowed", neInfo.NeType)))
c.JSON(200, result.ErrMsg("Please check if the file exists or if scp is allowed to copy remotely"))
return
}
defer func() {
if body.DelTemp {
_ = os.Remove(localFilePath)
}
}()
c.JSON(200, result.OkData(filepath.ToSlash(neFilePath)))
}
@@ -91,6 +98,7 @@ func (s *NeActionController) PullFile(c *gin.Context) {
NeID string `form:"neId" binding:"required"`
Path string `form:"path" binding:"required"`
FileName string `form:"fileName" binding:"required"`
DelTemp bool `form:"delTemp"` // 删除本地临时文件
}
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
@@ -119,7 +127,7 @@ func (s *NeActionController) PullFile(c *gin.Context) {
}
defer sftpClient.Close()
nePath := filepath.Join(querys.Path, querys.FileName)
nePath := filepath.ToSlash(filepath.Join(querys.Path, querys.FileName))
fileName := generate.Code(6) + "_" + querys.FileName
localFilePath := filepath.Join("/tmp/omc/pull", fileName)
if runtime.GOOS == "windows" {
@@ -130,6 +138,12 @@ func (s *NeActionController) PullFile(c *gin.Context) {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer func() {
if querys.DelTemp {
_ = os.Remove(localFilePath)
}
}()
c.FileAttachment(localFilePath, fileName)
}

View File

@@ -1,6 +1,7 @@
package controller
import (
"os"
"path/filepath"
"be.ems/src/framework/i18n"
@@ -85,6 +86,7 @@ func (s *TcpdumpController) DumpDownload(c *gin.Context) {
NeType string `form:"neType" binding:"required"` // 网元类型
NeID string `form:"neId" binding:"required"` // 网元ID
TaskCode string `form:"taskCode" binding:"required"` // 任务码,停止任务并查看日志信息
DelTemp bool `form:"delTemp"` // 完成后是否删除本地临时zip文件
}
if err := c.ShouldBindQuery(&query); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
@@ -96,6 +98,12 @@ func (s *TcpdumpController) DumpDownload(c *gin.Context) {
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
return
}
defer func() {
if query.DelTemp {
_ = os.Remove(zipFilePath)
}
}()
c.FileAttachment(zipFilePath, filepath.Base(zipFilePath))
}