1
0

download.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package imagedata
  2. import (
  3. "compress/gzip"
  4. "crypto/tls"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "net/http"
  9. "time"
  10. "github.com/imgproxy/imgproxy/v3/config"
  11. "github.com/imgproxy/imgproxy/v3/ierrors"
  12. azureTransport "github.com/imgproxy/imgproxy/v3/transport/azure"
  13. fsTransport "github.com/imgproxy/imgproxy/v3/transport/fs"
  14. gcsTransport "github.com/imgproxy/imgproxy/v3/transport/gcs"
  15. s3Transport "github.com/imgproxy/imgproxy/v3/transport/s3"
  16. )
  17. var (
  18. downloadClient *http.Client
  19. imageHeadersToStore = []string{
  20. "Cache-Control",
  21. "Expires",
  22. "ETag",
  23. }
  24. // For tests
  25. redirectAllRequestsTo string
  26. ErrNotModified = ierrors.New(http.StatusNotModified, "Not Modified", "Not Modified")
  27. )
  28. const msgSourceImageIsUnreachable = "Source image is unreachable"
  29. func initDownloading() error {
  30. transport := &http.Transport{
  31. Proxy: http.ProxyFromEnvironment,
  32. MaxIdleConns: config.Concurrency,
  33. MaxIdleConnsPerHost: config.Concurrency,
  34. DisableCompression: true,
  35. DialContext: (&net.Dialer{KeepAlive: 600 * time.Second}).DialContext,
  36. }
  37. if config.IgnoreSslVerification {
  38. transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
  39. }
  40. if config.LocalFileSystemRoot != "" {
  41. transport.RegisterProtocol("local", fsTransport.New())
  42. }
  43. if config.S3Enabled {
  44. if t, err := s3Transport.New(); err != nil {
  45. return err
  46. } else {
  47. transport.RegisterProtocol("s3", t)
  48. }
  49. }
  50. if config.GCSEnabled {
  51. if t, err := gcsTransport.New(); err != nil {
  52. return err
  53. } else {
  54. transport.RegisterProtocol("gs", t)
  55. }
  56. }
  57. if config.ABSEnabled {
  58. if t, err := azureTransport.New(); err != nil {
  59. return err
  60. } else {
  61. transport.RegisterProtocol("abs", t)
  62. }
  63. }
  64. downloadClient = &http.Client{
  65. Timeout: time.Duration(config.DownloadTimeout) * time.Second,
  66. Transport: transport,
  67. }
  68. return nil
  69. }
  70. func requestImage(imageURL string, header http.Header) (*http.Response, error) {
  71. req, err := http.NewRequest("GET", imageURL, nil)
  72. if err != nil {
  73. return nil, ierrors.New(404, err.Error(), msgSourceImageIsUnreachable).SetUnexpected(config.ReportDownloadingErrors)
  74. }
  75. req.Header.Set("User-Agent", config.UserAgent)
  76. for k, v := range header {
  77. if len(v) > 0 {
  78. req.Header.Set(k, v[0])
  79. }
  80. }
  81. res, err := downloadClient.Do(req)
  82. if err != nil {
  83. return res, ierrors.New(404, checkTimeoutErr(err).Error(), msgSourceImageIsUnreachable).SetUnexpected(config.ReportDownloadingErrors)
  84. }
  85. if res.StatusCode == http.StatusNotModified {
  86. return nil, ErrNotModified
  87. }
  88. if res.StatusCode != 200 {
  89. body, _ := ioutil.ReadAll(res.Body)
  90. res.Body.Close()
  91. msg := fmt.Sprintf("Status: %d; %s", res.StatusCode, string(body))
  92. return res, ierrors.New(404, msg, msgSourceImageIsUnreachable).SetUnexpected(config.ReportDownloadingErrors)
  93. }
  94. return res, nil
  95. }
  96. func download(imageURL string, header http.Header) (*ImageData, error) {
  97. // We use this for testing
  98. if len(redirectAllRequestsTo) > 0 {
  99. imageURL = redirectAllRequestsTo
  100. }
  101. res, err := requestImage(imageURL, header)
  102. if res != nil {
  103. defer res.Body.Close()
  104. }
  105. if err != nil {
  106. return nil, err
  107. }
  108. body := res.Body
  109. contentLength := int(res.ContentLength)
  110. if res.Header.Get("Content-Encoding") == "gzip" {
  111. gzipBody, errGzip := gzip.NewReader(res.Body)
  112. if gzipBody != nil {
  113. defer gzipBody.Close()
  114. }
  115. if errGzip != nil {
  116. return nil, err
  117. }
  118. body = gzipBody
  119. contentLength = 0
  120. }
  121. imgdata, err := readAndCheckImage(body, contentLength)
  122. if err != nil {
  123. return nil, err
  124. }
  125. imgdata.Headers = make(map[string]string)
  126. for _, h := range imageHeadersToStore {
  127. if val := res.Header.Get(h); len(val) != 0 {
  128. imgdata.Headers[h] = val
  129. }
  130. }
  131. return imgdata, nil
  132. }
  133. func RedirectAllRequestsTo(u string) {
  134. redirectAllRequestsTo = u
  135. }
  136. func StopRedirectingRequests() {
  137. redirectAllRequestsTo = ""
  138. }