Просмотр исходного кода

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

DarthSim 2 месяцев назад
Родитель
Сommit
80fe152509
5 измененных файлов с 17 добавлено и 12 удалено
  1. 7 1
      imagedata/factory.go
  2. 2 8
      imagedata/image_data.go
  3. 1 1
      imagedata/read.go
  4. 6 1
      svg/svg.go
  5. 1 1
      vips/vips.go

+ 7 - 1
imagedata/factory.go

@@ -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
 }

+ 2 - 8
imagedata/image_data.go

@@ -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

+ 1 - 1
imagedata/read.go

@@ -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
 }

+ 6 - 1
svg/svg.go

@@ -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
 			}

+ 1 - 1
vips/vips.go

@@ -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