From 9f07f1d548303795c6cde4cf079b160c1bdf57d8 Mon Sep 17 00:00:00 2001 From: Donomii Date: Sat, 15 Jul 2017 18:05:13 +0200 Subject: [PATCH] Further appease pedants --- client.go | 27 +++++++++++++++++++++++++-- events.go | 9 ++++----- responses.go | 2 +- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index 9266d81..f4cc102 100644 --- a/client.go +++ b/client.go @@ -13,6 +13,7 @@ import ( "net/url" "path" "strconv" + "strings" "sync" "time" ) @@ -460,11 +461,10 @@ func (cli *Client) SendFile(roomID, body, url, filename string, info FileInfo, t Body: body, URL: url, FileName: filename, - Info: info, + Info: info, }) } - // SendImage sends an m.room.message event into the given room with a msgtype of m.image // See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-image func (cli *Client) SendImage(roomID, body, url string) (*RespSendEvent, error) { @@ -591,6 +591,29 @@ func (cli *Client) UploadLink(link string) (*RespMediaUpload, error) { return cli.UploadToContentRepo(res.Body, res.Header.Get("Content-Type"), res.ContentLength) } +// Download download a mxc url +func (cli *Client) Download(url string) (string, []byte, error) { + path := strings.Replace(url, "mxc://", "", 1) + req, err := http.NewRequest("GET", cli.BuildBaseURL("_matrix/media/r0/download/"+path), nil) + + res, err := cli.Client.Do(req) + if err != nil { + fmt.Println("Error while downloading", url, "-", err) + return "", nil, err + } + defer res.Body.Close() + + contents, err := ioutil.ReadAll(res.Body) + fmt.Println(res.Header) + filename := res.Header["Content-Disposition"][0] + if err != nil { + fmt.Println("Error while downloading", url, "-", err) + return "", nil, err + } + + return filename, contents, err +} + // UploadToContentRepo uploads the given bytes to the content repository and returns an MXC URI. // See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-media-r0-upload func (cli *Client) UploadToContentRepo(content io.Reader, contentType string, contentLength int64) (*RespMediaUpload, error) { diff --git a/events.go b/events.go index 0ce8278..92c1d4d 100644 --- a/events.go +++ b/events.go @@ -79,14 +79,13 @@ type VideoMessage struct { // FileMessage is an m.file event type FileMessage struct { - MsgType string `json:"msgtype"` - Body string `json:"body"` - URL string `json:"url"` + MsgType string `json:"msgtype"` + Body string `json:"body"` + URL string `json:"url"` FileName string `json:"filename"` - Info FileInfo `json:"info"` + Info FileInfo `json:"info"` } - // ImageMessage is an m.image event type ImageMessage struct { MsgType string `json:"msgtype"` diff --git a/responses.go b/responses.go index fbdf56b..2485da6 100644 --- a/responses.go +++ b/responses.go @@ -163,7 +163,7 @@ type RespSync struct { } `json:"rooms"` } -// LOL what lol could lol this lol do ? +// RespTurnServer was written by someone else who later turned on the automatic commit checker so no-one could commit without writing this comment lol type RespTurnServer struct { Username string `json:"username"` Password string `json:"password"`