From 3ffd96a35038d7be9a1aa8e8f885d82906eb3e71 Mon Sep 17 00:00:00 2001 From: Yousef Mansy Date: Sat, 18 Feb 2023 16:41:19 -0800 Subject: [PATCH] Use native upload when relaying from discord. Resolves #1965 --- bridge/discord/handlers.go | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/bridge/discord/handlers.go b/bridge/discord/handlers.go index 34cef55480..2c92ae2802 100644 --- a/bridge/discord/handlers.go +++ b/bridge/discord/handlers.go @@ -1,7 +1,10 @@ package bdiscord import ( + "path" + "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" "github.com/bwmarrin/discordgo" "github.com/davecgh/go-spew/spew" ) @@ -98,15 +101,14 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat return } - // add the url of the attachments to content - if len(m.Attachments) > 0 { - for _, attach := range m.Attachments { - m.Content = m.Content + "\n" + attach.URL - } + rmsg := config.Message{ + Account: b.Account, + Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg", + UserID: m.Author.ID, + ID: m.ID, + Extra: make(map[string][]interface{}), } - rmsg := config.Message{Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg", UserID: m.Author.ID, ID: m.ID} - b.Log.Debugf("== Receiving event %#v", m.Message) if m.Content != "" { @@ -138,8 +140,14 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat } } - // no empty messages - if rmsg.Text == "" { + if len(m.Attachments) > 0 { + b.handleDownloadFile(&rmsg, m) + } + + hasAttachment := len(rmsg.Extra["file"]) > 0 + + // no empty messages unless has attachment + if rmsg.Text == "" && !hasAttachment { return } @@ -279,3 +287,14 @@ func handleEmbed(embed *discordgo.MessageEmbed) string { return result } + +func (b *Bdiscord) handleDownloadFile(rmsg *config.Message, m *discordgo.MessageCreate) { + for _, attach := range m.Attachments { + data, err := helper.DownloadFile(attach.URL) + if err != nil { + b.Log.Errorf("download %s failed %#v", attach.URL, err) + } + + helper.HandleDownloadData(b.Log, rmsg, path.Base(attach.URL), rmsg.Text, attach.URL, data, b.General) + } +}