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) 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
}
Example #3
0
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"}
}
Example #4
0
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"}
	}
}
Example #5
0
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
}
Example #6
0
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
}
Example #7
0
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")
	}
}
Example #8
0
// 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
}
Example #9
0
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
}
Example #10
0
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
}
Example #11
0
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"}
	}
}
Example #12
0
func (prc *RotateProcessor) getInterpolation(params imageserver.Params) (gift.Interpolation, error) {
	if !params.Has("interpolation") {
		return prc.DefaultInterpolation, nil
	}
	interp, err := params.GetString("interpolation")
	if err != nil {
		return 0, err
	}
	switch interp {
	case "nearest_neighbor":
		return gift.NearestNeighborInterpolation, nil
	case "linear":
		return gift.LinearInterpolation, nil
	case "cubic":
		return gift.CubicInterpolation, nil
	}
	return 0, &imageserver.ParamError{Param: "interpolation", Message: "invalid value"}
}
Example #13
0
func TestFormatParserParse(t *testing.T) {
	parser := &FormatParser{}
	req, err := http.NewRequest("GET", "http://localhost?format=jpg", nil)
	if err != nil {
		t.Fatal(err)
	}
	params := imageserver.Params{}
	err = parser.Parse(req, params)
	if err != nil {
		t.Fatal(err)
	}
	format, err := params.GetString("format")
	if err != nil {
		t.Fatal(err)
	}
	if format != "jpeg" {
		t.Fatal("not equals")
	}
}
Example #14
0
func TestSourceParserParse(t *testing.T) {
	parser := &SourceParser{}
	req, err := http.NewRequest("GET", "http://localhost?source=foo", nil)
	if err != nil {
		t.Fatal(err)
	}
	params := imageserver.Params{}
	err = parser.Parse(req, params)
	if err != nil {
		t.Fatal(err)
	}
	source, err := params.GetString(imageserver.SourceParam)
	if err != nil {
		t.Fatal(err)
	}
	if source != "foo" {
		t.Fatal("not equals")
	}
}
Example #15
0
func (srv *Server) doRequest(params imageserver.Params) (*http.Response, error) {
	src, err := params.GetString(imageserver_source.Param)
	if err != nil {
		return nil, err
	}
	req, err := http.NewRequest("GET", src, nil)
	if err != nil {
		return nil, newSourceError(err.Error())
	}
	c := srv.Client
	if c == nil {
		c = http.DefaultClient
	}
	response, err := c.Do(req)
	if err != nil {
		return nil, newSourceError(err.Error())
	}
	return response, nil
}
Example #16
0
func getEncoderFormat(defaultFormat string, params imageserver.Params) (Encoder, string, error) {
	fromParams := false
	format := defaultFormat
	if params.Has("format") || defaultFormat == "" {
		fromParams = true
		var err error
		format, err = params.GetString("format")
		if err != nil {
			return nil, "", err
		}
	}
	enc, err := getEncoder(format)
	if err != nil {
		if fromParams {
			err = &imageserver.ParamError{Param: "format", Message: err.Error()}
		}
		return nil, "", err
	}
	return enc, format, nil
}
Example #17
0
func (prc *ResizeProcessor) getFilter(width, height int, params imageserver.Params) (gift.Filter, error) {
	rsp, err := prc.getResampling(params)
	if err != nil {
		return nil, err
	}
	if !params.Has("mode") || width == 0 || height == 0 {
		return gift.Resize(width, height, rsp), nil
	}
	mode, err := params.GetString("mode")
	if err != nil {
		return nil, err
	}
	switch mode {
	case "fit":
		return gift.ResizeToFit(width, height, rsp), nil
	case "fill":
		return gift.ResizeToFill(width, height, rsp, gift.CenterAnchor), nil
	}
	return nil, &imageserver.ParamError{Param: "mode", Message: "invalid value"}
}
Example #18
0
func getSourceURL(params imageserver.Params) (*url.URL, error) {
	source, err := params.GetString(imageserver.SourceParam)
	if err != nil {
		return nil, err
	}
	sourceURL, err := url.ParseRequestURI(source)
	if err != nil {
		return nil, &imageserver.ParamError{
			Param:   imageserver.SourceParam,
			Message: fmt.Sprintf("parse url error: %s", err),
		}
	}
	if sourceURL.Scheme != "http" && sourceURL.Scheme != "https" {
		return nil, &imageserver.ParamError{
			Param:   imageserver.SourceParam,
			Message: "url scheme must be http(s)",
		}
	}
	return sourceURL, nil
}
Example #19
0
func TestSourcePrefixParser(t *testing.T) {
	req, err := http.NewRequest("GET", "http://localhost?source=bar", nil)
	if err != nil {
		t.Fatal(err)
	}
	ps := &SourcePrefixParser{
		Parser: &SourceParser{},
		Prefix: "foo",
	}
	params := imageserver.Params{}
	err = ps.Parse(req, params)
	if err != nil {
		t.Fatal(err)
	}
	source, err := params.GetString(imageserver_source.Param)
	if err != nil {
		t.Fatal(err)
	}
	if source != "foobar" {
		t.Fatal("not equals")
	}
}
Example #20
0
func (hdr *Handler) buildArgumentsBackground(arguments *list.List, params imageserver.Params) error {
	if !params.Has("background") {
		return nil
	}
	background, err := params.GetString("background")
	if err != nil {
		return err
	}
	switch len(background) {
	case 3, 4, 6, 8:
	default:
		return &imageserver.ParamError{Param: "background", Message: "length must be equal to 3, 4, 6 or 8"}
	}
	for _, r := range background {
		if (r < '0' || r > '9') && (r < 'a' || r > 'f') {
			return &imageserver.ParamError{Param: "background", Message: "must only contain characters in 0-9a-f"}
		}
	}
	arguments.PushBack("-background")
	arguments.PushBack(fmt.Sprintf("#%s", background))
	return nil
}
Example #21
0
func TestSourceTransformParser(t *testing.T) {
	req, err := http.NewRequest("GET", "http://localhost?source=foo", nil)
	if err != nil {
		t.Fatal(err)
	}
	ps := &SourceTransformParser{
		Parser: &SourceParser{},
		Transform: func(source string) string {
			return "bar"
		},
	}
	params := imageserver.Params{}
	err = ps.Parse(req, params)
	if err != nil {
		t.Fatal(err)
	}
	source, err := params.GetString(imageserver.SourceParam)
	if err != nil {
		t.Fatal(err)
	}
	if source != "bar" {
		t.Fatal("not equals")
	}
}
Example #22
0
func (hdr *Handler) buildArgumentsFormat(arguments *list.List, params imageserver.Params, sourceImage *imageserver.Image) (format string, formatSpecified bool, err error) {
	if !params.Has("format") {
		return sourceImage.Format, false, nil
	}
	format, err = params.GetString("format")
	if err != nil {
		return "", false, err
	}
	if hdr.AllowedFormats != nil {
		ok := false
		for _, f := range hdr.AllowedFormats {
			if f == format {
				ok = true
				break
			}
		}
		if !ok {
			return "", false, &imageserver.ParamError{Param: "format", Message: "not allowed"}
		}
	}
	arguments.PushBack("-format")
	arguments.PushBack(format)
	return format, true, nil
}