コード例 #1
0
ファイル: httpsource.go プロジェクト: cautio/imageserver
func parseResponse(response *http.Response) (*imageserver.Image, error) {
	if response.StatusCode != http.StatusOK {
		return nil, &imageserver.ParamError{
			Param:   imageserver.SourceParam,
			Message: fmt.Sprintf("http status code %d while downloading", response.StatusCode),
		}
	}
	im := new(imageserver.Image)
	contentType := response.Header.Get("Content-Type")
	if contentType != "" {
		matches := contentTypeRegexp.FindStringSubmatch(contentType)
		if matches != nil && len(matches) == 2 {
			im.Format = matches[1]
		}
	}
	data, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, &imageserver.ParamError{
			Param:   imageserver.SourceParam,
			Message: fmt.Sprintf("error while downloading: %s", err),
		}
	}
	im.Data = data
	return im, nil
}