diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 3f99da0c..2c1a69d2 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -380,26 +380,51 @@ func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (st // handleUploadFile handles native upload of files func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (string, error) { for _, f := range msg.Extra["file"] { + var err error fi := f.(config.FileInfo) - file := discordgo.File{ - Name: fi.Name, - ContentType: "", - Reader: bytes.NewReader(*fi.Data), - } - m := discordgo.MessageSend{ - Content: msg.Username + fi.Comment, - Files: []*discordgo.File{&file}, - AllowedMentions: b.getAllowedMentions(), - } - res, err := b.c.ChannelMessageSendComplex(channelID, &m) - if err != nil { - return "", fmt.Errorf("file upload failed: %s", err) + + if fi.URL != "" { + err = b.handleUploadFileFromURL(msg, channelID, &fi) + } else if fi.Data != nil { + err = b.handleUploadFileFromData(msg, channelID, &fi) + } else { + b.Log.Errorf("Attachment %#v for message %#v had neither Data nor URL", fi, msg) } - // link file_upload_nativeID (file ID from the original bridge) to our upload id - // so that we can remove this later when it eg needs to be deleted - b.cache.Add(cFileUpload+fi.NativeID, res.ID) + if err != nil { + b.Log.Errorf("Could not send attachment %#v for message %#v: %s", fi, msg, err) + return "", err + } } return "", nil } + +func (b *Bdiscord) handleUploadFileFromData(msg *config.Message, channelID string, fi *config.FileInfo) error { + file := discordgo.File{ + Name: fi.Name, + ContentType: "", + Reader: bytes.NewReader(*fi.Data), + } + m := discordgo.MessageSend{ + Content: msg.Username + fi.Comment, + Files: []*discordgo.File{&file}, + AllowedMentions: b.getAllowedMentions(), + } + res, err := b.c.ChannelMessageSendComplex(channelID, &m) + if err != nil { + return fmt.Errorf("file upload failed: %s", err) + } + // link file_upload_nativeID (file ID from the original bridge) to our upload id + // so that we can remove this later when it eg needs to be deleted + b.cache.Add(cFileUpload+fi.NativeID, res.ID) + return nil +} + +func (b *Bdiscord) handleUploadFileFromURL(msg *config.Message, channelID string, fi *config.FileInfo) error { + _, err := b.c.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{ + Content: fi.URL, + AllowedMentions: b.getAllowedMentions(), + }) + return err +} diff --git a/bridge/discord/webhook.go b/bridge/discord/webhook.go index ef129d4c..2e7227eb 100644 --- a/bridge/discord/webhook.go +++ b/bridge/discord/webhook.go @@ -69,33 +69,63 @@ func (b *Bdiscord) webhookSendTextOnly(msg *config.Message, channelID string) (s func (b *Bdiscord) webhookSendFilesOnly(msg *config.Message, channelID string) error { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) //nolint:forcetypeassert - file := discordgo.File{ - Name: fi.Name, - ContentType: "", - Reader: bytes.NewReader(*fi.Data), - } - content := fi.Comment + var err error + + if fi.URL != "" { + err = b.webhookSendFileFromURL(msg, channelID, &fi) + } else if fi.Data != nil { + err = b.webhookSendFileFromData(msg, channelID, &fi) + } else { + b.Log.Errorf("Attachment %#v for message %#v had neither Data nor URL", fi, msg) + } - // Cannot use the resulting ID for any edits anyway, so throw it away. - // This has to be re-enabled when we implement message deletion. - _, err := b.transmitter.Send( - channelID, - &discordgo.WebhookParams{ - Username: msg.Username, - AvatarURL: msg.Avatar, - Files: []*discordgo.File{&file}, - Content: content, - AllowedMentions: b.getAllowedMentions(), - }, - ) if err != nil { - b.Log.Errorf("Could not send file %#v for message %#v: %s", file, msg, err) + b.Log.Errorf("Could not send attachment %#v for message %#v: %s", fi, msg, err) return err } } + return nil } +func (b *Bdiscord) webhookSendFileFromData(msg *config.Message, channelID string, fi *config.FileInfo) error { + file := discordgo.File{ + Name: fi.Name, + ContentType: "", + Reader: bytes.NewReader(*fi.Data), + } + content := fi.Comment + + // Cannot use the resulting ID for any edits anyway, so throw it away. + // This has to be re-enabled when we implement message deletion. + _, err := b.transmitter.Send( + channelID, + &discordgo.WebhookParams{ + Username: msg.Username, + AvatarURL: msg.Avatar, + Files: []*discordgo.File{&file}, + Content: content, + AllowedMentions: b.getAllowedMentions(), + }, + ) + return err +} + +func (b *Bdiscord) webhookSendFileFromURL(msg *config.Message, channelID string, fi *config.FileInfo) error { + // discord client will display any file url as an inline embed, + // without us having to do anything special. + _, err := b.transmitter.Send( + channelID, + &discordgo.WebhookParams{ + Content: fi.URL, + Username: msg.Username, + AvatarURL: msg.Avatar, + AllowedMentions: b.getAllowedMentions(), + }, + ) + return err +} + // webhookSend send one or more message via webhook, taking care of file // uploads (from slack, telegram or mattermost). // Returns messageID and error.