Ejemplo n.º 1
0
func (p dataProvider) Get(c schema.Content) (schema.Content, error) {
	schemeRest := strings.SplitN(c.Url, ":", 2)
	if len(schemeRest) < 2 || schemeRest[0] != "data" {
		return c, ErrInvalidDataURI
	}

	paramData := strings.SplitN(schemeRest[1], ",", 2)
	b64 := false
	if len(paramData) > 1 {
		params := strings.Split(paramData[0], ";")
		c.Type = params[0]
		if params[len(params)-1] == "base64" {
			b64 = true
		}
	}
	data, err := url.QueryUnescape(paramData[len(paramData)-1])
	if err != nil {
		return c, err
	}
	if b64 {
		if c.Data, err = base64.StdEncoding.DecodeString(data); err != nil {
			return c, err
		}
	} else {
		c.Data = []byte(data)
	}

	if c.Type == "" {
		c.Type = "text/plain"
	}

	return c, nil
}
Ejemplo n.º 2
0
func (p dataProvider) Put(c schema.Content) (schema.Content, error) {
	if c.Type == "" {
		c.Type = DetectContentType(c.Data)
	}
	out := schema.Content{
		Url:  "data:" + c.Type,
		Type: c.Type,
	}
	if strings.HasPrefix(c.Type, "text/") {
		out.Url += "," + url.QueryEscape(string(c.Data))
	} else {
		out.Url += ";base64." + url.QueryEscape(base64.StdEncoding.EncodeToString(c.Data))
	}
	return out, nil
}
Ejemplo n.º 3
0
func (p httpProvider) Get(c schema.Content) (schema.Content, error) {
	resp, err := http.Get(c.Url)
	if err != nil {
		return c, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return c, errors.New("unexpected status " + resp.Status)
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return c, err
	}
	c.Data = body
	c.Type = http.DetectContentType(c.Data)
	return c, err
}
Ejemplo n.º 4
0
func (p httpProvider) Put(c schema.Content) (schema.Content, error) {
	if c.Type == "" {
		c.Type = http.DetectContentType(c.Data)
	}
	out := schema.Content{
		Url:  c.Url,
		Type: c.Type,
	}

	resp, err := http.Post(c.Url, c.Type, bytes.NewReader(c.Data))
	if err != nil {
		return out, err
	}
	if resp.StatusCode != 200 {
		return out, errors.New("unexpected status " + resp.Status)
	}
	return out, nil
}