Skip to content

Commit 5026d3d

Browse files
c-holtermannselfhoster1312
authored andcommitted
matrix: Add metadata for image messages
1 parent f09eac4 commit 5026d3d

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

bridge/matrix/matrix.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ import (
1414
"github.com/matterbridge-org/matterbridge/bridge"
1515
"github.com/matterbridge-org/matterbridge/bridge/config"
1616
"github.com/matterbridge-org/matterbridge/bridge/helper"
17-
// Custom fork of unmaintained library, needs replacement:
17+
"image"
18+
// Initialize specific format decoders,
19+
// see https://pkg.go.dev/image
1820
matrix "github.com/matterbridge/gomatrix"
21+
_ "image/gif"
22+
_ "image/jpeg"
23+
_ "image/png"
1924
)
2025

2126
var (
@@ -696,9 +701,29 @@ func (b *Bmatrix) handleUploadFile(msg *config.Message, channel string, fi *conf
696701
}
697702
case strings.Contains(mtype, "image"):
698703
b.Log.Debugf("sendImage %s", res.ContentURI)
699-
err = b.retry(func() error {
700-
_, err = b.mc.SendImage(channel, fi.Name, res.ContentURI)
701704

705+
cfg, format, err2 := image.DecodeConfig(bytes.NewReader(*fi.Data))
706+
if err2 != nil {
707+
b.Log.WithError(err2).Errorf("Failed to decode image %s", fi.Name)
708+
return
709+
}
710+
711+
b.Log.Debugf("Image format detected: %s (%dx%d)", format, cfg.Width, cfg.Height)
712+
713+
img := matrix.ImageMessage{
714+
MsgType: "m.image",
715+
Body: fi.Name,
716+
URL: res.ContentURI,
717+
Info: matrix.ImageInfo{
718+
Mimetype: mtype,
719+
Size: uint(len(*fi.Data)),
720+
Width: uint(cfg.Width), // #nosec G115 -- go std will not returned negative size
721+
Height: uint(cfg.Height), // #nosec G115 -- go std will not returned negative size
722+
},
723+
}
724+
725+
err = b.retry(func() error {
726+
_, err = b.mc.SendMessageEvent(channel, "m.room.message", img)
702727
return err
703728
})
704729
if err != nil {

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
the return code is not 200 to avoid saving trash data ([#20](https://github.com/matterbridge-org/matterbridge/pull/20))
3434
- matrix
3535
- attachments received from matrix are working again, with authenticated media (MSC3916) implemented ([#61](https://github.com/matterbridge-org/matterbridge/pull/61))
36+
- image attachments are now send as images with more metadata ([#61](https://github.com/matterbridge-org/matterbridge/pull/61))
3637

3738
## Upstream
3839

0 commit comments

Comments
 (0)