-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.go
83 lines (70 loc) · 1.72 KB
/
base.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package images
import (
"bytes"
"encoding/base64"
"errors"
"net/http"
)
var typePrefix = []byte("data:image/")
var dataPrefix = []byte("base64,")
var nullBytes = []byte("null")
type Image struct {
ContentType string
Data []byte
}
func New(data []byte) (*Image, error) {
p := bytes.SplitN(data, []byte{';'}, 2)
if len(p) < 2 || !bytes.HasPrefix(p[0], typePrefix) || !bytes.HasPrefix(p[1], dataPrefix) {
return nil, errors.New("invalid image data")
}
var rawB64 = p[1][7:]
var img = Image{
ContentType: string(p[0][5:]),
Data: make([]byte, base64.StdEncoding.DecodedLen(len(rawB64))),
}
_, err := base64.StdEncoding.Decode(img.Data, rawB64)
if err != nil {
return nil, err
}
return &img, nil
}
func (i Image) Encode() ([]byte, error) {
if i.ContentType == "" {
i.ContentType = http.DetectContentType(i.Data)
if !(i.ContentType == "image/png" || i.ContentType == "image/jpeg" || i.ContentType == "image/gif") {
return nil, errors.New("invalid content type. accepts png, jpeg and gif")
}
}
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(i.Data)))
base64.StdEncoding.Encode(encoded, i.Data)
return bytes.Join([][]byte{
[]byte("data:"),
[]byte(i.ContentType),
[]byte(";base64,"),
encoded,
}, nil), nil
}
func (i *Image) UnmarshalJSON(data []byte) error {
data = data[1 : len(data)-1]
if data[0] == 'n' && data[1] == 'u' && data[2] == 'l' && data[3] == 'l' {
return nil
}
img, err := New(data)
if err != nil {
return err
}
*i = *img
return nil
}
func (i *Image) MarshalJSON() ([]byte, error) {
if len(i.Data) == 0 {
return nullBytes, nil
}
data, err := i.Encode()
if err != nil {
return nil, err
}
return bytes.Join([][]byte{
{'"'}, data, {'"'},
}, nil), nil
}