download.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package imagedatanew
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/imgproxy/imgproxy/v3/imagefetcher"
  6. "github.com/imgproxy/imgproxy/v3/security"
  7. )
  8. // HTTPOptions defines options for HTTP requests made to fetch images
  9. type HTTPOptions struct {
  10. Header http.Header
  11. CookieJar http.CookieJar
  12. }
  13. // NewFromURL creates a new ImageData making a HTTP request to the specified URL
  14. func NewFromURL(ctx context.Context, fetcher *imagefetcher.Fetcher, url string, opts HTTPOptions, secopts security.Options) (ImageData, error) {
  15. req, err := fetcher.BuildRequest(ctx, url, opts.Header, opts.CookieJar)
  16. if err != nil {
  17. if req != nil {
  18. defer req.Cancel()
  19. }
  20. return nil, err
  21. }
  22. res, err := req.FetchImage()
  23. if err != nil {
  24. if res != nil {
  25. res.Body.Close()
  26. }
  27. return nil, err
  28. }
  29. // Create factory with the provided security options for this request
  30. imgdata, err := NewFromResponse(res, secopts)
  31. if err != nil {
  32. if res != nil {
  33. req.Cancel()
  34. res.Body.Close()
  35. }
  36. return nil, err
  37. }
  38. return imgdata, nil
  39. }