diff --git a/controllers.go b/controllers.go index 98825bfa..3ca98759 100644 --- a/controllers.go +++ b/controllers.go @@ -4,8 +4,8 @@ import ( "encoding/json" "fmt" "mime" - "path" "net/http" + "path" "strconv" "strings" @@ -16,14 +16,14 @@ import ( func indexController(o ServerOptions) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != path.Join(o.PathPrefix, "/") { - ErrorReply(r, w, ErrNotFound, ServerOptions{}) - return + ErrorReply(r, w, ErrNotFound, ServerOptions{}) + return } body, _ := json.Marshal(Versions{ - Version, - bimg.Version, - bimg.VipsVersion, + Version, + bimg.Version, + bimg.VipsVersion, }) w.Header().Set("Content-Type", "application/json") _, _ = w.Write(body) @@ -86,6 +86,7 @@ func imageHandler(w http.ResponseWriter, r *http.Request, buf []byte, operation // If cannot infer the type, infer it via magic numbers if mimeType == "application/octet-stream" { + kind, err := filetype.Get(buf) if err == nil && kind.MIME.Value != "" { mimeType = kind.MIME.Value @@ -111,15 +112,24 @@ func imageHandler(w http.ResponseWriter, r *http.Request, buf []byte, operation return } + acceptedType := determineAcceptMimeType(r.Header.Get("Accept")) + vary := "" if opts.Type == "auto" { - opts.Type = determineAcceptMimeType(r.Header.Get("Accept")) + opts.Type = acceptedType vary = "Accept" // Ensure caches behave correctly for negotiated content } else if opts.Type != "" && ImageType(opts.Type) == 0 { ErrorReply(r, w, ErrOutputFormat, o) return } + for _, opt := range opts.Operations { + val, ok := opt.Params["type"] + if ok && val == "auto" { + opt.Params["type"] = acceptedType + } + } + image, err := operation.Run(buf, opts) if err != nil { // Ensure the Vary header is set when an error occurs @@ -149,34 +159,34 @@ func imageHandler(w http.ResponseWriter, r *http.Request, buf []byte, operation func formController(o ServerOptions) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { operations := []struct { - name string - method string - args string + name string + method string + args string }{ - {"Resize", "resize", "width=300&height=200&type=jpeg"}, - {"Force resize", "resize", "width=300&height=200&force=true"}, - {"Crop", "crop", "width=300&quality=95"}, - {"SmartCrop", "crop", "width=300&height=260&quality=95&gravity=smart"}, - {"Extract", "extract", "top=100&left=100&areawidth=300&areaheight=150"}, - {"Enlarge", "enlarge", "width=1440&height=900&quality=95"}, - {"Rotate", "rotate", "rotate=180"}, - {"AutoRotate", "autorotate", "quality=90"}, - {"Flip", "flip", ""}, - {"Flop", "flop", ""}, - {"Thumbnail", "thumbnail", "width=100"}, - {"Zoom", "zoom", "factor=2&areawidth=300&top=80&left=80"}, - {"Color space (black&white)", "resize", "width=400&height=300&colorspace=bw"}, - {"Add watermark", "watermark", "textwidth=100&text=Hello&font=sans%2012&opacity=0.5&color=255,200,50"}, - {"Convert format", "convert", "type=png"}, - {"Image metadata", "info", ""}, - {"Gaussian blur", "blur", "sigma=15.0&minampl=0.2"}, - {"Pipeline (image reduction via multiple transformations)", "pipeline", "operations=%5B%7B%22operation%22:%20%22crop%22,%20%22params%22:%20%7B%22width%22:%20300,%20%22height%22:%20260%7D%7D,%20%7B%22operation%22:%20%22convert%22,%20%22params%22:%20%7B%22type%22:%20%22webp%22%7D%7D%5D"}, + {"Resize", "resize", "width=300&height=200&type=jpeg"}, + {"Force resize", "resize", "width=300&height=200&force=true"}, + {"Crop", "crop", "width=300&quality=95"}, + {"SmartCrop", "crop", "width=300&height=260&quality=95&gravity=smart"}, + {"Extract", "extract", "top=100&left=100&areawidth=300&areaheight=150"}, + {"Enlarge", "enlarge", "width=1440&height=900&quality=95"}, + {"Rotate", "rotate", "rotate=180"}, + {"AutoRotate", "autorotate", "quality=90"}, + {"Flip", "flip", ""}, + {"Flop", "flop", ""}, + {"Thumbnail", "thumbnail", "width=100"}, + {"Zoom", "zoom", "factor=2&areawidth=300&top=80&left=80"}, + {"Color space (black&white)", "resize", "width=400&height=300&colorspace=bw"}, + {"Add watermark", "watermark", "textwidth=100&text=Hello&font=sans%2012&opacity=0.5&color=255,200,50"}, + {"Convert format", "convert", "type=png"}, + {"Image metadata", "info", ""}, + {"Gaussian blur", "blur", "sigma=15.0&minampl=0.2"}, + {"Pipeline (image reduction via multiple transformations)", "pipeline", "operations=%5B%7B%22operation%22:%20%22crop%22,%20%22params%22:%20%7B%22width%22:%20300,%20%22height%22:%20260%7D%7D,%20%7B%22operation%22:%20%22convert%22,%20%22params%22:%20%7B%22type%22:%20%22webp%22%7D%7D%5D"}, } html := "
" for _, form := range operations { - html += fmt.Sprintf(` + html += fmt.Sprintf(`