download.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package imagedata
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/imgproxy/imgproxy/v3/config"
  6. "github.com/imgproxy/imgproxy/v3/ierrors"
  7. "github.com/imgproxy/imgproxy/v3/imagefetcher"
  8. "github.com/imgproxy/imgproxy/v3/transport"
  9. )
  10. var (
  11. Fetcher *imagefetcher.Fetcher
  12. // For tests. This needs to move to fetcher once we will have a way to isolate
  13. // the fetcher in tests.
  14. redirectAllRequestsTo string
  15. )
  16. type DownloadOptions struct {
  17. Header http.Header
  18. CookieJar http.CookieJar
  19. MaxSrcFileSize int
  20. DownloadFinished context.CancelFunc
  21. }
  22. func DefaultDownloadOptions() DownloadOptions {
  23. return DownloadOptions{
  24. Header: nil,
  25. CookieJar: nil,
  26. MaxSrcFileSize: config.MaxSrcFileSize,
  27. DownloadFinished: nil,
  28. }
  29. }
  30. func initDownloading() error {
  31. ts, err := transport.NewTransport()
  32. if err != nil {
  33. return err
  34. }
  35. Fetcher, err = imagefetcher.NewFetcher(ts, imagefetcher.NewConfigFromEnv())
  36. if err != nil {
  37. return ierrors.Wrap(err, 0, ierrors.WithPrefix("can't create image fetcher"))
  38. }
  39. return nil
  40. }
  41. func RedirectAllRequestsTo(u string) {
  42. redirectAllRequestsTo = u
  43. }
  44. func StopRedirectingRequests() {
  45. redirectAllRequestsTo = ""
  46. }