diff options
author | Alex Auvolat <alex@adnab.me> | 2020-02-21 15:12:22 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-02-21 15:12:22 +0100 |
commit | 92d380aa86dfd3e60f5b8d826ec96c0fbc17614a (patch) | |
tree | c5edf0e60ee308ba49a0aa526fa5ad184f289f2f /mxlib/mediaobject.go | |
parent | a4cf41536fc8eef743cd330b86e784079100ec6c (diff) | |
download | easybridge-92d380aa86dfd3e60f5b8d826ec96c0fbc17614a.tar.gz easybridge-92d380aa86dfd3e60f5b8d826ec96c0fbc17614a.zip |
Easybridge has an avatar
Diffstat (limited to 'mxlib/mediaobject.go')
-rw-r--r-- | mxlib/mediaobject.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/mxlib/mediaobject.go b/mxlib/mediaobject.go new file mode 100644 index 0000000..8a730d2 --- /dev/null +++ b/mxlib/mediaobject.go @@ -0,0 +1,54 @@ +package mxlib + +import ( + "io" + "fmt" + "net/url" + "net/http" +) + +type MediaObject struct { + mxClient *Client + filename string + size int64 + mimetype string + MxcServer string + MxcMediaId string +} + +func (m *MediaObject) Filename() string { + return m.filename +} + +func (m *MediaObject) Size() int64 { + return m.size +} + +func (m *MediaObject) Mimetype() string { + return m.mimetype +} + +func (m *MediaObject) Read() (io.ReadCloser, error) { + req, err := http.NewRequest("GET", m.URL(), nil) + if err != nil { + return nil, err + } + + req.Header.Add("Authorization", "Bearer "+m.mxClient.Token) + + resp, err := m.mxClient.httpClient.Do(req) + if err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("HTTP error %d", resp.StatusCode) + } + + return resp.Body, nil +} + +func (m *MediaObject) URL() string { + return fmt.Sprintf("%s/_matrix/media/r0/download/%s/%s/%s", + m.mxClient.Server, m.MxcServer, m.MxcMediaId, url.QueryEscape(m.filename)) +} |