From deba4ca564d98f904e3c0bb6bf088e75a6f516ed Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Wed, 21 Aug 2024 16:31:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BD=91=E5=85=83=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=8F=AF=E5=88=A0=E9=99=A4=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../network_element/controller/action.go | 24 +++++++++++++++---- src/modules/trace/controller/tcpdump.go | 8 +++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/modules/network_element/controller/action.go b/src/modules/network_element/controller/action.go index 1e34c5be..9c44ca9d 100644 --- a/src/modules/network_element/controller/action.go +++ b/src/modules/network_element/controller/action.go @@ -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) } diff --git a/src/modules/trace/controller/tcpdump.go b/src/modules/trace/controller/tcpdump.go index fcd119f7..94012723 100644 --- a/src/modules/trace/controller/tcpdump.go +++ b/src/modules/trace/controller/tcpdump.go @@ -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)) }