-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia_helpers.go
More file actions
64 lines (59 loc) · 1.87 KB
/
media_helpers.go
File metadata and controls
64 lines (59 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package agentremote
import (
"context"
"encoding/base64"
"errors"
"io"
"net/http"
"os"
"strings"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
// DownloadMediaBytes downloads media from a Matrix content URI and returns the raw bytes and detected MIME type.
func DownloadMediaBytes(ctx context.Context, login *bridgev2.UserLogin, mediaURL string, encFile *event.EncryptedFileInfo, maxBytes int64) ([]byte, string, error) {
if strings.TrimSpace(mediaURL) == "" {
return nil, "", errors.New("missing media URL")
}
if login == nil || login.Bridge == nil || login.Bridge.Bot == nil {
return nil, "", errors.New("bridge is unavailable")
}
var data []byte
errMediaTooLarge := errors.New("media exceeds max size")
err := login.Bridge.Bot.DownloadMediaToFile(ctx, id.ContentURIString(mediaURL), encFile, false, func(f *os.File) error {
var reader io.Reader = f
if maxBytes > 0 {
reader = io.LimitReader(f, maxBytes+1)
}
var err error
data, err = io.ReadAll(reader)
if err != nil {
return err
}
if maxBytes > 0 && int64(len(data)) > maxBytes {
return errMediaTooLarge
}
return nil
})
if err != nil {
return nil, "", err
}
return data, http.DetectContentType(data), nil
}
// DownloadAndEncodeMedia downloads media from a Matrix content URI, enforces an
// optional size limit, and returns the base64-encoded content.
func DownloadAndEncodeMedia(ctx context.Context, login *bridgev2.UserLogin, mediaURL string, encFile *event.EncryptedFileInfo, maxMB int) (string, string, error) {
maxBytes := int64(0)
if maxMB > 0 {
maxBytes = int64(maxMB) * 1024 * 1024
}
data, mimeType, err := DownloadMediaBytes(ctx, login, mediaURL, encFile, maxBytes)
if err != nil {
return "", "", err
}
if mimeType == "" {
mimeType = "application/octet-stream"
}
return base64.StdEncoding.EncodeToString(data), mimeType, nil
}