Skip to content

Commit 0a6d7fb

Browse files
selfhoster1312poVoq
authored andcommitted
discord: Always download attachments in the background
1 parent 319b7d9 commit 0a6d7fb

4 files changed

Lines changed: 41 additions & 69 deletions

File tree

bridge/discord/discord.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ type Bdiscord struct {
3636
userMemberMap map[string]*discordgo.Member
3737
nickMemberMap map[string]*discordgo.Member
3838

39-
// Never send Discord's attachments as URLs, always download and re-upload them to the destination as regular files
40-
alwaysDownloadFiles bool
41-
4239
// Webhook specific logic
4340
useAutoWebhooks bool
4441
transmitter *transmitter.Transmitter
@@ -60,8 +57,6 @@ func New(cfg *bridge.Config) bridge.Bridger {
6057
b.nickMemberMap = make(map[string]*discordgo.Member)
6158
b.channelInfoMap = make(map[string]*config.ChannelInfo)
6259

63-
b.alwaysDownloadFiles = b.GetBool("AlwaysDownloadFiles")
64-
6560
b.useAutoWebhooks = b.GetBool("AutoWebhooks")
6661
if b.useAutoWebhooks {
6762
b.Log.Debug("Using automatic webhooks")

bridge/discord/handlers.go

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/bwmarrin/discordgo"
77
"github.com/davecgh/go-spew/spew"
88
"github.com/matterbridge-org/matterbridge/bridge/config"
9-
"github.com/matterbridge-org/matterbridge/bridge/helper"
109
)
1110

1211
func (b *Bdiscord) messageDelete(s *discordgo.Session, m *discordgo.MessageDelete) { //nolint:unparam
@@ -142,43 +141,6 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
142141

143142
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{})}
144143

145-
// add the url of the attachments to content
146-
if len(m.Attachments) > 0 {
147-
first := true
148-
for _, attach := range m.Attachments {
149-
if b.alwaysDownloadFiles {
150-
var url, name, caption string
151-
var data *[]byte
152-
153-
url = attach.URL
154-
name = attach.Filename
155-
156-
err = helper.HandleDownloadSize(b.Log, &rmsg, name, int64(attach.Size), b.General)
157-
if err != nil {
158-
return
159-
}
160-
data, err = helper.DownloadFile(url)
161-
if err != nil {
162-
return
163-
}
164-
165-
if first {
166-
caption = m.Content
167-
if caption == "" {
168-
caption = name
169-
}
170-
first = false
171-
} else {
172-
caption = ""
173-
}
174-
175-
helper.HandleDownloadData(b.Log, &rmsg, name, caption, "", data, b.General)
176-
} else {
177-
m.Content = m.Content + "\n" + attach.URL
178-
}
179-
}
180-
}
181-
182144
b.Log.Debugf("== Receiving event %#v", m.Message)
183145

184146
if m.Content != "" {
@@ -210,11 +172,6 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
210172
}
211173
}
212174

213-
// no empty messages
214-
if rmsg.Text == "" && len(rmsg.Extra["file"]) == 0 {
215-
return
216-
}
217-
218175
// do we have a /me action
219176
var ok bool
220177
rmsg.Text, ok = b.replaceAction(rmsg.Text)
@@ -233,9 +190,46 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
233190
rmsg.ParentID = ref.MessageID
234191
}
235192

236-
b.Log.Debugf("<= Sending message from %s on %s to gateway", m.Author.Username, b.Account)
237-
b.Log.Debugf("<= Message is %#v", rmsg)
238-
b.Remote <- rmsg
193+
// no empty messages
194+
if rmsg.Text == "" && len(m.Attachments) == 0 {
195+
return
196+
}
197+
198+
// if no attachments, send the message as-is
199+
if len(m.Attachments) == 0 {
200+
b.Log.Debugf("<= Sending message from %s on %s to gateway", m.Author.Username, b.Account)
201+
b.Log.Debugf("<= Message is %#v", rmsg)
202+
203+
b.Remote <- rmsg
204+
205+
return
206+
}
207+
208+
// We process attachments last, after all pre-processing is done
209+
// Perform the operations in the background, so we can process other
210+
// messages while we download the attachments.
211+
go func() {
212+
count := 0
213+
for _, attach := range m.Attachments {
214+
err := b.AddAttachmentFromURL(&rmsg, attach.Filename, attach.ID, "", attach.URL)
215+
if err != nil {
216+
b.Log.WithError(err).Warnf("Failed to download attachment %s", attach.Filename)
217+
continue
218+
}
219+
220+
count += 1
221+
}
222+
223+
if rmsg.Text == "" && count == 0 {
224+
b.Log.Warnf("Skipping message because there is no text and file uploads all failed")
225+
return
226+
}
227+
228+
b.Log.Debugf("<= Sending message attachments from %s on %s to gateway", m.Author.Username, b.Account)
229+
b.Log.Debugf("<= Message is %#v", rmsg)
230+
231+
b.Remote <- rmsg
232+
}()
239233
}
240234

241235
func (b *Bdiscord) memberUpdate(s *discordgo.Session, m *discordgo.GuildMemberUpdate) {

changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
- file uploading now use the new upload steps described in the slack docs via `UploadFileV2`, replacing the deprecated and now disabled `file.upload` based method (via `UploadFile`) ([#129](https://github.com/matterbridge-org/matterbridge/pull/129))
5858
- discord
5959
- attached files are always downloaded, so when the media server is enabled, URLs can stay valid
60-
longer than Discord URLs ([#37](https://github.com/matterbridge-org/matterbridge/issues/37))
60+
longer than Discord URLs ([#37](https://github.com/matterbridge-org/matterbridge/issues/37), [#114](https://github.com/matterbridge-org/matterbridge/pull/114))
6161
- irc
6262
- when there are attachments in the message, the body is now sent instead of being discarded silently ([#156](https://github.com/matterbridge-org/matterbridge/pull/156))
6363
- when an attachment has no public URL, an error message is printed/logged encouraging the

docs/protocols/discord/settings.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,6 @@ Show `#xxxx` discriminator with `UseUserName`
8282
UseDiscriminator=true
8383
```
8484

85-
## AlwaysDownloadFiles
86-
87-
Any uploads will be always downloaded as files and re-uploaded to the destination
88-
gateway or the mediaproxy if enabled instead of forwarding URLs to Discord's CDNs.
89-
By default, any file uploads from Discord gets forwarded as a list URLs to the
90-
Discord CDN.
91-
92-
Use this if direct use of Discord CDN leads inconveniences or just unavailable
93-
directly due to various reasons.
94-
95-
- Setting: **OPTIONAL**, **RELOADABLE**
96-
- Format: *boolean*
97-
- Example:
98-
```toml
99-
AlwaysDownloadFiles=true
100-
```
101-
10285
## WebhookURL
10386

10487
Specify WebhookURL. If given, will relay messages using the Webhook, which

0 commit comments

Comments
 (0)