easybridge/mxlib/mediaobject.go

66 lines
1.2 KiB
Go
Raw Normal View History

2020-02-21 14:12:22 +00:00
package mxlib
import (
"fmt"
2020-02-23 19:24:50 +00:00
"io"
2020-02-21 14:12:22 +00:00
"net/http"
2020-02-23 19:24:50 +00:00
"net/url"
2020-02-21 14:57:53 +00:00
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
2020-02-21 14:12:22 +00:00
)
type MediaObject struct {
2020-02-23 19:24:50 +00:00
mxClient *Client
filename string
size int64
mimetype string
imageSize *connector.ImageSize
MxcServer string
2020-02-21 14:12:22 +00:00
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
}
2020-02-21 14:57:53 +00:00
func (m *MediaObject) ImageSize() *connector.ImageSize {
return m.imageSize
}
2020-02-21 14:12:22 +00:00
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))
}
func (m *MediaObject) MxcUri() string {
return fmt.Sprintf("mxc://%s/%s", m.MxcServer, m.MxcMediaId)
}