factory.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package imagedata
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/base64"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "os"
  10. "github.com/imgproxy/imgproxy/v3/asyncbuffer"
  11. "github.com/imgproxy/imgproxy/v3/ierrors"
  12. "github.com/imgproxy/imgproxy/v3/imagefetcher"
  13. "github.com/imgproxy/imgproxy/v3/imagemeta"
  14. "github.com/imgproxy/imgproxy/v3/imagetype"
  15. "github.com/imgproxy/imgproxy/v3/security"
  16. )
  17. // NewFromBytesWithFormat creates a new ImageData instance from the provided format
  18. // and byte slice.
  19. func NewFromBytesWithFormat(format imagetype.Type, b []byte) ImageData {
  20. return &imageDataBytes{
  21. data: b,
  22. format: format,
  23. cancel: nil,
  24. }
  25. }
  26. // NewFromBytes creates a new ImageData instance from the provided byte slice.
  27. func NewFromBytes(b []byte) (ImageData, error) {
  28. r := bytes.NewReader(b)
  29. meta, err := imagemeta.DecodeMeta(r)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return NewFromBytesWithFormat(meta.Format(), b), nil
  34. }
  35. // NewFromPath creates a new ImageData from an os.File
  36. func NewFromPath(path string, secopts security.Options) (ImageData, error) {
  37. fl, err := os.Open(path)
  38. if err != nil {
  39. return nil, err
  40. }
  41. defer fl.Close()
  42. fr, err := security.LimitFileSize(fl, secopts)
  43. if err != nil {
  44. return nil, err
  45. }
  46. b, err := io.ReadAll(fr)
  47. if err != nil {
  48. return nil, err
  49. }
  50. r := bytes.NewReader(b)
  51. // NOTE: This will be removed in the future in favor of VIPS metadata extraction
  52. // It's here temporarily to maintain compatibility with existing code
  53. meta, err := imagemeta.DecodeMeta(r)
  54. if err != nil {
  55. return nil, err
  56. }
  57. err = security.CheckMeta(meta, secopts)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return NewFromBytes(b)
  62. }
  63. // NewFromBase64 creates a new ImageData from a base64 encoded byte slice
  64. func NewFromBase64(encoded string, secopts security.Options) (ImageData, error) {
  65. b, err := base64.StdEncoding.DecodeString(encoded)
  66. if err != nil {
  67. return nil, err
  68. }
  69. r := bytes.NewReader(b)
  70. // NOTE: This will be removed in the future in favor of VIPS metadata extraction
  71. // It's here temporarily to maintain compatibility with existing code
  72. meta, err := imagemeta.DecodeMeta(r)
  73. if err != nil {
  74. return nil, err
  75. }
  76. err = security.CheckMeta(meta, secopts)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return NewFromBytes(b)
  81. }
  82. // sendRequest is a common logic between sync and async download.
  83. func sendRequest(ctx context.Context, url string, opts DownloadOptions, secopts security.Options) (*imagefetcher.Request, *http.Response, http.Header, error) {
  84. h := make(http.Header)
  85. // NOTE: This will be removed in the future when our test context gets better isolation
  86. if len(redirectAllRequestsTo) > 0 {
  87. url = redirectAllRequestsTo
  88. }
  89. req, err := Fetcher.BuildRequest(ctx, url, opts.Header, opts.CookieJar)
  90. if err != nil {
  91. return req, nil, h, err
  92. }
  93. res, err := req.FetchImage()
  94. if res != nil {
  95. h = res.Header.Clone()
  96. }
  97. if err != nil {
  98. if res != nil {
  99. res.Body.Close()
  100. }
  101. req.Cancel()
  102. return req, nil, h, err
  103. }
  104. res, err = security.LimitResponseSize(res, secopts)
  105. if err != nil {
  106. if res != nil {
  107. res.Body.Close()
  108. }
  109. req.Cancel()
  110. return req, nil, h, err
  111. }
  112. return req, res, h, nil
  113. }
  114. // DownloadSync downloads the image synchronously and returns the ImageData and HTTP headers.
  115. func downloadSync(ctx context.Context, imageURL string, opts DownloadOptions, secopts security.Options) (ImageData, http.Header, error) {
  116. req, res, h, err := sendRequest(ctx, imageURL, opts, secopts)
  117. if res != nil {
  118. defer res.Body.Close()
  119. }
  120. if req != nil {
  121. defer req.Cancel()
  122. }
  123. if err != nil {
  124. return nil, h, err
  125. }
  126. b, err := io.ReadAll(res.Body)
  127. if err != nil {
  128. return nil, h, err
  129. }
  130. meta, err := imagemeta.DecodeMeta(bytes.NewReader(b))
  131. if err != nil {
  132. return nil, h, err
  133. }
  134. err = security.CheckMeta(meta, secopts)
  135. if err != nil {
  136. return nil, h, err
  137. }
  138. d := NewFromBytesWithFormat(meta.Format(), b)
  139. return d, h, err
  140. }
  141. // downloadAsync downloads the image asynchronously and returns the ImageData
  142. // backed by AsyncBuffer and HTTP headers.
  143. func downloadAsync(ctx context.Context, imageURL string, opts DownloadOptions, secopts security.Options) (ImageData, http.Header, error) {
  144. // We pass this responsibility to AsyncBuffer
  145. //nolint:bodyclose
  146. req, res, h, err := sendRequest(ctx, imageURL, opts, secopts)
  147. if err != nil {
  148. return nil, h, err
  149. }
  150. b := asyncbuffer.New(res.Body)
  151. meta, err := imagemeta.DecodeMeta(b.Reader())
  152. if err != nil {
  153. b.Close()
  154. req.Cancel()
  155. return nil, h, err
  156. }
  157. err = security.CheckMeta(meta, secopts)
  158. if err != nil {
  159. b.Close()
  160. req.Cancel()
  161. return nil, h, err
  162. }
  163. d := &imageDataAsyncBuffer{
  164. b: b,
  165. format: meta.Format(),
  166. cancel: nil,
  167. }
  168. d.AddCancel(req.Cancel) // request will be closed when the image data is consumed
  169. return d, h, err
  170. }
  171. // DownloadSyncWithDesc downloads the image synchronously and returns the ImageData, but
  172. // wraps errors with desc.
  173. func DownloadSync(ctx context.Context, imageURL, desc string, opts DownloadOptions, secopts security.Options) (ImageData, http.Header, error) {
  174. imgdata, h, err := downloadSync(ctx, imageURL, opts, secopts)
  175. if err != nil {
  176. return nil, h, ierrors.Wrap(
  177. err, 0,
  178. ierrors.WithPrefix(fmt.Sprintf("can't download %s", desc)),
  179. )
  180. }
  181. return imgdata, h, nil
  182. }
  183. // DownloadSyncWithDesc downloads the image synchronously and returns the ImageData, but
  184. // wraps errors with desc.
  185. func DownloadAsync(ctx context.Context, imageURL, desc string, opts DownloadOptions, secopts security.Options) (ImageData, http.Header, error) {
  186. imgdata, h, err := downloadAsync(ctx, imageURL, opts, secopts)
  187. if err != nil {
  188. return nil, h, ierrors.Wrap(
  189. err, 0,
  190. ierrors.WithPrefix(fmt.Sprintf("can't download %s", desc)),
  191. )
  192. }
  193. return imgdata, h, nil
  194. }