Преглед изворни кода

no svg_processing error category

Viktor Sokolov пре 1 месец
родитељ
комит
108a2d1e7f
4 измењених фајлова са 21 додато и 30 уклоњено
  1. 11 14
      processing/processing.go
  2. 1 8
      processing_handler.go
  3. 5 5
      svg/errors.go
  4. 4 3
      svg/svg.go

+ 11 - 14
processing/processing.go

@@ -307,31 +307,28 @@ func ProcessImage(ctx context.Context, imgdata imagedata.ImageData, po *options.
 				OutData:      sanitized,
 				OriginWidth:  originWidth,
 				OriginHeight: originHeight,
-				ResultWidth:  0, // TODO: not sure what to put here
-				ResultHeight: 0, // TODO: not sure what to put here
-			}, nil
-
-		} else {
-			// Return the original image
-			return &Result{
-				OutData:      imgdata,
-				OriginWidth:  originWidth,
-				OriginHeight: originHeight,
 				ResultWidth:  originWidth,
 				ResultHeight: originHeight,
 			}, nil
 		}
-	}
 
-	// At this point we can't allow requested format to be SVG as we can't save SVGs
-	if po.Format == imagetype.SVG {
-		return nil, newSaveFormatError(po.Format)
+		// Return the original image
+		return &Result{
+			OutData:      imgdata,
+			OriginWidth:  originWidth,
+			OriginHeight: originHeight,
+			ResultWidth:  originWidth,
+			ResultHeight: originHeight,
+		}, nil
 	}
 
 	animated := img.IsAnimated()
 	expectAlpha := !po.Flatten && (img.HasAlpha() || po.Padding.Enabled || po.Extend.Enabled)
 
 	switch {
+	case po.Format == imagetype.SVG:
+		// At this point we can't allow requested format to be SVG as we can't save SVGs
+		return nil, newSaveFormatError(po.Format)
 	case po.Format == imagetype.Unknown:
 		switch {
 		case po.PreferJxl && !animated:

+ 1 - 8
processing_handler.go

@@ -29,7 +29,6 @@ import (
 	"github.com/imgproxy/imgproxy/v3/processing"
 	"github.com/imgproxy/imgproxy/v3/router"
 	"github.com/imgproxy/imgproxy/v3/security"
-	"github.com/imgproxy/imgproxy/v3/svg"
 	"github.com/imgproxy/imgproxy/v3/vips"
 )
 
@@ -463,18 +462,12 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
 		return processing.ProcessImage(ctx, originData, po)
 	}()
 
-	// Let's check resulting image date only if it differs from the source image data
+	// Let's close resulting image data only if it differs from the source image data
 	if result != nil && result.OutData != nil && result.OutData != originData {
 		defer result.OutData.Close()
 	}
 
 	if err != nil {
-		// svg_processing is an exception. We use As here because Is does not work with types.
-		var e svg.SanitizeError
-		if errors.As(err, &e) {
-			checkErr(ctx, "svg_processing", e)
-		}
-
 		// First, check if the processing error wasn't caused by an image data error
 		checkErr(ctx, "download", originData.Error())
 

+ 5 - 5
svg/errors.go

@@ -7,17 +7,17 @@ import (
 )
 
 type (
-	SanitizeError string
+	SanitizeError struct{ error }
 )
 
-func newSanitizeError(msg string) error {
+func newSanitizeError(err error) error {
 	return ierrors.Wrap(
-		SanitizeError(msg),
+		SanitizeError{err},
 		1,
 		ierrors.WithStatusCode(http.StatusUnprocessableEntity),
-		ierrors.WithPublicMessage("Invalid URL"),
+		ierrors.WithPublicMessage("Broken or unsupported SVG image"),
 		ierrors.WithShouldReport(false),
 	)
 }
 
-func (e SanitizeError) Error() string { return string(e) }
+func (e SanitizeError) Unwrap() error { return e.error }

+ 4 - 3
svg/svg.go

@@ -2,6 +2,7 @@ package svg
 
 import (
 	"bytes"
+	"errors"
 	"io"
 	"strings"
 	"sync"
@@ -25,7 +26,7 @@ func Sanitize(data imagedata.ImageData) (imagedata.ImageData, error) {
 
 	buf, ok := pool.Get().(*bytes.Buffer)
 	if !ok {
-		return nil, newSanitizeError("svg.Sanitize: failed to get buffer from pool")
+		return nil, newSanitizeError(errors.New("svg.Sanitize: failed to get buffer from pool"))
 	}
 	buf.Reset()
 
@@ -44,7 +45,7 @@ func Sanitize(data imagedata.ImageData) (imagedata.ImageData, error) {
 			switch tt {
 			case xml.ErrorToken:
 				cancel()
-				return nil, newSanitizeError(l.Err().Error())
+				return nil, newSanitizeError(l.Err())
 			case xml.EndTagToken, xml.StartTagCloseVoidToken:
 				ignoreTag--
 			case xml.StartTagToken:
@@ -58,7 +59,7 @@ func Sanitize(data imagedata.ImageData) (imagedata.ImageData, error) {
 		case xml.ErrorToken:
 			if l.Err() != io.EOF {
 				cancel()
-				return nil, newSanitizeError(l.Err().Error())
+				return nil, newSanitizeError(l.Err())
 			}
 
 			newData := imagedata.NewFromBytesWithFormat(