aboutsummaryrefslogtreecommitdiff
path: root/connector/marshal.go
diff options
context:
space:
mode:
Diffstat (limited to 'connector/marshal.go')
-rw-r--r--connector/marshal.go78
1 files changed, 59 insertions, 19 deletions
diff --git a/connector/marshal.go b/connector/marshal.go
index 3321fb0..065c955 100644
--- a/connector/marshal.go
+++ b/connector/marshal.go
@@ -6,18 +6,28 @@ import (
"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("join"), nil
+ return []byte(S_EVENT_JOIN), nil
case EVENT_LEAVE:
- return []byte("leave"), nil
+ return []byte(S_EVENT_LEAVE), nil
case EVENT_MESSAGE:
- return []byte("message"), nil
+ return []byte(S_EVENT_MESSAGE), nil
case EVENT_ACTION:
- return []byte("action"), nil
+ return []byte(S_EVENT_ACTION), nil
default:
return nil, fmt.Errorf("Invalid event type: %d", t)
}
@@ -25,16 +35,16 @@ func (t EventType) MarshalText() ([]byte, error) {
func (t *EventType) UnmarshalText(text []byte) error {
switch string(text) {
- case "join":
+ case S_EVENT_JOIN:
*t = EVENT_JOIN
return nil
- case "leave":
+ case S_EVENT_LEAVE:
*t = EVENT_LEAVE
return nil
- case "message":
+ case S_EVENT_MESSAGE:
*t = EVENT_MESSAGE
return nil
- case "action":
+ case S_EVENT_ACTION:
*t = EVENT_ACTION
return nil
default:
@@ -47,31 +57,40 @@ func (t *EventType) UnmarshalText(text []byte) error {
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 MediaObject(mo) == nil {
+ 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(),
}
- 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
+
+ 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())
}
- mod.Data = base64.StdEncoding.EncodeToString(buf.Bytes())
+
return json.Marshal(&mod)
}
@@ -85,6 +104,27 @@ func (mo *SMediaObject) UnmarshalJSON(jdata []byte) error {
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