media.go
func (m *MediaService) GetImage(ctx context.Context, messageSid string, sid string) (image.Image, error) {
	u, err := m.GetURL(ctx, messageSid, sid)
	if err != nil {
		return nil, err
	}
	if u.Scheme == "http" {
		return nil, fmt.Errorf("Attempted to download image over insecure URL: %s", u.String())
	}
	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return nil, err
	}
	req = withContext(req, ctx)
	req.Header.Set("User-Agent", userAgent)
	resp, err := MediaClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	// https://www.twilio.com/docs/api/rest/accepted-mime-types#supported
	ctype := resp.Header.Get("Content-Type")
	switch ctype {
	case "image/jpeg":
		return jpeg.Decode(resp.Body)
	case "image/gif":
		return gif.Decode(resp.Body)
	case "image/png":
		return png.Decode(resp.Body)
	default:
		io.Copy(ioutil.Discard, resp.Body)
		return nil, fmt.Errorf("twilio: Unknown content-type %s", ctype)
	}
}
func (m *MediaService) GetImage(ctx context.Context, messageSid string, sid string) (image.Image, error) {
	u, err := m.GetURL(ctx, messageSid, sid)
	if err != nil {
		return nil, err
	}
	if u.Scheme == "http" {
		return nil, fmt.Errorf("Attempted to download image over insecure URL: %s", u.String())
	}
	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return nil, err
	}
	req = withContext(req, ctx)
	req.Header.Set("User-Agent", userAgent)
	resp, err := MediaClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	// https://www.twilio.com/docs/api/rest/accepted-mime-types#supported
	ctype := resp.Header.Get("Content-Type")
	switch ctype {
	case "image/jpeg":
		return jpeg.Decode(resp.Body)
	case "image/gif":
		return gif.Decode(resp.Body)
	case "image/png":
		return png.Decode(resp.Body)
	default:
		io.Copy(ioutil.Discard, resp.Body)
		return nil, fmt.Errorf("twilio: Unknown content-type %s", ctype)
	}
}
func ( m * MediaService ) GetImage ( ctx context . Context , messageSid string , sid string ) ( image . Image , error ) { u , err := m . GetURL ( ctx , messageSid , sid ) if err != nil { return nil , err } if u . Scheme == " " { return nil , fmt . Errorf ( " " , u . String ( ) ) } req , err := http . NewRequest ( " " , u . String ( ) , nil ) if err != nil { return nil , err } req = withContext ( req , ctx ) req . Header . Set ( " " , userAgent ) resp , err := MediaClient . Do ( req ) if err != nil { return nil , err } defer resp . Body . Close ( ) // https://www.twilio.com/docs/api/rest/accepted-mime-types#supported ctype := resp . Header . Get ( " " ) switch ctype { case " " : return jpeg . Decode ( resp . Body ) case " " : return gif . Decode ( resp . Body ) case " " : return png . Decode ( resp . Body ) default : io . Copy ( ioutil . Discard , resp . Body ) return nil , fmt . Errorf ( " " , ctype ) } }
// GetImage downloads a Media object and returns an image.Image. The
// documentation isn't great on what happens - as of October 2016, we make a
// request to the Twilio API, then to media.twiliocdn.com, then to a S3 URL. We
// then download that image and decode it based on the provided content-type.
GetImage downloads a Media object and returns an image . Image . The documentation isn t great on what happens - as of October 2016 we make a request to the Twilio API then to media . twiliocdn . com then to a S3 URL . We then download that image and decode it based on the provided content - type .