From 8a439738ded3a6b2598e0571309eadd0ba70e9b6 Mon Sep 17 00:00:00 2001 From: Florent Messa Date: Tue, 26 Sep 2023 09:50:14 +0200 Subject: [PATCH] chore: avoid using mimetype detector if key already exists (thx @Houtmann) --- middleware/parsers.go | 18 +++++++++++++----- server/http.go | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/middleware/parsers.go b/middleware/parsers.go index 67463ede..acbdc938 100644 --- a/middleware/parsers.go +++ b/middleware/parsers.go @@ -9,6 +9,7 @@ import ( "github.com/gin-gonic/gin" + "github.com/thoas/picfit" "github.com/thoas/picfit/constants" "github.com/thoas/picfit/engine" "github.com/thoas/picfit/hash" @@ -102,15 +103,23 @@ func setParamsFromURLValues(params map[string]interface{}, values url.Values) ma } // URLParser extracts the url query string and add a url.URL to the context -func URLParser(mimetypeDetectorType string) gin.HandlerFunc { +func URLParser(mimetypeDetectorType string, processor *picfit.Processor) gin.HandlerFunc { mimetypeDetector := image.GetMimetypeDetector(mimetypeDetectorType) return func(c *gin.Context) { - value := c.Query("url") + if storeKey := c.MustGet("key").(string); storeKey != "" { + exists, err := processor.KeyExists(c.Request.Context(), storeKey) + if err != nil { + c.Abort() + } + if exists { + c.Next() + return + } + } - if value != "" { + if value := c.Query("url"); value != "" { url, err := url.Parse(value) - if err != nil { c.String(http.StatusBadRequest, fmt.Sprintf("URL %s is not valid", value)) c.Abort() @@ -120,7 +129,6 @@ func URLParser(mimetypeDetectorType string) gin.HandlerFunc { mimetype, _ := mimetypeDetector(url) _, ok := image.Extensions[mimetype] - if !ok { c.String(http.StatusBadRequest, fmt.Sprintf("Mimetype %s is not supported", mimetype)) c.Abort() diff --git a/server/http.go b/server/http.go index 7384935a..b5090a1d 100644 --- a/server/http.go +++ b/server/http.go @@ -143,7 +143,7 @@ func (s *HTTPServer) Init() error { middleware.ParametersParser(), middleware.KeyParser(), middleware.Security(s.config.SecretKey), - middleware.URLParser(s.config.Options.MimetypeDetector), + middleware.URLParser(s.config.Options.MimetypeDetector, s.processor), middleware.OperationParser(), middleware.RestrictSizes(s.config.Options.AllowedSizes), e.handler,