groupme/groupmeExt/message.go

59 lines
1.2 KiB
Go
Raw Normal View History

2021-02-13 05:53:35 +00:00
package groupmeExt
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
2021-03-07 16:46:25 +00:00
"io/ioutil"
"net/http"
2021-02-13 05:53:35 +00:00
"github.com/karmanyaahm/groupme"
)
type Message struct{ groupme.Message }
func (m *Message) Scan(value interface{}) error {
2021-02-28 20:02:11 +00:00
bytes, ok := value.(string)
2021-02-13 05:53:35 +00:00
if !ok {
2021-02-28 20:02:11 +00:00
return errors.New(fmt.Sprint("Failed to unmarshal json value:", value))
2021-02-13 05:53:35 +00:00
}
message := Message{}
2021-02-28 20:02:11 +00:00
err := json.Unmarshal([]byte(bytes), &message)
2021-02-13 05:53:35 +00:00
*m = Message(message)
return err
}
func (m *Message) Value() (driver.Value, error) {
e, err := json.Marshal(m)
if err != nil {
return nil, err
}
return e, nil
}
2021-03-07 16:46:25 +00:00
//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
}