Exemplo n.º 1
0
func (this *AudioMerger) parse(cmd string) (format string, mime string, bucket string, url string, duration string, err error) {
	pattern := "^amerge/format/[a-zA-Z0-9]+/mime/[0-9a-zA-Z-_=]+/bucket/[0-9a-zA-Z-_=]+/url/[0-9a-zA-Z-_=]+(/duration/(first|shortest|longest)){0,1}$"
	matched, _ := regexp.MatchString(pattern, cmd)
	if !matched {
		err = errors.New("invalid amerge command format")
		return
	}

	var decodeErr error
	format = utils.GetParam(cmd, "format/[a-zA-Z0-9]+", "format")
	mime, decodeErr = utils.GetParamDecoded(cmd, "mime/[0-9a-zA-Z-_=]+", "mime")
	if decodeErr != nil {
		err = errors.New("invalid amerge parameter 'mime'")
		return
	}
	bucket, decodeErr = utils.GetParamDecoded(cmd, "bucket/[0-9a-zA-Z-_=]+", "bucket")
	if decodeErr != nil {
		err = errors.New("invalid amerge parameter 'bucket'")
		return
	}
	url, decodeErr = utils.GetParamDecoded(cmd, "url/[0-9a-zA-Z-_=]+", "url")
	if decodeErr != nil {
		err = errors.New("invalid amerge parameter 'url'")
		return
	}
	duration = utils.GetParam(cmd, "duration/(first|shortest|longest)", "duration")
	if duration == "" {
		duration = "longest"
	}
	return
}
Exemplo n.º 2
0
/*

unzip/bucket/<encoded bucket>/prefix/<encoded prefix>/overwrite/<[0|1]>

*/
func (this *Unzipper) parse(cmd string) (bucket string, prefix string, overwrite bool, err error) {
	pattern := "^unzip/bucket/[0-9a-zA-Z-_=]+(/prefix/[0-9a-zA-Z-_=]+){0,1}(/overwrite/(0|1)){0,1}$"
	matched, _ := regexp.MatchString(pattern, cmd)
	if !matched {
		err = errors.New("invalid unzip command format")
		return
	}

	var decodeErr error
	bucket, decodeErr = utils.GetParamDecoded(cmd, "bucket/[0-9a-zA-Z-_=]+", "bucket")
	if decodeErr != nil {
		err = errors.New("invalid unzip parameter 'bucket'")
		return
	}
	prefix, decodeErr = utils.GetParamDecoded(cmd, "prefix/[0-9a-zA-Z-_=]+", "prefix")
	if decodeErr != nil {
		err = errors.New("invalid unzip parameter 'prefix'")
		return
	}
	overwriteStr := utils.GetParam(cmd, "overwrite/(0|1)", "overwrite")
	if overwriteStr != "" {
		overwriteVal, paramErr := strconv.ParseInt(overwriteStr, 10, 64)
		if paramErr != nil {
			err = errors.New("invalid unzip parameter 'overwrite'")
			return
		}
		if overwriteVal == 1 {
			overwrite = true
		}
	}
	return
}
Exemplo n.º 3
0
func (this *RoundPicer) parse(cmd string) (params RoundPicParams, err error) {
	pattern := `^roundpic((/radius/\d+(\.\d+){0,1}%{0,1})|(/radius-x/\d+(\.\d+){0,1}%{0,1}/radius-y/\d+(\.\d+){0,1}%{0,1}))$`
	if matched, _ := regexp.MatchString(pattern, cmd); !matched {
		err = errors.New("invalid roundpic command")
		return
	}

	params = RoundPicParams{}

	//get radius if specified
	params.Radius = utils.GetParam(cmd, `radius/\d+(\.\d+){0,1}%{0,1}`, "radius")

	//get radius-x, radius-y
	params.RadiusX = utils.GetParam(cmd, `radius-x/\d+(\.\d+){0,1}%{0,1}`, "radius-x")
	params.RadiusY = utils.GetParam(cmd, `radius-y/\d+(\.\d+){0,1}%{0,1}`, "radius-y")

	if params.Radius == "" && (params.RadiusX == "" || params.RadiusY == "") {
		err = errors.New("roundpic radius or radius-x or radius-y empty error")
		return
	}

	return
}
Exemplo n.º 4
0
func (this *Html2Imager) parse(cmd string) (url string, options *Html2ImageOptions, err error) {
	pattern := `^html2image/url/[0-9a-zA-Z-_=]+(/croph/\d+|/cropw/\d+|/cropx/\d+|/cropy/\d+|/format/(png|jpg|jpeg)|/height/\d+|/quality/\d+|/width/\d+|/force/[0|1]){0,9}$`
	matched, _ := regexp.MatchString(pattern, cmd)
	if !matched {
		err = errors.New("invalid html2image command format")
		return
	}

	options = &Html2ImageOptions{
		Format: "jpg",
	}

	//url
	var decodeErr error
	url, decodeErr = utils.GetParamDecoded(cmd, `url/[0-9a-zA-Z-_=]+`, "url")
	if decodeErr != nil {
		err = errors.New("invalid html2image parameter 'url'")
		return
	}

	//croph
	cropHStr := utils.GetParam(cmd, `croph/\d+`, "croph")
	if cropHStr != "" {
		cropH, _ := strconv.Atoi(cropHStr)
		if cropH <= 0 {
			err = errors.New("invalid html2image parameter 'croph'")
			return
		} else {
			options.CropH = cropH
		}
	}

	//cropw
	cropWStr := utils.GetParam(cmd, `cropw/\d+`, "cropw")
	if cropWStr != "" {
		cropW, _ := strconv.Atoi(cropWStr)
		if cropW <= 0 {
			err = errors.New("invalid html2image parameter 'cropw'")
			return
		} else {
			options.CropW = cropW
		}
	}

	//cropx
	cropXStr := utils.GetParam(cmd, `cropx/\d+`, "cropx")
	fmt.Println(cropXStr)
	if cropXStr != "" {
		cropX, _ := strconv.Atoi(cropXStr)
		if cropX <= 0 {
			err = errors.New("invalid html2image parameter 'cropx'")
			return
		} else {
			options.CropX = cropX
		}
	}

	//cropy
	cropYStr := utils.GetParam(cmd, `cropy/\d+`, "cropy")
	if cropYStr != "" {
		cropY, _ := strconv.Atoi(cropYStr)
		if cropY <= 0 {
			err = errors.New("invalid html2image parameter 'cropy'")
			return
		} else {
			options.CropY = cropY
		}
	}

	//format
	formatStr := utils.GetParam(cmd, "format/(png|jpg|jpeg)", "format")
	if formatStr != "" {
		options.Format = formatStr
	}

	//height
	heightStr := utils.GetParam(cmd, `height/\d+`, "height")
	if heightStr != "" {
		height, _ := strconv.Atoi(heightStr)
		if height <= 0 {
			err = errors.New("invalid html2image parameter 'height'")
			return
		} else {
			options.Height = height
		}
	}

	//width
	widthStr := utils.GetParam(cmd, `width/\d+`, "width")
	if widthStr != "" {
		width, _ := strconv.Atoi(widthStr)
		if width <= 0 {
			err = errors.New("invalid html2image parameter 'width'")
			return
		} else {
			options.Width = width
		}
	}

	//quality
	qualityStr := utils.GetParam(cmd, `quality/\d+`, "quality")
	if qualityStr != "" {
		quality, _ := strconv.Atoi(qualityStr)
		if quality > 100 || quality <= 0 {
			err = errors.New("invalid html2image parameter 'quality'")
			return
		} else {
			options.Quality = quality
		}
	}

	//force
	forceStr := utils.GetParam(cmd, "force/[0|1]", "force")
	if forceStr != "" {
		force, _ := strconv.Atoi(forceStr)
		if force == 1 {
			options.Force = true
		}
	}

	return

}
Exemplo n.º 5
0
func (this *Html2Pdfer) parse(cmd string) (options *Html2PdfOptions, err error) {
	pattern := `^html2pdf(/gray/[0|1]|/low/[0|1]|/orient/(Portrait|Landscape)|/size/[A-B][0-8]|/title/[0-9a-zA-Z-_=]+|/collate/[0|1]|/copies/\d+){0,7}$`
	matched, _ := regexp.MatchString(pattern, cmd)
	if !matched {
		err = errors.New("invalid html2pdf command format")
		return
	}

	var decodeErr error

	//get optional parameters

	options = &Html2PdfOptions{
		Collate: true,
		Copies:  1,
	}

	//get gray
	grayStr := utils.GetParam(cmd, "gray/[0|1]", "gray")
	if grayStr != "" {
		grayInt, _ := strconv.Atoi(grayStr)
		if grayInt == 1 {
			options.Gray = true
		}
	}

	//get low quality
	lowStr := utils.GetParam(cmd, "low/[0|1]", "low")
	if lowStr != "" {
		lowInt, _ := strconv.Atoi(lowStr)
		if lowInt == 1 {
			options.LowQuality = true
		}
	}

	//orient
	options.Orientation = utils.GetParam(cmd, "orient/(Portrait|Landscape)", "orient")

	//size
	options.Size = utils.GetParam(cmd, "size/[A-B][0-8]", "size")

	//title
	title, decodeErr := utils.GetParamDecoded(cmd, "title/[0-9a-zA-Z-_=]+", "title")
	if decodeErr != nil {
		err = errors.New("invalid html2pdf parameter 'title'")
		return
	}
	options.Title = title

	//collate
	collateStr := utils.GetParam(cmd, "collate/[0|1]", "collate")
	if collateStr != "" {
		collateInt, _ := strconv.Atoi(collateStr)
		if collateInt == 0 {
			options.Collate = false
		}
	}

	//copies
	copiesStr := utils.GetParam(cmd, `copies/\d+`, "copies")
	if copiesStr != "" {
		copiesInt, _ := strconv.Atoi(copiesStr)
		if copiesInt <= 0 {
			err = errors.New("invalid html2pdf parameter 'copies'")
			return
		} else {
			options.Copies = copiesInt
		}
	}

	return
}
Exemplo n.º 6
0
/*

imagecomp
/bucket/<string>
/format/<string> 	optional, default jpg
/rows/<int>			optional, default 1
/cols/<int>			optional, default 1
/halign/<string> 	optional, default left
/valign/<string> 	optional, default top
/order/<int>		optional, default 1
/alpha/<int> 		optional, default 0
/bgcolor/<string>	optional, default gray
/margin/<int>		optional, default 0
/url/<string>
/url/<string>

*/
func (this *ImageComposer) parse(cmd string) (bucket, format, halign, valign string,
	rows, cols, order int, bgColor color.Color, margin int, urls []map[string]string, err error) {
	pattern := `^imagecomp/bucket/[0-9a-zA-Z-_=]+(/format/(png|jpg|jpeg)|/halign/(left|right|center)|/valign/(top|bottom|middle)|/rows/\d+|/cols/\d+|/order/(0|1)|/alpha/\d+|/margin/\d+|/bgcolor/[0-9a-zA-Z-_=]+){0,9}(/url/[0-9a-zA-Z-_=]+)+$`

	matched, _ := regexp.MatchString(pattern, cmd)
	if !matched {
		err = errors.New("invalid imagecomp command format")
		return
	}

	var decodeErr error

	//bucket
	bucket, decodeErr = utils.GetParamDecoded(cmd, "bucket/[0-9a-zA-Z-_=]+", "bucket")
	if decodeErr != nil {
		err = errors.New("invalid imagecomp parameter 'bucket'")
		return
	}

	//format
	format = "jpg"
	if v := utils.GetParam(cmd, "format/(png|jpg|jpeg)", "format"); v != "" {
		format = v
	}

	//check later by url count
	//rows
	rows = 0
	if rowsStr := utils.GetParam(cmd, `rows/\d+`, "rows"); rowsStr != "" {
		rows, _ = strconv.Atoi(rowsStr)
	}

	//cols
	cols = 0
	if colsStr := utils.GetParam(cmd, `cols/\d+`, "cols"); colsStr != "" {
		cols, _ = strconv.Atoi(colsStr)
	}

	//halign
	halign = H_ALIGN_LEFT
	if v := utils.GetParam(cmd, "halign/(left|right|center)", "halign"); v != "" {
		halign = v
	}

	//valign
	valign = V_ALIGN_TOP
	if v := utils.GetParam(cmd, "valign/(top|bottom|middle)", "valign"); v != "" {
		valign = v
	}

	//order
	order = IMAGECOMP_ORDER_BY_COL
	if orderStr := utils.GetParam(cmd, "order/(0|1)", "order"); orderStr != "" {
		order, _ = strconv.Atoi(orderStr)
	}

	//alpha
	alpha := 255

	if format == "png" {
		alpha = 0
	}

	if alphaStr := utils.GetParam(cmd, `alpha/\d+`, "alpha"); alphaStr != "" {
		alpha, _ = strconv.Atoi(alphaStr)
	}

	if alpha < 0 || alpha > 255 {
		err = errors.New("invalid imagecomp parameter 'alhpa', should between [0,255]")
		return
	}

	//bgcolor, default white
	bgColor = color.RGBA{0xFF, 0xFF, 0xFF, uint8(alpha)}

	var bgColorStr string
	bgColorStr, decodeErr = utils.GetParamDecoded(cmd, "bgcolor/[0-9a-zA-Z-_=]+", "bgcolor")
	if decodeErr != nil {
		err = errors.New("invalid imagecomp parameter 'bgcolor'")
		return
	} else {
		if bgColorStr != "" {
			colorPattern := `^#[a-fA-F0-9]{6}$`
			if matched, _ := regexp.Match(colorPattern, []byte(bgColorStr)); !matched {
				err = errors.New("invalid imagecomp parameter 'bgcolor', should in format '#FFFFFF'")
				return
			}

			bgColorStr = bgColorStr[1:]

			redPart := bgColorStr[0:2]
			greenPart := bgColorStr[2:4]
			bluePart := bgColorStr[4:6]

			redInt, _ := strconv.ParseInt(redPart, 16, 64)
			greenInt, _ := strconv.ParseInt(greenPart, 16, 64)
			blueInt, _ := strconv.ParseInt(bluePart, 16, 64)

			bgColor = color.RGBA{
				uint8(redInt),
				uint8(greenInt),
				uint8(blueInt),
				uint8(alpha),
			}
		}
	}

	//margin
	margin = 0
	if marginStr := utils.GetParam(cmd, `margin/\d+`, "margin"); marginStr != "" {
		margin, _ = strconv.Atoi(marginStr)
	}

	//urls
	urls = make([]map[string]string, 0)
	urlsPattern := regexp.MustCompile("url/[0-9a-zA-Z-_=]+")
	urlStrings := urlsPattern.FindAllString(cmd, -1)
	for _, urlString := range urlStrings {
		urlBytes, _ := base64.URLEncoding.DecodeString(urlString[4:])
		urlStr := string(urlBytes)
		uri, pErr := url.Parse(urlStr)
		if pErr != nil {
			err = errors.New(fmt.Sprintf("invalid imagecomp parameter 'url', wrong '%s'", urlStr))
			return
		}

		urls = append(urls, map[string]string{
			"path": uri.Path[1:],
			"url":  urlStr,
		})
	}

	//check rows and cols valid or not
	urlCount := len(urls)

	if urlCount > IMAGECOMP_MAX_URL_COUNT {
		err = errors.New(fmt.Sprintf("only allow url count not larger than %d", IMAGECOMP_MAX_URL_COUNT))
		return
	}

	if rows == 0 && cols == 0 {
		cols = 1
		rows = urlCount / cols
	} else if rows == 0 && cols != 0 {
		if cols > urlCount {
			err = errors.New("cols larger than url count error")
			return
		}
		if urlCount%cols == 0 {
			rows = urlCount / cols
		} else {
			rows = urlCount/cols + 1
		}
	} else if rows != 0 && cols == 0 {
		if rows > urlCount {
			err = errors.New("rows larger than url count error")
			return
		}
		if urlCount%rows == 0 {
			cols = urlCount / rows
		} else {
			cols = urlCount/rows + 1
		}
	} else {
		if urlCount > rows*cols {
			err = errors.New("url count larger than rows*cols error")
			return
		}

		if urlCount < rows*cols {
			switch order {
			case IMAGECOMP_ORDER_BY_ROW:
				if urlCount < (rows-1)*cols+1 {
					err = errors.New("url count less than (rows-1)*cols+1 error")
					return
				}
			case IMAGECOMP_ORDER_BY_COL:
				if urlCount < rows*(cols-1)+1 {
					err = errors.New("url count less than rows*(cols-1)+1 error")
					return
				}
			}
		}
	}

	return
}