feat: 添加网元配置文件备份的FTP配置管理功能
This commit is contained in:
@@ -4,19 +4,16 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"be.ems/lib/file"
|
||||
"be.ems/lib/log"
|
||||
"be.ems/lib/services"
|
||||
"be.ems/src/framework/config"
|
||||
"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"
|
||||
"be.ems/src/framework/utils/crypto"
|
||||
systemService "be.ems/src/modules/system/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -161,23 +158,16 @@ func (m *SysJob) SetFTPConfig(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
cfg := systemService.NewSysConfig.FindByKey("sys.exportTable")
|
||||
cfg := systemService.NewSysConfig.FindByKey("neData.exportTableFTP")
|
||||
if cfg.ConfigId > 0 {
|
||||
// 加密body
|
||||
appKey := config.Get("aes.appKey").(string)
|
||||
byteData, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
bodyEn, err := crypto.AESEncryptBase64(string(byteData), appKey)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
// 更新
|
||||
cfg.ConfigValue = bodyEn
|
||||
systemService.NewSysConfig.Update(cfg)
|
||||
cfg.ConfigValue = string(byteData)
|
||||
cfg.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
systemService.NewSysConfig.UpdateEncryptValue(cfg)
|
||||
}
|
||||
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
@@ -187,15 +177,8 @@ func (m *SysJob) SetFTPConfig(c *gin.Context) {
|
||||
// GET /table/ftp
|
||||
func (m *SysJob) GetFTPConfig(c *gin.Context) {
|
||||
// 获取配置
|
||||
cfg := systemService.NewSysConfig.FindByKey("sys.exportTable")
|
||||
cfg := systemService.NewSysConfig.FindByKeyDecryptValue("neData.exportTableFTP")
|
||||
if cfg.ConfigId > 0 {
|
||||
// 解密body
|
||||
appKey := config.Get("aes.appKey").(string)
|
||||
bodyDe, err := crypto.AESDecryptBase64(cfg.ConfigValue, appKey)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Password string `json:"password" `
|
||||
Username string `json:"username" binding:"required"`
|
||||
@@ -204,8 +187,7 @@ func (m *SysJob) GetFTPConfig(c *gin.Context) {
|
||||
Enable bool `json:"enable"`
|
||||
Dir string `json:"dir" binding:"required"`
|
||||
}
|
||||
err = json.Unmarshal([]byte(bodyDe), &body)
|
||||
if err != nil {
|
||||
if err := json.Unmarshal([]byte(cfg.ConfigValue), &body); err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
@@ -246,17 +228,9 @@ func (m *SysJob) PutFTP(c *gin.Context) {
|
||||
Enable bool `json:"enable"`
|
||||
Dir string `json:"dir" binding:"required"`
|
||||
}
|
||||
cfg := systemService.NewSysConfig.FindByKey("sys.exportTable")
|
||||
cfg := systemService.NewSysConfig.FindByKeyDecryptValue("neData.exportTableFTP")
|
||||
if cfg.ConfigId > 0 {
|
||||
// 解密body
|
||||
appKey := config.Get("aes.appKey").(string)
|
||||
bodyDe, err := crypto.AESDecryptBase64(cfg.ConfigValue, appKey)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal([]byte(bodyDe), &cfgData)
|
||||
if err != nil {
|
||||
if err := json.Unmarshal([]byte(cfg.ConfigValue), &cfgData); err != nil {
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
@@ -287,7 +261,7 @@ func (m *SysJob) PutFTP(c *gin.Context) {
|
||||
}
|
||||
defer sftpClient.Close()
|
||||
// 远程文件
|
||||
remotePath := filepath.Join(cfgData.Dir, path.Base(body.FilePath), body.FileName)
|
||||
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"))
|
||||
|
||||
Reference in New Issue
Block a user