瀏覽代碼

Implements #421 processing option to control meta stripping (#422)

* Implements processing option to control meta stripping

* Update based on code review comments

Change name of option to "stripmeta" and change how we handle config vs request setting logic

* Fix indentation style typo

* Fix indentation style typo

* Use full metadata naming for config and variables

Co-authored-by: John Koehl <johnk@zillowgroup.com>
Co-authored-by: John Koehl <jkoehl>
John Koehl 4 年之前
父節點
當前提交
616ec996b3
共有 4 個文件被更改,包括 68 次插入35 次删除
  1. 11 0
      docs/generating_the_url_advanced.md
  2. 2 2
      process.go
  3. 45 33
      processing_options.go
  4. 10 0
      processing_options_test.go

+ 11 - 0
docs/generating_the_url_advanced.md

@@ -428,6 +428,17 @@ It's highly recommended to prefer `cachebuster` option over URL query string bec
 
 Default: empty
 
+#### Strip Metadata
+
+```
+strip_metadata:%strip_metadata
+sm:%strip_metadata
+```
+
+When set to `1`, `t` or `true`, imgproxy will strip the metadata (EXIF, IPTC, etc.) on JPEG and WebP output images. Normally this is controlled by the [IMGPROXY_STRIP_METADATA](configuration.md#miscellaneous) configuration but this procesing option allows the configuration to be set for each request.
+
+Default: `false`
+
 #### Filename
 
 ```

+ 2 - 2
process.go

@@ -639,7 +639,7 @@ func saveImageToFitBytes(po *processingOptions, img *vipsImage) ([]byte, context
 	img.CopyMemory()
 
 	for {
-		result, cancel, err := img.Save(po.Format, quality, conf.StripMetadata)
+		result, cancel, err := img.Save(po.Format, quality, po.StripMetadata)
 		if len(result) <= po.MaxBytes || quality <= 10 || err != nil {
 			return result, cancel, err
 		}
@@ -767,5 +767,5 @@ func processImage(ctx context.Context) ([]byte, context.CancelFunc, error) {
 		return saveImageToFitBytes(po, img)
 	}
 
-	return img.Save(po.Format, po.Quality, conf.StripMetadata)
+	return img.Save(po.Format, po.Quality, po.StripMetadata)
 }

+ 45 - 33
processing_options.go

@@ -126,23 +126,24 @@ type watermarkOptions struct {
 }
 
 type processingOptions struct {
-	ResizingType resizeType
-	Width        int
-	Height       int
-	Dpr          float64
-	Gravity      gravityOptions
-	Enlarge      bool
-	Extend       extendOptions
-	Crop         cropOptions
-	Padding      paddingOptions
-	Trim         trimOptions
-	Format       imageType
-	Quality      int
-	MaxBytes     int
-	Flatten      bool
-	Background   rgbColor
-	Blur         float32
-	Sharpen      float32
+	ResizingType  resizeType
+	Width         int
+	Height        int
+	Dpr           float64
+	Gravity       gravityOptions
+	Enlarge       bool
+	Extend        extendOptions
+	Crop          cropOptions
+	Padding       paddingOptions
+	Trim          trimOptions
+	Format        imageType
+	Quality       int
+	MaxBytes      int
+	Flatten       bool
+	Background    rgbColor
+	Blur          float32
+	Sharpen       float32
+	StripMetadata bool
 
 	CacheBuster string
 
@@ -211,22 +212,23 @@ var (
 func newProcessingOptions() *processingOptions {
 	newProcessingOptionsOnce.Do(func() {
 		_newProcessingOptions = processingOptions{
-			ResizingType: resizeFit,
-			Width:        0,
-			Height:       0,
-			Gravity:      gravityOptions{Type: gravityCenter},
-			Enlarge:      false,
-			Extend:       extendOptions{Enabled: false, Gravity: gravityOptions{Type: gravityCenter}},
-			Padding:      paddingOptions{Enabled: false},
-			Trim:         trimOptions{Enabled: false, Threshold: 10, Smart: true},
-			Quality:      conf.Quality,
-			MaxBytes:     0,
-			Format:       imageTypeUnknown,
-			Background:   rgbColor{255, 255, 255},
-			Blur:         0,
-			Sharpen:      0,
-			Dpr:          1,
-			Watermark:    watermarkOptions{Opacity: 1, Replicate: false, Gravity: gravityOptions{Type: gravityCenter}},
+			ResizingType:  resizeFit,
+			Width:         0,
+			Height:        0,
+			Gravity:       gravityOptions{Type: gravityCenter},
+			Enlarge:       false,
+			Extend:        extendOptions{Enabled: false, Gravity: gravityOptions{Type: gravityCenter}},
+			Padding:       paddingOptions{Enabled: false},
+			Trim:          trimOptions{Enabled: false, Threshold: 10, Smart: true},
+			Quality:       conf.Quality,
+			MaxBytes:      0,
+			Format:        imageTypeUnknown,
+			Background:    rgbColor{255, 255, 255},
+			Blur:          0,
+			Sharpen:       0,
+			Dpr:           1,
+			Watermark:     watermarkOptions{Opacity: 1, Replicate: false, Gravity: gravityOptions{Type: gravityCenter}},
+			StripMetadata: conf.StripMetadata,
 		}
 	})
 
@@ -848,6 +850,14 @@ func applyFilenameOption(po *processingOptions, args []string) error {
 	return nil
 }
 
+func applyMetaStripmetaOption(po *processingOptions, args []string) error {
+	if len(args[0]) > 0 {
+		po.StripMetadata = parseBoolOption(args[0])
+	}
+
+	return nil
+}
+
 func applyProcessingOption(po *processingOptions, name string, args []string) error {
 	switch name {
 	case "format", "f", "ext":
@@ -894,6 +904,8 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
 		return applyCacheBusterOption(po, args)
 	case "filename", "fn":
 		return applyFilenameOption(po, args)
+	case "strip_metadata", "sm":
+		return applyMetaStripmetaOption(po, args)
 	}
 
 	return fmt.Errorf("Unknown processing option: %s", name)

+ 10 - 0
processing_options_test.go

@@ -405,6 +405,16 @@ func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedCachebuster() {
 	assert.Equal(s.T(), "123", po.CacheBuster)
 }
 
+func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedStripMetadata() {
+	req := s.getRequest("http://example.com/unsafe/strip_metadata:true/plain/http://images.dev/lorem/ipsum.jpg")
+	ctx, err := parsePath(context.Background(), req)
+
+	require.Nil(s.T(), err)
+
+	po := getProcessingOptions(ctx)
+	assert.True(s.T(), po.StripMetadata)
+}
+
 func (s *ProcessingOptionsTestSuite) TestParsePathWebpDetection() {
 	conf.EnableWebpDetection = true