mautrix-discord/attachments.go

197 lines
4.6 KiB
Go
Raw Normal View History

package main
import (
"bytes"
2022-07-03 09:58:57 +00:00
"fmt"
"image"
2022-05-28 20:03:24 +00:00
"io"
"net/http"
"strings"
2023-01-27 23:57:44 +00:00
"time"
2022-07-03 09:58:57 +00:00
"github.com/bwmarrin/discordgo"
2023-01-28 01:16:33 +00:00
"github.com/gabriel-vasile/mimetype"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/appservice"
2023-01-27 23:57:44 +00:00
"maunium.net/go/mautrix/crypto/attachment"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
2023-01-27 23:57:44 +00:00
"go.mau.fi/mautrix-discord/database"
)
2023-01-27 23:57:44 +00:00
func downloadDiscordAttachment(url string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
2022-05-28 20:03:24 +00:00
for key, value := range discordgo.DroidDownloadHeaders {
req.Header.Set(key, value)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
2022-07-03 09:58:57 +00:00
if resp.StatusCode > 300 {
data, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("unexpected status %d: %s", resp.StatusCode, data)
}
2022-05-28 20:03:24 +00:00
return io.ReadAll(resp.Body)
}
2023-01-28 13:43:16 +00:00
func uploadDiscordAttachment(url string, data []byte) error {
req, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(data))
if err != nil {
return err
}
for key, value := range discordgo.DroidFetchHeaders {
req.Header.Set(key, value)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode > 300 {
respData, _ := io.ReadAll(resp.Body)
return fmt.Errorf("unexpected status %d: %s", resp.StatusCode, respData)
}
return nil
}
func downloadMatrixAttachment(intent *appservice.IntentAPI, content *event.MessageEventContent) ([]byte, error) {
var file *event.EncryptedFileInfo
rawMXC := content.URL
if content.File != nil {
file = content.File
rawMXC = file.URL
}
mxc, err := rawMXC.Parse()
if err != nil {
return nil, err
}
data, err := intent.DownloadBytes(mxc)
if err != nil {
return nil, err
}
if file != nil {
err = file.DecryptInPlace(data)
if err != nil {
return nil, err
}
}
return data, nil
}
func (br *DiscordBridge) uploadMatrixAttachment(intent *appservice.IntentAPI, data []byte, url string, encrypt bool, meta AttachmentMeta) (*database.File, error) {
2023-01-27 23:57:44 +00:00
dbFile := br.DB.File.New()
dbFile.Timestamp = time.Now()
dbFile.URL = url
dbFile.ID = meta.AttachmentID
dbFile.EmojiName = meta.EmojiName
2023-01-27 23:57:44 +00:00
dbFile.Size = len(data)
2023-01-28 11:11:35 +00:00
dbFile.MimeType = mimetype.Detect(data).String()
if meta.MimeType == "" {
meta.MimeType = dbFile.MimeType
2023-01-28 01:16:33 +00:00
}
if strings.HasPrefix(meta.MimeType, "image/") {
2022-07-03 09:58:57 +00:00
cfg, _, _ := image.DecodeConfig(bytes.NewReader(data))
2023-01-27 23:57:44 +00:00
dbFile.Width = cfg.Width
dbFile.Height = cfg.Height
2022-07-03 09:58:57 +00:00
}
uploadMime := meta.MimeType
2023-01-27 23:57:44 +00:00
if encrypt {
dbFile.Encrypted = true
dbFile.DecryptionInfo = attachment.NewEncryptedFile()
dbFile.DecryptionInfo.EncryptInPlace(data)
2022-07-03 09:58:57 +00:00
uploadMime = "application/octet-stream"
}
req := mautrix.ReqUploadMedia{
ContentBytes: data,
2022-07-03 09:58:57 +00:00
ContentType: uploadMime,
}
2023-01-27 23:57:44 +00:00
if br.Config.Homeserver.AsyncMedia {
resp, err := intent.UnstableCreateMXC()
if err != nil {
2023-01-27 23:57:44 +00:00
return nil, err
}
2023-01-27 23:57:44 +00:00
dbFile.MXC = resp.ContentURI
req.UnstableMXC = resp.ContentURI
req.UploadURL = resp.UploadURL
go func() {
_, err = intent.UploadMedia(req)
if err != nil {
br.Log.Errorfln("Failed to upload %s: %v", req.UnstableMXC, err)
dbFile.Delete()
}
}()
} else {
uploaded, err := intent.UploadMedia(req)
if err != nil {
2023-01-27 23:57:44 +00:00
return nil, err
}
2023-01-27 23:57:44 +00:00
dbFile.MXC = uploaded.ContentURI
}
2023-01-27 23:57:44 +00:00
return dbFile, nil
}
type AttachmentMeta struct {
AttachmentID string
MimeType string
EmojiName string
}
func (br *DiscordBridge) copyAttachmentToMatrix(intent *appservice.IntentAPI, url string, encrypt bool, meta *AttachmentMeta) (*database.File, error) {
2023-01-27 23:57:44 +00:00
dbFile := br.DB.File.Get(url, encrypt)
if dbFile == nil {
data, err := downloadDiscordAttachment(url)
if err != nil {
return nil, err
2022-07-03 09:58:57 +00:00
}
if meta == nil {
meta = &AttachmentMeta{}
}
dbFile, err = br.uploadMatrixAttachment(intent, data, url, encrypt, *meta)
2023-01-27 23:57:44 +00:00
if err != nil {
return nil, err
}
// TODO add option to cache encrypted files too?
if !dbFile.Encrypted {
dbFile.Insert(nil)
}
2023-01-27 23:57:44 +00:00
}
return dbFile, nil
}
func (portal *Portal) getEmojiMXCByDiscordID(emojiID, name string, animated bool) id.ContentURI {
var url, mimeType string
if animated {
url = discordgo.EndpointEmojiAnimated(emojiID)
mimeType = "image/gif"
} else {
url = discordgo.EndpointEmoji(emojiID)
mimeType = "image/png"
}
dbFile, err := portal.bridge.copyAttachmentToMatrix(portal.MainIntent(), url, false, &AttachmentMeta{
AttachmentID: emojiID,
MimeType: mimeType,
EmojiName: name,
})
if err != nil {
portal.log.Warnfln("Failed to download emoji %s from discord: %v", emojiID, err)
return id.ContentURI{}
}
return dbFile.MXC
}