refactor: 重构更新多个文件中的相关调用

This commit is contained in:
TsMask
2025-02-20 10:11:40 +08:00
parent f3c33b31ac
commit 5b9bcd6660
34 changed files with 1243 additions and 1894 deletions

View File

@@ -2,25 +2,21 @@ 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"
"be.ems/lib/services"
"be.ems/src/framework/config"
"be.ems/src/framework/datasource"
"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"
"be.ems/src/framework/utils/ctx"
"be.ems/src/framework/utils/ssh"
"be.ems/src/framework/vo/result"
systemService "be.ems/src/modules/system/service"
"github.com/gin-gonic/gin"
)
@@ -45,13 +41,13 @@ type TargetParams struct {
func (m *SysJob) GetFileExportTable(c *gin.Context) {
var results []SysJob
err := datasource.DefaultDB().Table(m.TableName()).Where("invoke_target=? and status=1", INVOKE_FILE_EXPORT).
err := db.DB("").Table(m.TableName()).Where("invoke_target=? and status=1", INVOKE_FILE_EXPORT).
Find(&results).Error
if err != nil {
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
return
}
language := ctx.AcceptLanguage(c)
language := reqctx.AcceptLanguage(c)
var response []SysJobResponse
for _, job := range results {
var params TargetParams
@@ -154,54 +150,54 @@ func (m *FileExport) Delete(c *gin.Context) {
// 设置FTP配置
// POST /table/ftp
func (m *SysJob) SetFTPConfig(c *gin.Context) {
language := ctx.AcceptLanguage(c)
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"`
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 {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 获取配置
cfg := systemService.NewSysConfigImpl.SelectConfigByKey("sys.exportTable")
if cfg.ConfigID != "" {
cfg := systemService.NewSysConfig.FindByKey("sys.exportTable")
if cfg.ConfigId > 0 {
// 加密body
appKey := config.Get("aes.appKey").(string)
byteData, err := json.Marshal(body)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
bodyEn, err := crypto.AESEncryptBase64(string(byteData), appKey)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
// 更新
cfg.ConfigValue = bodyEn
systemService.NewSysConfigImpl.UpdateConfig(cfg)
systemService.NewSysConfig.Update(cfg)
}
c.JSON(200, result.Ok(nil))
c.JSON(200, resp.Ok(nil))
}
// 设置FTP配置
// 获取FTP配置
// GET /table/ftp
func (m *SysJob) GetFTPConfig(c *gin.Context) {
// 获取配置
cfg := systemService.NewSysConfigImpl.SelectConfigByKey("sys.exportTable")
if cfg.ConfigID != "" {
cfg := systemService.NewSysConfig.FindByKey("sys.exportTable")
if cfg.ConfigId > 0 {
// 解密body
appKey := config.Get("aes.appKey").(string)
bodyDe, err := crypto.AESDecryptBase64(cfg.ConfigValue, appKey)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
var body struct {
@@ -209,31 +205,31 @@ 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)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
c.JSON(200, result.OkData(body))
c.JSON(200, resp.OkData(body))
return
}
c.JSON(200, result.Ok(nil))
c.JSON(200, resp.Ok(nil))
}
// FTP发送
// PUT /table/ftp
func (m *SysJob) PutFTP(c *gin.Context) {
language := ctx.AcceptLanguage(c)
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, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -241,7 +237,7 @@ func (m *SysJob) PutFTP(c *gin.Context) {
// 判断文件是否存在
if _, err := os.Stat(localFilePath); os.IsNotExist(err) {
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
@@ -251,88 +247,55 @@ 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")
if cfg.ConfigID != "" {
cfg := systemService.NewSysConfig.FindByKey("sys.exportTable")
if cfg.ConfigId > 0 {
// 解密body
appKey := config.Get("aes.appKey").(string)
bodyDe, err := crypto.AESDecryptBase64(cfg.ConfigValue, appKey)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
err = json.Unmarshal([]byte(bodyDe), &cfgData)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, resp.ErrMsg(err.Error()))
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, resp.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, 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, path.Base(body.FilePath), body.FileName)
// 复制到远程
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

@@ -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",