Пример #1
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"}
}
Пример #2
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"}
	}
}
Пример #3
0
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
}
Пример #4
0
// Change implements Processor.
func (proc *Processor) Change(params imageserver.Params) bool {
	if !params.Has(Param) {
		return false
	}
	params, err := params.GetParams(Param)
	if err != nil {
		return true
	}
	if params.Empty() {
		return false
	}
	if params.Has("width") {
		return true
	}
	if params.Has("height") {
		return true
	}
	if params.Has("interpolation") {
		return true
	}
	if params.Has("mode") {
		return true
	}
	return false
}
Пример #5
0
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")
	}
}
Пример #6
0
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
}
Пример #7
0
// 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)
}
Пример #8
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
}
Пример #9
0
// 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
}
Пример #10
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
}
Пример #11
0
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")
	}
}
Пример #12
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
}
Пример #13
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
}
Пример #14
0
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
}
Пример #15
0
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
}
Пример #16
0
// Change implements imageserver/image.Processor.
func (prc *RotateProcessor) Change(params imageserver.Params) bool {
	if !params.Has(rotateParam) {
		return false
	}
	params, err := params.GetParams(rotateParam)
	if err != nil {
		return true
	}
	if params.Empty() {
		return false
	}
	if params.Has("rotation") {
		return true
	}
	return false
}
Пример #17
0
func (server *Server) buildArgumentsExtent(arguments *list.List, params imageserver.Params, width int, height int) error {
	if width == 0 || height == 0 {
		return nil
	}
	if !params.Has("extent") {
		return nil
	}
	extent, err := params.GetBool("extent")
	if err != nil {
		return err
	}
	if extent {
		arguments.PushBack("-extent")
		arguments.PushBack(fmt.Sprintf("%dx%d", width, height))
	}
	return nil
}
Пример #18
0
func TestListParserParse(t *testing.T) {
	parser := ListParser{
		&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)
	}
	if !params.Has(imageserver.SourceParam) {
		t.Fatal("not set")
	}
}
Пример #19
0
func TestSourceTransformParserUndefined(t *testing.T) {
	req, err := http.NewRequest("GET", "http://localhost", nil)
	if err != nil {
		t.Fatal(err)
	}
	ps := &SourceTransformParser{
		Parser: &SourceParser{},
	}
	params := imageserver.Params{}
	err = ps.Parse(req, params)
	if err != nil {
		t.Fatal(err)
	}
	if params.Has(imageserver.SourceParam) {
		t.Fatal("should not be set")
	}
}
Пример #20
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"}
	}
}
Пример #21
0
// 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
}
Пример #22
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"}
}
Пример #23
0
func (enc *Encoder) getQuality(params imageserver.Params) (int, error) {
	if !params.Has("quality") {
		if enc.DefaultQuality != 0 {
			return enc.DefaultQuality, nil
		}
		return jpeg.DefaultQuality, nil
	}
	quality, err := params.GetInt("quality")
	if err != nil {
		return 0, err
	}
	if quality < 1 {
		return 0, &imageserver.ParamError{Param: "quality", Message: "must be greater than or equal to 1"}
	}
	if quality > 100 {
		return 0, &imageserver.ParamError{Param: "quality", Message: "must be less than or equal to 100"}
	}
	return quality, nil
}
Пример #24
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"}
}
Пример #25
0
// Process implements Processor.
func (prc *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
	}
	if params.Empty() {
		return nim, nil
	}
	nim, err = prc.process(nim, params)
	if err != nil {
		if err, ok := err.(*imageserver.ParamError); ok {
			err.Param = fmt.Sprintf("%s.%s", Param, err.Param)
		}
		return nil, err
	}
	return nim, nil
}
Пример #26
0
// Handle implements imageserver.Handler.
func (hdr *Handler) Handle(im *imageserver.Image, params imageserver.Params) (*imageserver.Image, error) {
	if !params.Has(param) {
		return im, nil
	}
	params, err := params.GetParams(param)
	if err != nil {
		return nil, err
	}
	if params.Empty() {
		return im, nil
	}
	im, err = hdr.handle(im, params)
	if err != nil {
		if err, ok := err.(*imageserver.ParamError); ok {
			err.Param = param + "." + err.Param
		}
		return nil, err
	}
	return im, nil
}
Пример #27
0
func (hdr *Handler) buildArgumentsQuality(arguments *list.List, params imageserver.Params, format string) error {
	if !params.Has("quality") {
		return nil
	}
	quality, err := params.GetInt("quality")
	if err != nil {
		return err
	}
	if quality < 0 {
		return &imageserver.ParamError{Param: "quality", Message: "must be greater than or equal to 0"}
	}
	if format == "jpeg" {
		if quality < 0 || quality > 100 {
			return &imageserver.ParamError{Param: "quality", Message: "must be between 0 and 100"}
		}
	}
	arguments.PushBack("-quality")
	arguments.PushBack(strconv.Itoa(quality))
	return nil
}
Пример #28
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
}
Пример #29
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
}
Пример #30
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
}