1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-08 10:45:04 +02:00

Skip processing option (#590)

* Skip processing option

* Simplify SkipProcessingFormats iteration
This commit is contained in:
Svyatoslav Kryukov 2021-03-22 17:16:03 +03:00 committed by GitHub
parent b93aa2f144
commit c07222b501
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 4 deletions

View File

@ -1,6 +1,11 @@
# Changelog
## [Unreleased]
### Added
- `IMGPROXY_FALLBACK_IMAGE_HTTP_CODE` config.
- [expires](https://docs.imgproxy.net/#/generating_the_url?id=expires) processing option.
- [skip processing](https://docs.imgproxy.net/#/generating_the_url?id=skip-processing) processing option.
### Removed
- Removed basic URL format, use [advanced one](./docs/generating_the_url.md) instead.
- Removed `IMGPROXY_MAX_SRC_DIMENSION` config, use `IMGPROXY_MAX_SRC_RESOLUTION` instead.

View File

@ -195,7 +195,7 @@ You can configure imgproxy to skip processing of some formats:
**📝Note:** Processing can be skipped only when the requested format is the same as the source format.
**📝Note:** Video thumbnails processing can't be skipped.
**📝Note:** Video thumbnail processing can't be skipped.
## Presets

View File

@ -518,6 +518,21 @@ ar:%auto_rotate
When set to `1`, `t` or `true`, imgproxy will automatically rotate images based onon the EXIF Orientation parameter (if available in the image meta data). The orientation tag will be removed from the image anyway. Normally this is controlled by the [IMGPROXY_AUTO_ROTATE](configuration.md#miscellaneous) configuration but this procesing option allows the configuration to be set for each request.
### Skip processing
```
skip_processing:%extension1:%extension2:...:%extensionN
skp:%extension1:%extension2:...:%extensionN
```
When set, imgproxy will skip the processing of listed formats. Also available as [IMGPROXY_SKIP_PROCESSING_FORMATS](configuration.md#skip-processing) configuration.
**📝Note:** Processing can be skipped only when the requested format is the same as the source format.
**📝Note:** Video thumbnail processing can't be skipped.
Default: empty
### Filename
```

View File

@ -184,12 +184,12 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
checkTimeout(ctx)
if len(conf.SkipProcessingFormats) > 0 {
po := getProcessingOptions(ctx)
if len(po.SkipProcessingFormats) > 0 {
imgdata := getImageData(ctx)
po := getProcessingOptions(ctx)
if imgdata.Type == po.Format || po.Format == imageTypeUnknown {
for _, f := range conf.SkipProcessingFormats {
for _, f := range po.SkipProcessingFormats {
if f == imgdata.Type {
po.Format = imgdata.Type
respondWithImage(ctx, reqID, r, rw, imgdata.Data)

View File

@ -147,6 +147,8 @@ type processingOptions struct {
StripColorProfile bool
AutoRotate bool
SkipProcessingFormats []imageType
CacheBuster string
Watermark watermarkOptions
@ -241,6 +243,7 @@ func newProcessingOptions() *processingOptions {
})
po := _newProcessingOptions
po.SkipProcessingFormats = append([]imageType(nil), conf.SkipProcessingFormats...)
po.UsedPresets = make([]string, 0, len(conf.Presets))
return &po
@ -880,6 +883,18 @@ func applyCacheBusterOption(po *processingOptions, args []string) error {
return nil
}
func applySkipProcessingFormatsOption(po *processingOptions, args []string) error {
for _, format := range args {
if f, ok := imageTypes[format]; ok {
po.SkipProcessingFormats = append(po.SkipProcessingFormats, f)
} else {
return fmt.Errorf("Invalid image format in skip processing: %s", format)
}
}
return nil
}
func applyFilenameOption(po *processingOptions, args []string) error {
if len(args) > 1 {
return fmt.Errorf("Invalid filename arguments: %v", args)
@ -989,6 +1004,8 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
return applyStripColorProfileOption(po, args)
case "auto_rotate", "ar":
return applyAutoRotateOption(po, args)
case "skip_processing", "skp":
return applySkipProcessingFormatsOption(po, args)
case "filename", "fn":
return applyFilenameOption(po, args)
case "expires", "exp":

View File

@ -568,6 +568,26 @@ func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
assert.Equal(s.T(), 50, po.Quality)
}
func (s *ProcessingOptionsTestSuite) TestParseSkipProcessing() {
req := s.getRequest("/unsafe/skp:jpg:png/plain/http://images.dev/lorem/ipsum.jpg")
ctx, err := parsePath(context.Background(), req)
require.Nil(s.T(), err)
po := getProcessingOptions(ctx)
assert.Equal(s.T(), []imageType{imageTypeJPEG, imageTypePNG}, po.SkipProcessingFormats)
}
func (s *ProcessingOptionsTestSuite) TestParseSkipProcessingInvalid() {
req := s.getRequest("/unsafe/skp:jpg:png:bad_format/plain/http://images.dev/lorem/ipsum.jpg")
_, err := parsePath(context.Background(), req)
require.Error(s.T(), err)
assert.Equal(s.T(), "Invalid image format in skip processing: bad_format", err.Error())
}
func (s *ProcessingOptionsTestSuite) TestParseExpires() {
req := s.getRequest("/unsafe/exp:32503669200/plain/http://images.dev/lorem/ipsum.jpg")
_, err := parsePath(context.Background(), req)