2022-05-22 19:16:42 +00:00
|
|
|
package main
|
2022-02-11 10:04:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-07-03 09:58:57 +00:00
|
|
|
"fmt"
|
2022-02-11 10:04:07 +00:00
|
|
|
"image"
|
2022-05-28 20:03:24 +00:00
|
|
|
"io"
|
2022-02-11 10:04:07 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2023-01-27 23:57:44 +00:00
|
|
|
"time"
|
2022-07-03 09:58:57 +00:00
|
|
|
|
2022-02-11 10:04:07 +00:00
|
|
|
"github.com/bwmarrin/discordgo"
|
2023-01-28 01:16:33 +00:00
|
|
|
"github.com/gabriel-vasile/mimetype"
|
2022-02-11 10:04:07 +00:00
|
|
|
|
2022-04-26 03:29:35 +00:00
|
|
|
"maunium.net/go/mautrix"
|
2022-02-11 10:04:07 +00:00
|
|
|
"maunium.net/go/mautrix/appservice"
|
2023-01-27 23:57:44 +00:00
|
|
|
"maunium.net/go/mautrix/crypto/attachment"
|
2022-02-11 10:04:07 +00:00
|
|
|
"maunium.net/go/mautrix/event"
|
2023-01-29 23:35:17 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
2023-01-27 23:57:44 +00:00
|
|
|
|
|
|
|
"go.mau.fi/mautrix-discord/database"
|
2022-02-11 10:04:07 +00:00
|
|
|
)
|
|
|
|
|
2023-01-27 23:57:44 +00:00
|
|
|
func downloadDiscordAttachment(url string) ([]byte, error) {
|
2022-02-11 10:04:07 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-02-11 10:04:07 +00:00
|
|
|
|
|
|
|
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)
|
2022-02-11 10:04:07 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-29 23:35:17 +00:00
|
|
|
func downloadMatrixAttachment(intent *appservice.IntentAPI, content *event.MessageEventContent) ([]byte, error) {
|
2022-02-19 13:37:12 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-29 23:35:17 +00:00
|
|
|
data, err := intent.DownloadBytes(mxc)
|
2022-02-19 13:37:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if file != nil {
|
2022-05-22 19:16:42 +00:00
|
|
|
err = file.DecryptInPlace(data)
|
2022-02-19 13:37:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2023-01-29 23:35:17 +00:00
|
|
|
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
|
2023-01-29 23:35:17 +00:00
|
|
|
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()
|
2023-01-29 23:35:17 +00:00
|
|
|
if meta.MimeType == "" {
|
|
|
|
meta.MimeType = dbFile.MimeType
|
2023-01-28 01:16:33 +00:00
|
|
|
}
|
2023-01-29 23:35:17 +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
|
|
|
}
|
|
|
|
|
2023-01-29 23:35:17 +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"
|
|
|
|
}
|
2022-04-26 03:29:35 +00:00
|
|
|
req := mautrix.ReqUploadMedia{
|
|
|
|
ContentBytes: data,
|
2022-07-03 09:58:57 +00:00
|
|
|
ContentType: uploadMime,
|
2022-04-26 03:29:35 +00:00
|
|
|
}
|
2023-01-27 23:57:44 +00:00
|
|
|
if br.Config.Homeserver.AsyncMedia {
|
|
|
|
resp, err := intent.UnstableCreateMXC()
|
2022-04-26 03:29:35 +00:00
|
|
|
if err != nil {
|
2023-01-27 23:57:44 +00:00
|
|
|
return nil, err
|
2022-04-26 03:29:35 +00:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}()
|
2022-04-26 03:29:35 +00:00
|
|
|
} else {
|
|
|
|
uploaded, err := intent.UploadMedia(req)
|
|
|
|
if err != nil {
|
2023-01-27 23:57:44 +00:00
|
|
|
return nil, err
|
2022-04-26 03:29:35 +00:00
|
|
|
}
|
2023-01-27 23:57:44 +00:00
|
|
|
dbFile.MXC = uploaded.ContentURI
|
2022-02-11 10:04:07 +00:00
|
|
|
}
|
2023-01-27 23:57:44 +00:00
|
|
|
return dbFile, nil
|
|
|
|
}
|
2022-02-11 10:04:07 +00:00
|
|
|
|
2023-01-29 23:35:17 +00:00
|
|
|
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
|
|
|
}
|
2022-02-11 10:04:07 +00:00
|
|
|
|
2023-01-29 23:35:17 +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
|
|
|
|
}
|
2023-01-29 23:35:17 +00:00
|
|
|
// 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
|
2022-02-11 10:04:07 +00:00
|
|
|
}
|
2023-01-29 23:35:17 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|