fix: 导出备份配置调整

This commit is contained in:
TsMask
2025-02-11 18:31:54 +08:00
parent bea9ce7092
commit 55dd32b124
8 changed files with 77 additions and 157 deletions

View File

@@ -2,14 +2,10 @@ package file_export
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"time"
"github.com/jlaffaye/ftp"
"be.ems/lib/file"
"be.ems/lib/log"
@@ -160,7 +156,7 @@ func (m *SysJob) SetFTPConfig(c *gin.Context) {
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Protocol string `json:"protocol" binding:"required,oneof=ssh ftp"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
@@ -191,7 +187,7 @@ func (m *SysJob) SetFTPConfig(c *gin.Context) {
c.JSON(200, result.Ok(nil))
}
// 设置FTP配置
// 获取FTP配置
// GET /table/ftp
func (m *SysJob) GetFTPConfig(c *gin.Context) {
// 获取配置
@@ -209,7 +205,7 @@ func (m *SysJob) GetFTPConfig(c *gin.Context) {
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Protocol string `json:"protocol" binding:"required,oneof=ssh ftp"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
err = json.Unmarshal([]byte(bodyDe), &body)
@@ -251,7 +247,7 @@ func (m *SysJob) PutFTP(c *gin.Context) {
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Protocol string `json:"protocol" binding:"required,oneof=ssh ftp"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
cfg := systemService.NewSysConfigImpl.SelectConfigByKey("sys.exportTable")
@@ -269,70 +265,37 @@ func (m *SysJob) PutFTP(c *gin.Context) {
return
}
}
if cfgData.Protocol == "ssh" {
connSSH := ssh.ConnSSH{
User: cfgData.Username,
Password: cfgData.Password,
Addr: cfgData.ToIp,
Port: cfgData.ToPort,
AuthMode: "0",
}
sshClient, err := connSSH.NewClient()
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()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, path.Base(body.FilePath), body.FileName)
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(localFilePath, remotePath); err != nil {
c.JSON(200, result.ErrMsg("error uploading file"))
return
}
c.JSON(200, result.Ok(nil))
if !cfgData.Enable {
c.JSON(200, result.ErrMsg("Setting Remote Backup is disabled"))
return
}
if cfgData.Protocol == "ftp" {
// 连接到 FTP 服务器
addr := fmt.Sprintf("%s:%d", cfgData.ToIp, cfgData.ToPort)
ftpComm, err := ftp.Dial(addr, ftp.DialWithTimeout(15*time.Second))
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
// 登录到 FTP 服务器
err = ftpComm.Login(cfgData.Username, cfgData.Password)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer ftpComm.Quit()
// 打开本地文件
file, err := os.Open(localFilePath)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer file.Close()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, path.Base(body.FilePath), body.FileName)
// 上传文件到 FTP 服务器
err = ftpComm.Stor(remotePath, file)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
connSSH := ssh.ConnSSH{
User: cfgData.Username,
Password: cfgData.Password,
Addr: cfgData.ToIp,
Port: cfgData.ToPort,
AuthMode: "0",
}
c.JSON(200, result.Err(nil))
sshClient, err := connSSH.NewClient()
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()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, path.Base(body.FilePath), body.FileName)
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(localFilePath, remotePath); err != nil {
c.JSON(200, result.ErrMsg("error uploading file"))
return
}
c.JSON(200, result.Ok(nil))
}

View File

@@ -17,10 +17,12 @@ func Register(r *gin.RouterGroup) {
)
lmTable.POST("/ftp",
middleware.PreAuthorize(nil),
middleware.CryptoApi(true, false),
m.SetFTPConfig,
)
lmTable.GET("/ftp",
middleware.PreAuthorize(nil),
middleware.CryptoApi(false, true),
m.GetFTPConfig,
)
lmTable.PUT("/ftp",