Replace imgagedata.ImageData.meta with format; Add imagedata.NewFromBytesWithFormat

This commit is contained in:
DarthSim
2025-07-31 19:59:22 +03:00
parent c59ef97a2a
commit 80fe152509
5 changed files with 17 additions and 12 deletions

View File

@@ -6,6 +6,7 @@ import (
"strings"
"github.com/imgproxy/imgproxy/v3/imagemeta"
"github.com/imgproxy/imgproxy/v3/imagetype"
)
// NewFromBytes creates a new ImageData instance from the provided byte slice.
@@ -17,6 +18,11 @@ func NewFromBytes(b []byte, headers http.Header) (*ImageData, error) {
return nil, err
}
return NewFromBytesWithFormat(meta.Format(), b, headers)
}
// NewFromBytesWithFormat creates a new ImageData instance from the provided format and byte slice.
func NewFromBytesWithFormat(format imagetype.Type, b []byte, headers http.Header) (*ImageData, error) {
// Temporary workaround for the old ImageData interface
h := make(map[string]string, len(headers))
for k, v := range headers {
@@ -25,7 +31,7 @@ func NewFromBytes(b []byte, headers http.Header) (*ImageData, error) {
return &ImageData{
data: b,
meta: meta,
format: format,
Headers: h,
}, nil
}

View File

@@ -12,7 +12,6 @@ import (
"github.com/imgproxy/imgproxy/v3/config"
"github.com/imgproxy/imgproxy/v3/ierrors"
"github.com/imgproxy/imgproxy/v3/imagemeta"
"github.com/imgproxy/imgproxy/v3/imagetype"
"github.com/imgproxy/imgproxy/v3/security"
)
@@ -23,7 +22,7 @@ var (
)
type ImageData struct {
meta imagemeta.Meta
format imagetype.Type
data []byte
Headers map[string]string
@@ -41,14 +40,9 @@ func (d *ImageData) Close() error {
return nil
}
// Meta returns the image metadata
func (d *ImageData) Meta() imagemeta.Meta {
return d.meta
}
// Format returns the image format based on the metadata
func (d *ImageData) Format() imagetype.Type {
return d.meta.Format()
return d.format
}
// Reader returns an io.ReadSeeker for the image data

View File

@@ -51,7 +51,7 @@ func readAndCheckImage(r io.Reader, contentLength int, secopts security.Options)
return &ImageData{
data: buf.Bytes(),
meta: meta,
format: meta.Format(),
cancel: cancel,
}, nil
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/tdewolff/parse/v2/xml"
"github.com/imgproxy/imgproxy/v3/imagedata"
"github.com/imgproxy/imgproxy/v3/imagetype"
)
func cloneHeaders(src map[string]string) http.Header {
@@ -54,7 +55,11 @@ func Sanitize(data *imagedata.ImageData) (*imagedata.ImageData, error) {
return nil, l.Err()
}
newData, err := imagedata.NewFromBytes(buf.Bytes(), cloneHeaders(data.Headers))
newData, err := imagedata.NewFromBytesWithFormat(
imagetype.SVG,
buf.Bytes(),
cloneHeaders(data.Headers),
)
if err != nil {
return nil, err
}

View File

@@ -470,7 +470,7 @@ func (img *Image) Save(imgtype imagetype.Type, quality int) (*imagedata.ImageDat
b := ptrToBytes(ptr, int(imgsize))
imgdata, ierr := imagedata.NewFromBytes(b, make(http.Header))
imgdata, ierr := imagedata.NewFromBytesWithFormat(imgtype, b, make(http.Header))
if ierr != nil {
cancel()
return nil, ierr