From e08676079afd2b2726edc1250890f4257749b390 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 20 May 2020 16:43:55 +0300 Subject: [PATCH] Fix bridging encrypted media from Matrix --- portal.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/portal.go b/portal.go index ccc1572..f453973 100644 --- a/portal.go +++ b/portal.go @@ -1002,8 +1002,8 @@ func (portal *Portal) HandleMediaMessage(source *User, download func() ([]byte, } else if err != nil { portal.log.Errorfln("Failed to download media for %s: %v", info.Id, err) resp, err := portal.sendMainIntentMessage(event.MessageEventContent{ - MsgType: event.MsgNotice, - Body: "Failed to bridge media", + MsgType: event.MsgNotice, + Body: "Failed to bridge media", }) if err != nil { portal.log.Errorfln("Failed to send media download error message for %s: %v", info.Id, err) @@ -1157,25 +1157,38 @@ func (portal *Portal) downloadThumbnail(content *event.MessageEventContent, id i return buf.Bytes() } -func (portal *Portal) preprocessMatrixMedia(sender *User, relaybotFormatted bool, content *event.MessageEventContent, id id.EventID, mediaType whatsapp.MediaType) *MediaUpload { +func (portal *Portal) preprocessMatrixMedia(sender *User, relaybotFormatted bool, content *event.MessageEventContent, eventID id.EventID, mediaType whatsapp.MediaType) *MediaUpload { var caption string if relaybotFormatted { caption = portal.bridge.Formatter.ParseMatrix(content.FormattedBody) } - mxc, err := content.URL.Parse() + var file *event.EncryptedFileInfo + rawMXC := content.URL + if content.File != nil { + file = content.File + rawMXC = file.URL + } + mxc, err := rawMXC.Parse() if err != nil { - portal.log.Errorln("Malformed content URL in %s: %v", id, err) + portal.log.Errorln("Malformed content URL in %s: %v", eventID, err) } data, err := portal.MainIntent().DownloadBytes(mxc) if err != nil { - portal.log.Errorfln("Failed to download media in %s: %v", id, err) + portal.log.Errorfln("Failed to download media in %s: %v", eventID, err) return nil } + if file != nil { + data, err = file.Decrypt(data) + if err != nil { + portal.log.Errorfln("Failed to decrypt media in %s: %v", eventID, err) + return nil + } + } url, mediaKey, fileEncSHA256, fileSHA256, fileLength, err := sender.Conn.Upload(bytes.NewReader(data), mediaType) if err != nil { - portal.log.Errorfln("Failed to upload media in %s: %v", id, err) + portal.log.Errorfln("Failed to upload media in %s: %v", eventID, err) return nil } @@ -1186,7 +1199,7 @@ func (portal *Portal) preprocessMatrixMedia(sender *User, relaybotFormatted bool FileEncSHA256: fileEncSHA256, FileSHA256: fileSHA256, FileLength: fileLength, - Thumbnail: portal.downloadThumbnail(content, id), + Thumbnail: portal.downloadThumbnail(content, eventID), } }