Exemple #1
0
func parseThumbs(r *http.Request) ([]*uploadedfile.ThumbFile, error) {
	thumbString := r.FormValue("thumbs")
	if thumbString == "" {
		return []*uploadedfile.ThumbFile{}, nil
	}

	var t map[string]map[string]interface{}
	err := json.Unmarshal([]byte(thumbString), &t)
	if err != nil {
		return nil, errors.New("Error parsing thumbnail JSON!")
	}

	var thumbs []*uploadedfile.ThumbFile
	for name, thumb := range t {
		width, wOk := thumb["width"].(float64)
		maxWidth, mwOk := thumb["max_width"].(float64)
		height, hOk := thumb["height"].(float64)
		maxHeight, mhOk := thumb["max_height"].(float64)
		if !wOk && !mwOk && !hOk && !mhOk {
			return nil, errors.New("One of [width, max_width, height, max_height] must be set")
		}

		shape, sOk := thumb["shape"].(string)
		if !sOk {
			return nil, errors.New("Invalid thumbnail shape!")
		}

		switch shape {
		case "thumb":
		case "square":
		case "circle":
		case "custom":
		default:
			return nil, errors.New("Invalid thumbnail shape!")
		}

		cropGravity, _ := thumb["crop_gravity"].(string)
		cropHeight, _ := thumb["crop_height"].(float64)
		cropWidth, _ := thumb["crop_width"].(float64)
		quality, _ := thumb["quality"].(float64)
		cropRatio, _ := thumb["crop_ratio"].(string)

		thumbs = append(thumbs, uploadedfile.NewThumbFile(int(width), int(maxWidth), int(height), int(maxHeight), name, shape, "", cropGravity, int(cropWidth), int(cropHeight), cropRatio, int(quality)))
	}

	return thumbs, nil
}
Exemple #2
0
func parseThumbs(r *http.Request) ([]*uploadedfile.ThumbFile, error) {
	thumbString := r.FormValue("thumbs")
	if thumbString == "" {
		return []*uploadedfile.ThumbFile{}, nil
	}

	var t map[string]map[string]interface{}
	err := json.Unmarshal([]byte(thumbString), &t)
	if err != nil {
		return nil, errors.New("Error parsing thumbnail JSON!")
	}

	var thumbs []*uploadedfile.ThumbFile
	for name, thumb := range t {
		width, wOk := thumb["width"].(float64)
		if !wOk {
			return nil, errors.New("Invalid thumbnail width!")
		}

		height, hOk := thumb["height"].(float64)
		if !hOk {
			return nil, errors.New("Invalid thumbnail height!")
		}

		shape, sOk := thumb["shape"].(string)
		if !sOk {
			return nil, errors.New("Invalid thumbnail shape!")
		}

		switch shape {
		case "thumb":
		case "square":
		case "circle":
		default:
			return nil, errors.New("Invalid thumbnail shape!")
		}

		thumbs = append(thumbs, uploadedfile.NewThumbFile(int(width), int(height), name, shape, ""))
	}

	return thumbs, nil
}