diff options
author | Alex Auvolat <alex@adnab.me> | 2020-02-21 15:57:53 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-02-21 15:57:53 +0100 |
commit | ddd5936fb1f92432123a9a30d1d3a1fa644a4f8e (patch) | |
tree | 6a06aac9b5e676a2987f4bf612abea5500155ea2 /connector | |
parent | 92d380aa86dfd3e60f5b8d826ec96c0fbc17614a (diff) | |
download | easybridge-ddd5936fb1f92432123a9a30d1d3a1fa644a4f8e.tar.gz easybridge-ddd5936fb1f92432123a9a30d1d3a1fa644a4f8e.zip |
Files&images Mattermost->Matrix works
Diffstat (limited to 'connector')
-rw-r--r-- | connector/connector.go | 10 | ||||
-rw-r--r-- | connector/mattermost/mattermost.go | 24 | ||||
-rw-r--r-- | connector/mediaobject.go | 85 |
3 files changed, 118 insertions, 1 deletions
diff --git a/connector/connector.go b/connector/connector.go index d05f4a2..2bf1704 100644 --- a/connector/connector.go +++ b/connector/connector.go @@ -115,7 +115,7 @@ type Event struct { Text string // Attached files such as images - Attachements map[string]MediaObject + Attachements []MediaObject } type UserInfo struct { @@ -134,6 +134,9 @@ type MediaObject interface { Size() int64 Mimetype() string + // Returns the size of an image if it is an image, otherwise nil + ImageSize() *ImageSize + // Read: must always be implemented Read() (io.ReadCloser, error) @@ -141,3 +144,8 @@ type MediaObject interface { // If so, Read() is the only way to retrieve the object URL() string } + +type ImageSize struct { + Width int + Height int +} diff --git a/connector/mattermost/mattermost.go b/connector/mattermost/mattermost.go index 81adbde..330026a 100644 --- a/connector/mattermost/mattermost.go +++ b/connector/mattermost/mattermost.go @@ -345,6 +345,30 @@ func (mm *Mattermost) handlePosted(msg *model.WebSocketEvent) error { msg_ev.Type = EVENT_ACTION } + // Handle files + if post.FileIds != nil && len(post.FileIds) > 0 { + msg_ev.Attachements = []MediaObject{} + for _, file := range post.Metadata.Files { + blob, resp := mm.conn.Client.GetFile(file.Id) + if resp.Error != nil { + return resp.Error + } + media_object := &BlobMediaObject{ + ObjectFilename: file.Name, + ObjectSize: file.Size, + ObjectMimetype: file.MimeType, + ObjectData: blob, + } + if file.Width > 0 { + media_object.ObjectImageSize = &ImageSize{ + Width: file.Width, + Height: file.Height, + } + } + msg_ev.Attachements = append(msg_ev.Attachements, media_object) + } + } + // Dispatch as PM or as room message if len(strings.Split(channel_name, "__")) == 2 { // Private message, no need to find room id diff --git a/connector/mediaobject.go b/connector/mediaobject.go index 75635ee..c6634b7 100644 --- a/connector/mediaobject.go +++ b/connector/mediaobject.go @@ -1,6 +1,7 @@ package connector import ( + "bytes" "io" "net/http" "os" @@ -41,6 +42,11 @@ func (m *FileMediaObject) Mimetype() string { return http.DetectContentType(buffer) } +func (m *FileMediaObject) ImageSize() *ImageSize { + // TODO but not really usefull + return nil +} + func (m *FileMediaObject) Read() (io.ReadCloser, error) { return os.Open(m.Path) } @@ -48,3 +54,82 @@ func (m *FileMediaObject) Read() (io.ReadCloser, error) { func (m *FileMediaObject) URL() string { return "" } + +// ---- + +type UrlMediaObject struct { + ObjectFilename string + ObjectSize int64 + ObjectMimetype string + ObjectURL string + ObjectImageSize *ImageSize +} + +func (m *UrlMediaObject) Filename() string { + return m.ObjectFilename +} + +func (m *UrlMediaObject) Size() int64 { + return m.ObjectSize +} + +func (m *UrlMediaObject) Mimetype() string { + return m.ObjectMimetype +} + +func (m *UrlMediaObject) ImageSize() *ImageSize { + return m.ObjectImageSize +} + +func (m *UrlMediaObject) Read() (io.ReadCloser, error) { + resp, err := http.Get(m.ObjectURL) + if err != nil { + return nil, err + } + return resp.Body, nil +} + +func (m *UrlMediaObject) URL() string { + return m.ObjectURL +} + +// ---- + +type BlobMediaObject struct { + ObjectFilename string + ObjectSize int64 + ObjectMimetype string + ObjectImageSize *ImageSize + ObjectData []byte +} + +func (m *BlobMediaObject) Filename() string { + return m.ObjectFilename +} + +func (m *BlobMediaObject) Size() int64 { + return m.ObjectSize +} + +func (m *BlobMediaObject) Mimetype() string { + return m.ObjectMimetype +} + +func (m *BlobMediaObject) ImageSize() *ImageSize { + return m.ObjectImageSize +} + +func (m *BlobMediaObject) Read() (io.ReadCloser, error) { + return nullCloseReader{bytes.NewBuffer(m.ObjectData)}, nil +} + +func (m *BlobMediaObject) URL() string { + return "" +} + +type nullCloseReader struct { + io.Reader +} +func (ncr nullCloseReader) Close() error { + return nil +} |