Skip to content

Commit 6d468ad

Browse files
matrix: file extension is added even for non-image attachments
1 parent 36709fb commit 6d468ad

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

bridge/matrix/matrix.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -787,26 +787,39 @@ func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content event.Content
787787
return fmt.Errorf("mtype isn't a %T", mtype)
788788
}
789789

790-
// check if we have an image uploaded without extension
791-
if !strings.Contains(name, ".") {
792-
if msgtype == "m.image" {
793-
mext, _ := mime.ExtensionsByType(mtype)
794-
if len(mext) > 0 {
795-
name += mext[0]
796-
}
797-
} else {
798-
// just a default .png extension if we don't have mime info
799-
name += ".png"
790+
b.Log.Debugf("Processing attachment %s with mimetype %s", name, mtype)
791+
792+
// If the mime library can't guess an appropriate extension for that
793+
// content-type, we're not going to deal with that content because other
794+
// bridges will have problems too.
795+
mext, err := mime.ExtensionsByType(mtype)
796+
if err != nil {
797+
return err
798+
}
799+
800+
// Make sure file has an extension matching the mimetype.
801+
foundExt := false
802+
803+
for _, ext := range mext {
804+
if strings.HasSuffix(name, ext) {
805+
foundExt = true
806+
break
800807
}
801808
}
802809

810+
if !foundExt {
811+
// No extension was found, set the first matching extension
812+
// according to the mime library.
813+
name += mext[0]
814+
}
815+
803816
// Now that we have performed sanity checks and edited the filename,
804817
// remove the message "body" (which was parsed into the filename) so
805818
// we don't have duplicates later on.
806819
rmsg.Text = ""
807820

808821
// TODO: add attachment ID?
809-
err := b.AddAttachmentFromProtectedURL(rmsg, name, "", caption, url)
822+
err = b.AddAttachmentFromProtectedURL(rmsg, name, "", caption, url)
810823
if err != nil {
811824
return err
812825
}

changelog.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@
5252
the return code is not 200 to avoid saving trash data ([#20](https://github.com/matterbridge-org/matterbridge/pull/20))
5353
- matrix
5454
- attachments received from matrix are working again, with authenticated media (MSC3916) implemented ([#61](https://github.com/matterbridge-org/matterbridge/pull/61))
55-
- attachment body is treated as attachment caption and will no longer produce bogus text messages on other bridges
55+
- attachment body is treated as attachment caption and will no longer produce bogus text messages on other bridges ([#169](https://github.com/matterbridge-org/matterbridge/pull/169/))
56+
- attachment filenames without extension how have an extension added according to mimetype, even when they're not images ;
57+
when they are images, it's no longer assumed that they are PNG ([#169](https://github.com/matterbridge-org/matterbridge/pull/169/))
58+
- attachments with an unknown mimetype are discarded to avoid producing more errors further down ([#169](https://github.com/matterbridge-org/matterbridge/pull/169/))
5659
- image attachments are now sent as images with more metadata ([#61](https://github.com/matterbridge-org/matterbridge/pull/61))
5760
- xmpp
5861
- various upstream go-xmpp changes fix connection on SASL2 with PLAIN auth

0 commit comments

Comments
 (0)