-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrevisit_msg.go
156 lines (132 loc) · 3.94 KB
/
revisit_msg.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package gorevisit
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"image"
"io"
"io/ioutil"
"os"
"strings"
)
// ImageData holds a reference the data URI of image data in a Revisit.link message
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs
type ImageData struct {
Data string `json:"data"`
}
// byteReader returns an io.Reader for the image data in a Revisit message
func (i *ImageData) byteReader() io.Reader {
dataUri := i.Data
data := strings.Split(dataUri, ",")[1]
return base64.NewDecoder(base64.StdEncoding, strings.NewReader(data))
}
// AudioData holds a reference to the data URI of sound data in a Revisit.link message
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs
type AudioData struct {
Data string `json:data"`
}
// MetaData wraps the Audio data of a Revisit.link message as per the specification
// See: http://revisit.link/spec.html
type MetaData struct {
Audio AudioData `json:"audio"`
}
// RevisitMsg holds a decoded Revisit.link message
// See: http://revisit.link/spec.html
type RevisitMsg struct {
Content ImageData `json:"content"`
Meta MetaData `json:"meta"`
}
// NewRevisitMsgFromReaders given an io.Reader containing an image file
// and optional io.Reader containing a sound file, returns a *RevisitMsg
func NewRevisitMsgFromReaders(readers ...io.Reader) (*RevisitMsg, error) {
if len(readers) < 1 || len(readers) > 2 {
return nil, errors.New("must have image buffer, may have audio buffer")
}
imageBytes, err := ioutil.ReadAll(readers[0])
if err != nil {
return nil, err
}
_, format, err := image.Decode(bytes.NewBuffer(imageBytes))
if err != nil {
return &RevisitMsg{}, err
}
imageDataURI := bytesToDataURI(imageBytes, fmt.Sprintf("image/%s", format))
var soundDataURI string
// if we have sound info get it
if len(readers) == 2 {
soundBytes, _ := ioutil.ReadAll(readers[1])
if err != nil {
return nil, err
}
// FIXME: add sound type detection instead of hard coded ogg
soundDataURI = bytesToDataURI(soundBytes, "audio/ogg")
}
content := &ImageData{
Data: imageDataURI,
}
audioContent := &AudioData{
Data: soundDataURI,
}
metaContent := &MetaData{
Audio: *audioContent,
}
revisitMsg := &RevisitMsg{
Content: *content,
Meta: *metaContent,
}
return revisitMsg, nil
}
// NewRevisitMsgFromFiles given the path to an image file and optional
// path to an audio file, creates a JSON encoded Revisit.link message
func NewRevisitMsgFromFiles(mediaPath ...string) (*RevisitMsg, error) {
if len(mediaPath) < 1 || len(mediaPath) > 2 {
return &RevisitMsg{}, errors.New("must have image, may have audio")
}
iFile, err := os.Open(mediaPath[0])
if err != nil {
return nil, err
}
var revisitMsg *RevisitMsg
// if we have sound info get it
if len(mediaPath) == 2 {
sFile, err := os.Open(mediaPath[1])
if err != nil {
return nil, err
}
revisitMsg, err = NewRevisitMsgFromReaders(iFile, sFile)
if err != nil {
return nil, err
}
} else {
revisitMsg, err = NewRevisitMsgFromReaders(iFile)
if err != nil {
return nil, err
}
}
return revisitMsg, nil
}
// IsValidSize makes sure the size of the media in a RevisitMsg matches the spec
func (r *RevisitMsg) IsValidSize() bool {
if len(r.Content.Data) > 1048576 || len(r.Meta.Audio.Data) > 1048576 {
return false
} else {
return true
}
}
// ImageByteReader returns an io.Reader for the image data in a Revisit message
func (r *RevisitMsg) ImageByteReader() io.Reader {
return r.Content.byteReader()
}
// ImageType gets the type of image that is in the message
func (r *RevisitMsg) ImageType() string {
header := strings.Split(r.Content.Data, ",")[0]
subheader := strings.Split(header, ":")[1]
return strings.Split(subheader, ";")[0]
}
// bytesToDataURI given a byte array and a content type,
// creates a Data URI of the content
func bytesToDataURI(data []byte, contentType string) string {
return fmt.Sprintf("data:%s;base64,%s",
contentType, base64.StdEncoding.EncodeToString(data))
}