// ParseQueryFloat takes the param from the query string, parse it as a float64 and add it to params. func ParseQueryFloat(param string, req *http.Request, params imageserver.Params) error { s := req.URL.Query().Get(param) if s == "" { return nil } f, err := strconv.ParseFloat(s, 64) if err != nil { return newParseTypeParamError(param, "float", err) } params.Set(param, f) return nil }
// ParseQueryBool takes the param from the query string, parse it as an bool and add it to params. func ParseQueryBool(param string, req *http.Request, params imageserver.Params) error { s := req.URL.Query().Get(param) if s == "" { return nil } b, err := strconv.ParseBool(s) if err != nil { return newParseTypeParamError(param, "bool", err) } params.Set(param, b) return nil }
// ParseQueryInt takes the param from the query string, parse it as an int and add it to params. func ParseQueryInt(param string, req *http.Request, params imageserver.Params) error { s := req.URL.Query().Get(param) if s == "" { return nil } i, err := strconv.Atoi(s) if err != nil { return newParseTypeParamError(param, "int", err) } params.Set(param, i) return nil }
// Parse implements Parser. func (parser *Parser) Parse(req *http.Request, params imageserver.Params) error { p := imageserver.Params{} err := parse(req, p) if err != nil { if err, ok := err.(*imageserver.ParamError); ok { err.Param = Param + "." + err.Param } return err } if !p.Empty() { params.Set(Param, p) } return nil }
// Parse implements Parser. func (parser *FormatParser) Parse(req *http.Request, params imageserver.Params) error { ParseQueryString("format", req, params) if !params.Has("format") { return nil } format, err := params.GetString("format") if err != nil { return err } if format == "jpg" { format = "jpeg" } params.Set("format", format) return nil }
func parseSourceTransform(ps Parser, req *http.Request, params imageserver.Params, f func(string) string) error { err := ps.Parse(req, params) if err != nil { return err } if !params.Has(imageserver.SourceParam) { return nil } source, err := params.GetString(imageserver.SourceParam) if err != nil { return err } source = f(source) params.Set(imageserver.SourceParam, source) return nil }
func benchmark(b *testing.B, im *imageserver.Image, params imageserver.Params) { nim, err := imageserver_image.Decode(im) if err != nil { b.Fatal(err) } params.Set("width", 100) params = imageserver.Params{ Param: params, } proc := &Processor{} b.ResetTimer() for i := 0; i < b.N; i++ { _, err := proc.Process(nim, params) if err != nil { b.Fatal(err) } } }
func benchmarkResizeProcessor(b *testing.B, name string, im *imageserver.Image, params imageserver.Params) { nim, err := imageserver_image.Decode(im) if err != nil { b.Fatal(err) } params.Set("width", 100) params = imageserver.Params{ resizeParam: params, } prc := &ResizeProcessor{} b.Run(name, func(b *testing.B) { for i := 0; i < b.N; i++ { _, err := prc.Process(nim, params) if err != nil { b.Fatal(err) } } }) }
func benchmark(b *testing.B, im *imageserver.Image, params imageserver.Params) { nim, err := imageserver_image.Decode(im) if err != nil { b.Fatal(err) } params.Set("width", 100) params = imageserver.Params{ Param: params, } proc := &Processor{} b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { _, err := proc.Process(nim, params) if err != nil { b.Fatal(err) } } }) }
// Parse implements imageserver/http.Parser. func (prs *Parser) Parse(req *http.Request, params imageserver.Params) error { crop := req.URL.Query().Get(param) if crop == "" { return nil } var minX, minY, maxX, maxY int _, err := fmt.Sscanf(crop, "%d,%d|%d,%d", &minX, &minY, &maxX, &maxY) if err != nil { return &imageserver.ParamError{ Param: param, Message: fmt.Sprintf("expected format '<int>,<int>|<int>,<int>': %s", err), } } params.Set(param, imageserver.Params{ "min_x": minX, "min_y": minY, "max_x": maxX, "max_y": maxY, }) return nil }
// Parse implements Parser. func (parser *SourcePathParser) Parse(req *http.Request, params imageserver.Params) error { params.Set(imageserver.SourceParam, req.URL.Path) return nil }
// ParseQueryString takes the param from the query string and add it to params. func ParseQueryString(param string, req *http.Request, params imageserver.Params) { s := req.URL.Query().Get(param) if s != "" { params.Set(param, s) } }
// Parse implements Parser. func (parser *SourcePathParser) Parse(req *http.Request, params imageserver.Params) error { if len(req.URL.Path) > 0 { params.Set(imageserver_source.Param, req.URL.Path) } return nil }