aboutsummaryrefslogtreecommitdiff
path: root/connector/marshal.go
blob: 065c955afa1e1ad72e04f6139c592bd94a966c51 (plain) (blame)
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
package connector

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"strings"
)

const (
	S_EVENT_JOIN    = "join"
	S_EVENT_LEAVE   = "leave"
	S_EVENT_MESSAGE = "message"
	S_EVENT_ACTION  = "action"
)

func (t EventType) MarshalText() ([]byte, error) {
	switch t {
	case EVENT_JOIN:
		return []byte(S_EVENT_JOIN), nil
	case EVENT_LEAVE:
		return []byte(S_EVENT_LEAVE), nil
	case EVENT_MESSAGE:
		return []byte(S_EVENT_MESSAGE), nil
	case EVENT_ACTION:
		return []byte(S_EVENT_ACTION), nil
	default:
		return nil, fmt.Errorf("Invalid event type: %d", t)
	}
}

func (t *EventType) UnmarshalText(text []byte) error {
	switch string(text) {
	case S_EVENT_JOIN:
		*t = EVENT_JOIN
		return nil
	case S_EVENT_LEAVE:
		*t = EVENT_LEAVE
		return nil
	case S_EVENT_MESSAGE:
		*t = EVENT_MESSAGE
		return nil
	case S_EVENT_ACTION:
		*t = EVENT_ACTION
		return nil
	default:
		return fmt.Errorf("Invalid event type: %s", string(text))
	}
}

// ----

type MediaObjectJSON struct {
	Filename  string     `json:"filename"`
	Mimetype  string     `json:"mime_type"`
	Size      int64      `json:"size"`
	ImageSize *ImageSize `json:"image_size"`
	Data      string     `json:"data"`
	URL       string     `json:"url"`
}

func (mo SMediaObject) MarshalJSON() ([]byte, error) {
	if mo.MediaObject == nil {
		return []byte("null"), nil
	}

	mod := MediaObjectJSON{
		Filename:  mo.Filename(),
		Mimetype:  mo.Mimetype(),
		Size:      mo.Size(),
		ImageSize: mo.ImageSize(),
		URL:       mo.URL(),
	}

	if mod.URL == "" {
		// If we don't have a URL, the only way is to pass the blob itself
		rd, err := mo.Read()
		if err != nil {
			return nil, err
		}
		defer rd.Close()
		buf := bytes.NewBuffer([]byte{})
		_, err = io.Copy(buf, rd)
		if err != nil {
			return nil, err
		}
		mod.Data = base64.StdEncoding.EncodeToString(buf.Bytes())
	}

	return json.Marshal(&mod)
}

func (mo *SMediaObject) UnmarshalJSON(jdata []byte) error {
	if string(jdata) == "null" {
		return nil
	}

	var d MediaObjectJSON
	err := json.Unmarshal(jdata, &d)
	if err != nil {
		return err
	}

	if d.URL != "" {
		*mo = SMediaObject{&LazyBlobMediaObject{
			ObjectFilename:  d.Filename,
			ObjectMimetype:  d.Mimetype,
			ObjectImageSize: d.ImageSize,
			GetFn: func(o *LazyBlobMediaObject) error {
				resp, err := http.Get(d.URL)
				if err != nil {
					return err
				}
				if o.ObjectMimetype == "" {
					o.ObjectMimetype = strings.Join(resp.Header["Content-Type"], "")
				}
				o.ObjectData, err = ioutil.ReadAll(resp.Body)
				return err
			},
		}}
		return nil
	}

	bytes, err := base64.StdEncoding.DecodeString(d.Data)
	if err != nil {
		return err
	}
	*mo = SMediaObject{&BlobMediaObject{
		ObjectFilename:  d.Filename,
		ObjectMimetype:  d.Mimetype,
		ObjectImageSize: d.ImageSize,
		ObjectData:      bytes,
	}}
	return nil
}