func (prc *ResizeProcessor) getResampling(params imageserver.Params) (gift.Resampling, error) { if !params.Has("resampling") { if prc.DefaultResampling != nil { return prc.DefaultResampling, nil } return gift.NearestNeighborResampling, nil } rsp, err := params.GetString("resampling") if err != nil { return nil, err } switch rsp { case "nearest_neighbor": return gift.NearestNeighborResampling, nil case "box": return gift.BoxResampling, nil case "linear": return gift.LinearResampling, nil case "cubic": return gift.CubicResampling, nil case "lanczos": return gift.LanczosResampling, nil } return nil, &imageserver.ParamError{Param: "resampling", Message: "invalid value"} }
func (server *Server) buildArgumentsMonochrome(arguments *list.List, params imageserver.Params) error { monochrome, _ := params.GetBool("monochrome") if monochrome { arguments.PushBack("-monochrome") } return nil }
func (server *Server) buildArgumentsCrop(arguments *list.List, params imageserver.Params) error { if !params.Has("crop") { return nil } crop, _ := params.GetString("crop") cropArgs := strings.Split(crop, ",") cropArgsLen := len(cropArgs) if cropArgsLen != 2 && cropArgsLen != 4 { return &imageserver.ParamError{Param: "crop", Message: "Invalid crop request, parameters number mismatch"} } if cropArgsLen == 2 { width, _ := strconv.Atoi(cropArgs[0]) height, _ := strconv.Atoi(cropArgs[1]) arguments.PushBack("-crop") arguments.PushBack(fmt.Sprintf("%dx%d", width, height)) arguments.PushBack("+repage") } if cropArgsLen == 4 { width, _ := strconv.Atoi(cropArgs[0]) height, _ := strconv.Atoi(cropArgs[1]) x, _ := strconv.Atoi(cropArgs[2]) y, _ := strconv.Atoi(cropArgs[3]) arguments.PushBack("-crop") arguments.PushBack(fmt.Sprintf("%dx%d+%d+%d", width, height, x, y)) arguments.PushBack("+repage") } return nil }
func (server *Server) buildArgumentsFlop(arguments *list.List, params imageserver.Params) error { flop, _ := params.GetBool("flop") if flop { arguments.PushBack("-flop") } return nil }
func process(nim image.Image, params imageserver.Params) (image.Image, error) { if params.Empty() { return nim, nil } width, err := getDimension("width", params) if err != nil { return nil, err } height, err := getDimension("height", params) if err != nil { return nil, err } if width == 0 && height == 0 { return nim, nil } interp, err := getInterpolation(params) if err != nil { return nil, err } mode, err := getModeFunc(params) if err != nil { return nil, err } nim = mode(width, height, nim, interp) return nim, nil }
func (prc *Processor) getInterpolation(params imageserver.Params) (resize.InterpolationFunction, error) { if !params.Has("interpolation") { return prc.DefaultInterpolation, nil } interpolation, err := params.GetString("interpolation") if err != nil { return 0, err } switch interpolation { case "nearest_neighbor": return resize.NearestNeighbor, nil case "bilinear": return resize.Bilinear, nil case "bicubic": return resize.Bicubic, nil case "mitchell_netravali": return resize.MitchellNetravali, nil case "lanczos2": return resize.Lanczos2, nil case "lanczos3": return resize.Lanczos3, nil default: return 0, &imageserver.ParamError{Param: "interpolation", Message: "invalid value"} } }
func (server *Server) buildArgumentsGravity(arguments *list.List, params imageserver.Params) error { gravity, _ := params.GetString("gravity") var translatedGravity string if gravity != "" { switch { case gravity == "n": translatedGravity = "North" case gravity == "s": translatedGravity = "South" case gravity == "e": translatedGravity = "East" case gravity == "w": translatedGravity = "West" case gravity == "ne": translatedGravity = "NorthEast" case gravity == "se": translatedGravity = "SouthEast" case gravity == "nw": translatedGravity = "NorthWest" case gravity == "sw": translatedGravity = "SouthWest" } if translatedGravity == "" { return &imageserver.ParamError{Param: "gravity", Message: "gravity should n, s, e, w, ne, se, nw or sw"} } } else { // Default gravity is center. translatedGravity = "Center" } arguments.PushBack("-gravity") arguments.PushBack(fmt.Sprintf("%s", translatedGravity)) return nil }
func (server *Server) buildArgumentsGrey(arguments *list.List, params imageserver.Params) error { grey, _ := params.GetBool("grey") if grey { arguments.PushBack("-colorspace") arguments.PushBack("GRAY") } return nil }
func (server *Server) buildArgumentsInterlace(arguments *list.List, params imageserver.Params) error { interlace, _ := params.GetBool("no_interlace") if !interlace { arguments.PushBack("-interlace") arguments.PushBack("Line") } return nil }
func (server *Server) buildArgumentsTrim(arguments *list.List, params imageserver.Params) error { trim, _ := params.GetBool("trim") if trim { // We must execute trim first. (order of operations) arguments.PushFront("-trim") } return nil }
// Get implements imageserver.Server. func (s *Server) Get(params imageserver.Params) (*imageserver.Image, error) { src, err := params.Get(Param) if err != nil { return nil, err } params = imageserver.Params{Param: src} return s.Server.Get(params) }
func (srv *Server) getPath(params imageserver.Params) (string, error) { src, err := params.GetString(imageserver_source.Param) if err != nil { return "", err } // This trick comes from net/http.Dir.Open(). // It allows to "jail" the path inside the root. return filepath.Join(srv.Root, filepath.FromSlash(path.Clean("/"+src))), nil }
func TestParseQueryStringUndefined(t *testing.T) { req, err := http.NewRequest("GET", "http://localhost", nil) if err != nil { t.Fatal(err) } params := imageserver.Params{} ParseQueryString("string", req, params) if params.Has("string") { t.Fatal("should not be set") } }
// 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 }
func (server *Server) buildArgumentsRotate(arguments *list.List, params imageserver.Params) error { rotate, _ := params.GetInt("rotate") if rotate != 0 { if rotate < 0 || rotate > 359 { return &imageserver.ParamError{Param: "rotate", Message: "Invalid rotate parameter"} } arguments.PushBack("-rotate") arguments.PushBack(strconv.Itoa(rotate)) } 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 }
// 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 }
func getDimension(name string, params imageserver.Params) (int, error) { if !params.Has(name) { return 0, nil } dimension, err := params.GetInt(name) if err != nil { return 0, err } if dimension < 0 { return 0, &imageserver.ParamError{Param: name, Message: "must be greater than or equal to 0"} } return dimension, nil }
// Get implements Server. func (srv *ValidateParamsServer) Get(params imageserver.Params) (*imageserver.Image, error) { if params.Has(Param) { params, err := params.GetParams(Param) if err != nil { return nil, err } err = srv.validate(params) if err != nil { return nil, wrapParamError(err) } } return srv.Server.Get(params) }
// 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 }
// Process implements Processor. func (proc *Processor) Process(nim image.Image, params imageserver.Params) (image.Image, error) { if !params.Has(Param) { return nim, nil } params, err := params.GetParams(Param) if err != nil { return nil, err } nim, err = process(nim, params) if err != nil { return nil, wrapParamError(err) } return nim, nil }
func (srv *ValidateParamsServer) validate(params imageserver.Params) error { if params.Empty() { return nil } err := validateDimension("width", srv.WidthMax, params) if err != nil { return err } err = validateDimension("height", srv.HeightMax, params) if err != nil { return err } return nil }
func (prc *RotateProcessor) getBackground(params imageserver.Params) (color.Color, error) { if !params.Has("background") { return color.Transparent, nil } s, err := params.GetString("background") if err != nil { return nil, err } c, err := parseHexColor(s) if err != nil { return nil, &imageserver.ParamError{Param: "background", Message: err.Error()} } return c, nil }
func TestParseQueryString(t *testing.T) { req, err := http.NewRequest("GET", "http://localhost?string=foo", nil) if err != nil { t.Fatal(err) } params := imageserver.Params{} ParseQueryString("string", req, params) s, err := params.GetString("string") if err != nil { t.Fatal(err) } if s != "foo" { t.Fatal("not equals") } }
func TestFormatParserParseUndefined(t *testing.T) { parser := &FormatParser{} req, err := http.NewRequest("GET", "http://localhost", nil) if err != nil { t.Fatal(err) } params := imageserver.Params{} err = parser.Parse(req, params) if err != nil { t.Fatal(err) } if params.Has("format") { t.Fatal("should not be set") } }
func getDimension(name string, max int, params imageserver.Params) (uint, error) { if !params.Has(name) { return 0, nil } d, err := params.GetInt(name) if err != nil { return 0, err } if d < 0 { return 0, &imageserver.ParamError{Param: name, Message: "must be greater than or equal to 0"} } if max > 0 && d > max { return 0, &imageserver.ParamError{Param: name, Message: fmt.Sprintf("must be less than or equal to %d", max)} } return uint(d), nil }
func (hdr *FallbackHandler) getHandler(im *imageserver.Image, params imageserver.Params) (imageserver.Handler, error) { if im.Format != "gif" { return hdr.Fallback, nil } if !params.Has("format") { return hdr.Handler, nil } format, err := params.GetString("format") if err != nil { return nil, err } if format != "gif" { return hdr.Fallback, nil } return hdr.Handler, nil }
func (prc *RotateProcessor) getRotation(params imageserver.Params) (float32, error) { if !params.Has("rotation") { return 0, nil } rot, err := params.GetFloat("rotation") if err != nil { return 0, err } if rot < 0 { rot = math.Mod(rot, 360) + 360 } if rot >= 360 { rot = math.Mod(rot, 360) } return float32(rot), nil }
// Process implements imageserver/image.Processor. func (prc *Processor) Process(im image.Image, params imageserver.Params) (image.Image, error) { if !params.Has(param) { return im, nil } params, err := params.GetParams(param) if err != nil { return nil, err } im, err = prc.process(im, params) if err != nil { if err, ok := err.(*imageserver.ParamError); ok { err.Param = param + "." + err.Param } return nil, err } return im, nil }
func getModeFunc(params imageserver.Params) (modeFunc, error) { if !params.Has("mode") { return resize.Resize, nil } mode, err := params.GetString("mode") if err != nil { return nil, err } switch mode { case "resize": return resize.Resize, nil case "thumbnail": return resize.Thumbnail, nil default: return nil, &imageserver.ParamError{Param: "mode", Message: "invalid value"} } }