download.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package conv
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "errors"
  6. "gadmin/utility"
  7. "github.com/nahid/gohttp"
  8. "github.com/sirupsen/logrus"
  9. "io"
  10. "net"
  11. "net/http"
  12. "os"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. func GetUrlBody(version string, fileName string) ([]byte, error) {
  18. url := utility.PullJsonAddr(version, fileName)
  19. logrus.Info("GetUrlBody url:", url)
  20. tr := &http.Transport{
  21. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  22. DisableCompression: true,
  23. Dial: func(netw, addr string) (net.Conn, error) {
  24. deadline := time.Now().Add(15 * time.Second)
  25. c, err := net.DialTimeout(netw, addr, time.Second*3)
  26. if err != nil {
  27. return nil, err
  28. }
  29. c.SetDeadline(deadline)
  30. return c, nil
  31. },
  32. }
  33. a1 := gohttp.SetTransport(tr)
  34. req := gohttp.NewRequest(a1)
  35. resp, err := req.Get(url)
  36. if err != nil {
  37. return nil, err
  38. }
  39. if resp.GetStatusCode() != 200 {
  40. return nil, errors.New(strconv.Itoa(resp.GetStatusCode()))
  41. }
  42. return resp.GetBodyAsByte()
  43. }
  44. func SaveJsonPath(filePath string, v interface{}) {
  45. logrus.Infoln("Create filePath:", filePath)
  46. index := strings.LastIndex(filePath, "/")
  47. if index == -1 {
  48. logrus.Errorln("文件路径错误!")
  49. return
  50. }
  51. utility.CreateDir(filePath[:index])
  52. ptrFile2, err := os.Create(filePath)
  53. if err != nil {
  54. logrus.Errorln("写日志文件失败", err)
  55. }
  56. defer ptrFile2.Close()
  57. encoder := json.NewEncoder(ptrFile2)
  58. if err = encoder.Encode(&v); err != nil {
  59. logrus.Infoln("Encoder failed", err.Error())
  60. } else {
  61. logrus.Infoln("Encoder success")
  62. }
  63. }
  64. func HttpRequestToFile(url string, dstFileName string) error {
  65. logrus.Infof("HttpRequestToFile url:%v ,dstFileName:%v", url, dstFileName)
  66. tr := &http.Transport{
  67. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  68. DisableCompression: true,
  69. Dial: func(netw, addr string) (net.Conn, error) {
  70. deadline := time.Now().Add(15 * time.Second)
  71. c, err := net.DialTimeout(netw, addr, time.Second*3)
  72. if err != nil {
  73. return nil, err
  74. }
  75. c.SetDeadline(deadline)
  76. return c, nil
  77. },
  78. }
  79. a1 := gohttp.SetTransport(tr)
  80. req := gohttp.NewRequest(a1)
  81. resp, err := req.Get(url)
  82. if err != nil {
  83. return err
  84. }
  85. if resp.GetStatusCode() != 200 {
  86. return errors.New(strconv.Itoa(resp.GetStatusCode()))
  87. }
  88. body := resp.GetBody()
  89. if body == nil {
  90. return nil
  91. }
  92. defer body.Close()
  93. dstFile, err := os.Create(dstFileName) //, os.O_WRONLY|os.O_CREATE, 0755)
  94. if err != nil {
  95. return err
  96. }
  97. io.Copy(dstFile, body)
  98. return nil
  99. }