浏览代码

.Clone() headers, nil

Viktor Sokolov 2 月之前
父节点
当前提交
2c49a1be62
共有 5 个文件被更改,包括 26 次插入14 次删除
  1. 11 4
      imagedata/factory.go
  2. 6 7
      imagedata/image_data.go
  3. 3 1
      imagedata/read.go
  4. 2 1
      svg/svg.go
  5. 4 1
      vips/vips.go

+ 11 - 4
imagedata/factory.go

@@ -2,7 +2,6 @@ package imagedata
 
 import (
 	"bytes"
-	"context"
 	"encoding/base64"
 	"io"
 	"net/http"
@@ -16,11 +15,19 @@ import (
 // NewFromBytesWithFormat creates a new ImageData instance from the provided format,
 // http headers and byte slice.
 func NewFromBytesWithFormat(format imagetype.Type, b []byte, headers http.Header) ImageData {
+	var h http.Header
+
+	if headers == nil {
+		h = make(http.Header)
+	} else {
+		h = headers.Clone()
+	}
+
 	return &imageDataBytes{
 		data:    b,
 		format:  format,
-		headers: headers,
-		cancel:  make([]context.CancelFunc, 0),
+		headers: h,
+		cancel:  nil,
 	}
 }
 
@@ -33,7 +40,7 @@ func NewFromBytes(b []byte) (ImageData, error) {
 		return nil, err
 	}
 
-	return NewFromBytesWithFormat(meta.Format(), b, make(http.Header)), nil
+	return NewFromBytesWithFormat(meta.Format(), b, nil), nil
 }
 
 // NewFromPath creates a new ImageData from an os.File

+ 6 - 7
imagedata/image_data.go

@@ -20,11 +20,11 @@ var (
 )
 
 type ImageData interface {
-	io.Closer                                // Close closes the image data and releases any resources held by it
-	Reader() io.ReadSeeker                   // Reader returns a new ReadSeeker for the image data
-	Format() imagetype.Type                  // Format returns the image format from the metadata (shortcut)
-	Size() (int, error)                      // Size returns the size of the image data in bytes
-	WithCancel(context.CancelFunc) ImageData // WithCancel attaches a cancel function to the image data
+	io.Closer                     // Close closes the image data and releases any resources held by it
+	Reader() io.ReadSeeker        // Reader returns a new ReadSeeker for the image data
+	Format() imagetype.Type       // Format returns the image format from the metadata (shortcut)
+	Size() (int, error)           // Size returns the size of the image data in bytes
+	AddCancel(context.CancelFunc) // AddCancel attaches a cancel function to the image data
 
 	// This will be removed in the future
 	Headers() http.Header // Headers returns the HTTP headers of the image data, will be removed in the future
@@ -70,9 +70,8 @@ func (d *imageDataBytes) Headers() http.Header {
 	return d.headers
 }
 
-func (d *imageDataBytes) WithCancel(cancel context.CancelFunc) ImageData {
+func (d *imageDataBytes) AddCancel(cancel context.CancelFunc) {
 	d.cancel = append(d.cancel, cancel)
-	return d
 }
 
 func Init() error {

+ 3 - 1
imagedata/read.go

@@ -50,7 +50,9 @@ func readAndCheckImage(r io.Reader, contentLength int, secopts security.Options)
 		return nil, imagefetcher.WrapError(err)
 	}
 
-	return NewFromBytesWithFormat(meta.Format(), buf.Bytes(), make(http.Header)).WithCancel(cancel), nil
+	i := NewFromBytesWithFormat(meta.Format(), buf.Bytes(), make(http.Header))
+	i.AddCancel(cancel)
+	return i, nil
 }
 
 func BorrowBuffer() (*bytes.Buffer, context.CancelFunc) {

+ 2 - 1
svg/svg.go

@@ -49,7 +49,8 @@ func Sanitize(data imagedata.ImageData) (imagedata.ImageData, error) {
 				imagetype.SVG,
 				buf.Bytes(),
 				data.Headers().Clone(),
-			).WithCancel(cancel)
+			)
+			newData.AddCancel(cancel)
 
 			return newData, nil
 		case xml.StartTagToken:

+ 4 - 1
vips/vips.go

@@ -470,7 +470,10 @@ func (img *Image) Save(imgtype imagetype.Type, quality int) (imagedata.ImageData
 
 	b := ptrToBytes(ptr, int(imgsize))
 
-	return imagedata.NewFromBytesWithFormat(imgtype, b, make(http.Header)).WithCancel(cancel), nil
+	i := imagedata.NewFromBytesWithFormat(imgtype, b, make(http.Header))
+	i.AddCancel(cancel)
+
+	return i, nil
 }
 
 func (img *Image) Clear() {