2020-02-29 17:30:43 +00:00
|
|
|
package connector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-02-29 19:47:44 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
S_EVENT_JOIN = "join"
|
|
|
|
S_EVENT_LEAVE = "leave"
|
|
|
|
S_EVENT_MESSAGE = "message"
|
|
|
|
S_EVENT_ACTION = "action"
|
2020-02-29 17:30:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (t EventType) MarshalText() ([]byte, error) {
|
|
|
|
switch t {
|
|
|
|
case EVENT_JOIN:
|
2020-02-29 19:47:44 +00:00
|
|
|
return []byte(S_EVENT_JOIN), nil
|
2020-02-29 17:30:43 +00:00
|
|
|
case EVENT_LEAVE:
|
2020-02-29 19:47:44 +00:00
|
|
|
return []byte(S_EVENT_LEAVE), nil
|
2020-02-29 17:30:43 +00:00
|
|
|
case EVENT_MESSAGE:
|
2020-02-29 19:47:44 +00:00
|
|
|
return []byte(S_EVENT_MESSAGE), nil
|
2020-02-29 17:30:43 +00:00
|
|
|
case EVENT_ACTION:
|
2020-02-29 19:47:44 +00:00
|
|
|
return []byte(S_EVENT_ACTION), nil
|
2020-02-29 17:30:43 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid event type: %d", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *EventType) UnmarshalText(text []byte) error {
|
|
|
|
switch string(text) {
|
2020-02-29 19:47:44 +00:00
|
|
|
case S_EVENT_JOIN:
|
2020-02-29 17:30:43 +00:00
|
|
|
*t = EVENT_JOIN
|
|
|
|
return nil
|
2020-02-29 19:47:44 +00:00
|
|
|
case S_EVENT_LEAVE:
|
2020-02-29 17:30:43 +00:00
|
|
|
*t = EVENT_LEAVE
|
|
|
|
return nil
|
2020-02-29 19:47:44 +00:00
|
|
|
case S_EVENT_MESSAGE:
|
2020-02-29 17:30:43 +00:00
|
|
|
*t = EVENT_MESSAGE
|
|
|
|
return nil
|
2020-02-29 19:47:44 +00:00
|
|
|
case S_EVENT_ACTION:
|
2020-02-29 17:30:43 +00:00
|
|
|
*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"`
|
2020-02-29 19:47:44 +00:00
|
|
|
Size int64 `json:"size"`
|
2020-02-29 17:30:43 +00:00
|
|
|
ImageSize *ImageSize `json:"image_size"`
|
|
|
|
Data string `json:"data"`
|
2020-02-29 19:47:44 +00:00
|
|
|
URL string `json:"url"`
|
2020-02-29 17:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mo SMediaObject) MarshalJSON() ([]byte, error) {
|
2020-02-29 19:47:44 +00:00
|
|
|
if mo.MediaObject == nil {
|
2020-02-29 17:30:43 +00:00
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mod := MediaObjectJSON{
|
|
|
|
Filename: mo.Filename(),
|
|
|
|
Mimetype: mo.Mimetype(),
|
2020-02-29 19:47:44 +00:00
|
|
|
Size: mo.Size(),
|
2020-02-29 17:30:43 +00:00
|
|
|
ImageSize: mo.ImageSize(),
|
2020-02-29 19:47:44 +00:00
|
|
|
URL: mo.URL(),
|
2020-02-29 17:30:43 +00:00
|
|
|
}
|
2020-02-29 19:47:44 +00:00
|
|
|
|
|
|
|
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())
|
2020-02-29 17:30:43 +00:00
|
|
|
}
|
2020-02-29 19:47:44 +00:00
|
|
|
|
2020-02-29 17:30:43 +00:00
|
|
|
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
|
|
|
|
}
|
2020-02-29 19:47:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-29 17:30:43 +00:00
|
|
|
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
|
|
|
|
}
|