-
Notifications
You must be signed in to change notification settings - Fork 35
/
icon.go
60 lines (48 loc) · 1.15 KB
/
icon.go
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
package discord
import (
"encoding/base64"
"fmt"
"io"
"github.com/disgoorg/disgo/json"
)
type IconType string
const (
IconTypeJPEG IconType = "image/jpeg"
IconTypePNG IconType = "image/png"
IconTypeWEBP IconType = "image/webp"
IconTypeGIF IconType = "image/gif"
IconTypeUnknown = IconTypeJPEG
)
func (t IconType) GetMIME() string {
return string(t)
}
func (t IconType) GetHeader() string {
return "data:" + string(t) + ";base64"
}
var _ json.Marshaler = (*Icon)(nil)
var _ fmt.Stringer = (*Icon)(nil)
func NewIcon(iconType IconType, reader io.Reader) (*Icon, error) {
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
return NewIconRaw(iconType, data), nil
}
func NewIconRaw(iconType IconType, src []byte) *Icon {
data := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
base64.StdEncoding.Encode(data, src)
return &Icon{Type: iconType, Data: data}
}
type Icon struct {
Type IconType
Data []byte
}
func (i Icon) MarshalJSON() ([]byte, error) {
return json.Marshal(i.String())
}
func (i Icon) String() string {
if len(i.Data) == 0 {
return ""
}
return i.Type.GetHeader() + "," + string(i.Data)
}