image_data.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // imagedata provides shared ImageData interface for working with image data.
  2. package imagedatanew
  3. import (
  4. "bytes"
  5. "io"
  6. "net/http"
  7. "github.com/imgproxy/imgproxy/v3/asyncbuffer"
  8. "github.com/imgproxy/imgproxy/v3/imagemeta"
  9. "github.com/imgproxy/imgproxy/v3/imagetype"
  10. )
  11. // ImageData is an interface that defines methods for reading image data and metadata
  12. type ImageData interface {
  13. io.Closer // Close closes the image data and releases any resources held by it
  14. Reader() io.ReadSeeker // Reader returns a new ReadSeeker for the image data
  15. Meta() imagemeta.Meta // Meta returns the metadata of the image data
  16. Format() imagetype.Type // Format returns the image format from the metadata (shortcut)
  17. // Will be removed from the interface in the future (DEPRECATED)
  18. Headers() http.Header // Headers returns the HTTP headers of the image data, if applicable
  19. }
  20. // imageDataResponse is a struct that implements the ImageData interface for http.Response
  21. type imageDataResponse struct {
  22. b *asyncbuffer.AsyncBuffer // AsyncBuffer instance
  23. c io.Closer // Closer for the original response body
  24. meta imagemeta.Meta // Metadata of the image data
  25. headers http.Header // Headers for the response, if applicable
  26. }
  27. // imageDataBytes is a struct that implements the ImageData interface for a byte slice
  28. type imageDataBytes struct {
  29. b []byte // ReadSeeker for the image data
  30. meta imagemeta.Meta // Metadata of the image data
  31. headers http.Header // Headers for the response, if applicable
  32. }
  33. // Reader returns a ReadSeeker for the image data
  34. func (r *imageDataResponse) Reader() io.ReadSeeker {
  35. return r.b.Reader()
  36. }
  37. // Close closes the response body (hence, response) and the async buffer itself
  38. func (r *imageDataResponse) Close() error {
  39. if r.c != nil {
  40. defer r.c.Close()
  41. }
  42. return r.b.Close()
  43. }
  44. // Meta returns the metadata of the image data
  45. func (r *imageDataResponse) Meta() imagemeta.Meta {
  46. return r.meta
  47. }
  48. // Format returns the image format from the metadata
  49. func (r *imageDataResponse) Format() imagetype.Type {
  50. return r.meta.Format()
  51. }
  52. // Headers returns the headers of the image data, if applicable
  53. func (r *imageDataResponse) Headers() http.Header {
  54. return r.headers
  55. }
  56. // Reader returns a ReadSeeker for the image data
  57. func (b *imageDataBytes) Reader() io.ReadSeeker {
  58. return bytes.NewReader(b.b)
  59. }
  60. // Close does nothing for imageDataBytes as it does not hold any resources
  61. func (b *imageDataBytes) Close() error {
  62. return nil
  63. }
  64. // Meta returns the metadata of the image data
  65. func (b *imageDataBytes) Meta() imagemeta.Meta {
  66. return b.meta
  67. }
  68. // Format returns the image format from the metadata
  69. func (r *imageDataBytes) Format() imagetype.Type {
  70. return r.meta.Format()
  71. }
  72. // Headers returns the headers of the image data, if applicable
  73. func (r *imageDataBytes) Headers() http.Header {
  74. return r.headers
  75. }