Groupme -> Matrix Images

This commit is contained in:
Karmanyaah Malhotra
2021-03-07 11:46:25 -05:00
parent 7d6847fd3b
commit 4e2c73efe3
4 changed files with 264 additions and 172 deletions

View File

@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/karmanyaahm/groupme"
)
@ -31,3 +33,26 @@ func (m *Message) Value() (driver.Value, error) {
}
return e, nil
}
//DownloadImage helper function to download image from groupme;
// append .large/.preview/.avatar to get various sizes
func DownloadImage(URL string) (bytes *[]byte, mime string, err error) {
//TODO check its actually groupme?
response, err := http.Get(URL)
if err != nil {
return nil, "", errors.New("Failed to download avatar: " + err.Error())
}
defer response.Body.Close()
image, err := ioutil.ReadAll(response.Body)
bytes = &image
if err != nil {
return nil, "", errors.New("Failed to read downloaded image:" + err.Error())
}
mime = response.Header.Get("Content-Type")
if len(mime) == 0 {
mime = http.DetectContentType(image)
}
return
}