upgrade_ftp.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "rtu_linux_services/servicelib"
  9. "strings"
  10. "time"
  11. "hnyfkj.com.cn/rtu/linux/utils/ftpclient"
  12. )
  13. func getFtpUpgradeInfo(ctx context.Context, u *servicelib.UpgradeURL, timeout time.Duration) (upgradeInfo, error) {
  14. localFile, err := ftpclient.DownloadFileFromFtp(ctx, u.Host, u.User, u.Pass, u.Path, timeout)
  15. if err != nil {
  16. return upgradeInfo{}, err
  17. }
  18. defer os.Remove(localFile)
  19. data, err := os.ReadFile(localFile)
  20. if err != nil {
  21. return upgradeInfo{}, err
  22. }
  23. var info upgradeInfo
  24. if err := json.Unmarshal(data, &info); err != nil {
  25. return upgradeInfo{}, err
  26. }
  27. info.Version = strings.TrimSpace(info.Version)
  28. info.SHA256 = strings.TrimSpace(info.SHA256)
  29. if info.Version == "" {
  30. return upgradeInfo{}, fmt.Errorf("升级版本号不能为空")
  31. }
  32. if info.SHA256 == "" {
  33. return upgradeInfo{}, fmt.Errorf("升级校验值不能为空")
  34. }
  35. if len(info.SHA256) != 64 {
  36. return upgradeInfo{}, fmt.Errorf("升级校验值长度错误")
  37. }
  38. if _, err := hex.DecodeString(info.SHA256); err != nil {
  39. return upgradeInfo{}, fmt.Errorf("升级校验值格式错误")
  40. }
  41. return info, nil
  42. }
  43. func downloadFtpUpgradePackage(ctx context.Context, u *servicelib.UpgradeURL, localFile string, timeout time.Duration) error {
  44. file, err := ftpclient.DownloadFileFromFtp(ctx, u.Host, u.User, u.Pass, u.Path, timeout)
  45. if err != nil {
  46. return err
  47. }
  48. defer os.Remove(file)
  49. return os.Rename(file, localFile)
  50. }