fix: 新增发送文件到网元服务器

This commit is contained in:
TsMask
2023-11-28 09:47:21 +08:00
parent 05d94572f4
commit 1380bf8698
6 changed files with 88 additions and 11 deletions

View File

@@ -0,0 +1,68 @@
package controller
import (
"fmt"
"path/filepath"
"strings"
"ems.agt/src/framework/config"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/file"
"ems.agt/src/framework/utils/ssh"
"ems.agt/src/framework/vo/result"
neService "ems.agt/src/modules/network_element/service"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// 实例化控制层 NeActionController 结构体
var NewNeAction = &NeActionController{
neInfoService: neService.NewNeInfoImpl,
}
// 网元处理请求
//
// PATH /
type NeActionController struct {
// 网元信息服务
neInfoService neService.INeInfo
}
// 发送文件到网元端
//
// POST /pushFile
func (s *NeActionController) PushFile(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
NeType string `json:"neType" binding:"required"`
NeID string `json:"neId" binding:"required"`
UploadPath string `json:"uploadPath" binding:"required"`
}
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeID)
if neInfo.NeId != body.NeID || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 本地文件
localPath := file.ParseUploadFilePath(body.UploadPath)
nePath := config.Get("mml.upload").(string)
// 复制到远程
err := ssh.FileSCPLocalToNe(neInfo.IP, localPath, nePath)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
// 网元端文件路径
fileName := localPath[strings.LastIndex(localPath, "/")+1:]
neFilePath := fmt.Sprintf("%s/%s", nePath, fileName)
c.JSON(200, result.OkData(filepath.ToSlash(neFilePath)))
}