Upload files before sending message

This commit is contained in:
Tulir Asokan
2023-01-28 15:43:16 +02:00
parent 9cf9d7c446
commit e9249d6ff9
5 changed files with 62 additions and 10 deletions

View File

@@ -41,6 +41,27 @@ func downloadDiscordAttachment(url string) ([]byte, error) {
return io.ReadAll(resp.Body)
}
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 (portal *Portal) downloadMatrixAttachment(content *event.MessageEventContent) ([]byte, error) {
var file *event.EncryptedFileInfo
rawMXC := content.URL