feat: 网元行为接口新增获取目录压缩为ZIP/文件内容查看cat

This commit is contained in:
TsMask
2024-10-15 11:47:25 +08:00
parent 065895e1d2
commit 0a24e50e9d
2 changed files with 129 additions and 13 deletions

View File

@@ -31,7 +31,7 @@ type NeActionController struct {
neInfoService *neService.NeInfo
}
// 发送文件从本地到网元
// 从本地到网元发送文件
//
// POST /pushFile
func (s *NeActionController) PushFile(c *gin.Context) {
@@ -88,7 +88,7 @@ func (s *NeActionController) PushFile(c *gin.Context) {
c.JSON(200, result.OkData(filepath.ToSlash(neFilePath)))
}
// 获取文件从网元到本地
// 从网元到本地获取文件
//
// GET /pullFile
func (s *NeActionController) PullFile(c *gin.Context) {
@@ -147,6 +147,116 @@ func (s *NeActionController) PullFile(c *gin.Context) {
c.FileAttachment(localFilePath, fileName)
}
// 从网元到本地获取目录压缩为ZIP
//
// GET /pullDirZip
func (s *NeActionController) PullDirZip(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var querys struct {
NeType string `form:"neType" binding:"required"`
NeID string `form:"neId" binding:"required"`
Path string `form:"path" 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")))
return
}
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer sftpClient.Close()
nePath := querys.Path
dirName := generate.Code(6)
localFilePath := filepath.Join("/tmp/omc/pull/", dirName)
if runtime.GOOS == "windows" {
localFilePath = fmt.Sprintf("C:%s", localFilePath)
}
// 复制到本地
localDirFilePath := filepath.Join(localFilePath, "zip")
if err = sftpClient.CopyDirRemoteToLocal(nePath, localDirFilePath); err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
// 压缩zip文件名
zipFileName := fmt.Sprintf("%s-%s-%s.zip", neInfo.NeType, neInfo.NeId, dirName)
zipFilePath := filepath.Join(localFilePath, zipFileName)
if err := file.CompressZipByDir(zipFilePath, localDirFilePath); err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer func() {
if querys.DelTemp {
_ = os.RemoveAll(localFilePath)
}
}()
c.FileAttachment(zipFilePath, zipFileName)
}
// 查看网元端文件内容
//
// GET /viewFile
func (s *NeActionController) ViewFile(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var querys struct {
NeType string `form:"neType" binding:"required"`
NeID string `form:"neId" binding:"required"`
Path string `form:"path" binding:"required"`
FileName string `form:"fileName" binding:"required"`
}
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元端文件
nePath := filepath.ToSlash(filepath.Join(querys.Path, querys.FileName))
// 网元端临时目录
output, err := sshClient.RunCMD(fmt.Sprintf("cat %s", nePath))
output = strings.TrimSpace(output)
if err != nil || strings.HasPrefix(output, "ls: ") {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "file view cat error")))
return
}
c.JSON(200, result.OkData(output))
}
// 网元端文件列表
//
// GET /files