utils_http_download.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package utils
  7. import (
  8. "fmt"
  9. "io"
  10. "net/http"
  11. "os"
  12. "strconv"
  13. "time"
  14. "github.com/gogf/gf/v2/errors/gerror"
  15. "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
  16. )
  17. // HTTPDownloadFileWithPercent downloads target url file to local path with percent process printing.
  18. func HTTPDownloadFileWithPercent(url string, localSaveFilePath string) error {
  19. start := time.Now()
  20. out, err := os.Create(localSaveFilePath)
  21. if err != nil {
  22. return gerror.Wrapf(err, `download "%s" to "%s" failed`, url, localSaveFilePath)
  23. }
  24. defer out.Close()
  25. headResp, err := http.Head(url)
  26. if err != nil {
  27. return gerror.Wrapf(err, `download "%s" to "%s" failed`, url, localSaveFilePath)
  28. }
  29. defer headResp.Body.Close()
  30. size, err := strconv.Atoi(headResp.Header.Get("Content-Length"))
  31. if err != nil {
  32. return gerror.Wrap(err, "retrieve Content-Length failed")
  33. }
  34. doneCh := make(chan int64)
  35. go doPrintDownloadPercent(doneCh, localSaveFilePath, int64(size))
  36. resp, err := http.Get(url)
  37. if err != nil {
  38. return gerror.Wrapf(err, `download "%s" to "%s" failed`, url, localSaveFilePath)
  39. }
  40. defer resp.Body.Close()
  41. wroteBytesCount, err := io.Copy(out, resp.Body)
  42. if err != nil {
  43. return gerror.Wrapf(err, `download "%s" to "%s" failed`, url, localSaveFilePath)
  44. }
  45. doneCh <- wroteBytesCount
  46. elapsed := time.Since(start)
  47. if elapsed > time.Minute {
  48. mlog.Printf(`download completed in %.0fm`, float64(elapsed)/float64(time.Minute))
  49. } else {
  50. mlog.Printf(`download completed in %.0fs`, elapsed.Seconds())
  51. }
  52. return nil
  53. }
  54. func doPrintDownloadPercent(doneCh chan int64, localSaveFilePath string, total int64) {
  55. var (
  56. stop = false
  57. lastPercentFmt string
  58. )
  59. for {
  60. select {
  61. case <-doneCh:
  62. stop = true
  63. default:
  64. file, err := os.Open(localSaveFilePath)
  65. if err != nil {
  66. mlog.Fatal(err)
  67. }
  68. fi, err := file.Stat()
  69. if err != nil {
  70. mlog.Fatal(err)
  71. }
  72. size := fi.Size()
  73. if size == 0 {
  74. size = 1
  75. }
  76. var (
  77. percent = float64(size) / float64(total) * 100
  78. percentFmt = fmt.Sprintf(`%.0f`, percent) + "%"
  79. )
  80. if lastPercentFmt != percentFmt {
  81. lastPercentFmt = percentFmt
  82. mlog.Print(percentFmt)
  83. }
  84. }
  85. if stop {
  86. break
  87. }
  88. time.Sleep(time.Second)
  89. }
  90. }