feat: 统一备份数据发送ftp配置功能接口

This commit is contained in:
TsMask
2025-04-25 15:39:33 +08:00
parent 4d4f6095f6
commit eaf95cadf2
6 changed files with 217 additions and 147 deletions

View File

@@ -12,9 +12,6 @@ import (
"be.ems/src/framework/database/db"
"be.ems/src/framework/i18n"
"be.ems/src/framework/reqctx"
"be.ems/src/framework/resp"
"be.ems/src/framework/ssh"
systemService "be.ems/src/modules/system/service"
"github.com/gin-gonic/gin"
)
@@ -139,133 +136,3 @@ func (m *FileExport) Delete(c *gin.Context) {
}
c.JSON(http.StatusNoContent, nil) // 204 No Content
}
// 设置FTP配置
// POST /table/ftp
func (m *SysJob) SetFTPConfig(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var body struct {
Password string `json:"password" `
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 获取配置
cfg := systemService.NewSysConfig.FindByKey("neData.exportTableFTP")
if cfg.ConfigId > 0 {
byteData, err := json.Marshal(body)
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
cfg.ConfigValue = string(byteData)
cfg.UpdateBy = reqctx.LoginUserToUserName(c)
systemService.NewSysConfig.UpdateEncryptValue(cfg)
}
c.JSON(200, resp.Ok(nil))
}
// 获取FTP配置
// GET /table/ftp
func (m *SysJob) GetFTPConfig(c *gin.Context) {
// 获取配置
cfg := systemService.NewSysConfig.FindByKeyDecryptValue("neData.exportTableFTP")
if cfg.ConfigId > 0 {
var body struct {
Password string `json:"password" `
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
if err := json.Unmarshal([]byte(cfg.ConfigValue), &body); err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
c.JSON(200, resp.OkData(body))
return
}
c.JSON(200, resp.Ok(nil))
}
// FTP发送
// PUT /table/ftp
func (m *SysJob) PutFTP(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var body struct {
FilePath string `json:"filePath" binding:"required"`
FileName string `json:"fileName" binding:"required"`
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
localFilePath := filepath.Join(body.FilePath, body.FileName)
// 判断文件是否存在
if _, err := os.Stat(localFilePath); os.IsNotExist(err) {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
// 获取配置
var cfgData struct {
Password string `json:"password" `
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
cfg := systemService.NewSysConfig.FindByKeyDecryptValue("neData.exportTableFTP")
if cfg.ConfigId > 0 {
if err := json.Unmarshal([]byte(cfg.ConfigValue), &cfgData); err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
}
if !cfgData.Enable {
c.JSON(200, resp.ErrMsg("Setting Remote Backup is disabled"))
return
}
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, resp.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sftpClient.Close()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, "/backup", filepath.Base(localFilePath))
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(localFilePath, remotePath); err != nil {
c.JSON(200, resp.ErrMsg("error uploading file"))
return
}
c.JSON(200, resp.Ok(nil))
}

View File

@@ -15,20 +15,6 @@ func Register(r *gin.RouterGroup) {
middleware.PreAuthorize(nil),
m.GetFileExportTable,
)
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",
middleware.PreAuthorize(nil),
m.PutFTP,
)
}
lmFile := r.Group("/file")
{