aboutsummaryrefslogtreecommitdiff
path: root/connector
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-02-21 15:12:22 +0100
committerAlex Auvolat <alex@adnab.me>2020-02-21 15:12:22 +0100
commit92d380aa86dfd3e60f5b8d826ec96c0fbc17614a (patch)
treec5edf0e60ee308ba49a0aa526fa5ad184f289f2f /connector
parenta4cf41536fc8eef743cd330b86e784079100ec6c (diff)
downloadeasybridge-92d380aa86dfd3e60f5b8d826ec96c0fbc17614a.tar.gz
easybridge-92d380aa86dfd3e60f5b8d826ec96c0fbc17614a.zip
Easybridge has an avatar
Diffstat (limited to 'connector')
-rw-r--r--connector/connector.go19
-rw-r--r--connector/mediaobject.go50
2 files changed, 62 insertions, 7 deletions
diff --git a/connector/connector.go b/connector/connector.go
index 5df010a..d05f4a2 100644
--- a/connector/connector.go
+++ b/connector/connector.go
@@ -1,5 +1,9 @@
package connector
+import (
+ "io"
+)
+
/*
A generic connector framework for instant messaging protocols.
@@ -126,13 +130,14 @@ type RoomInfo struct {
}
type MediaObject interface {
- Size() int
- MimeType() string
+ Filename() string
+ Size() int64
+ Mimetype() string
- // AsBytes: must always be implemented
- AsBytes() ([]byte, error)
+ // Read: must always be implemented
+ Read() (io.ReadCloser, error)
- // AsString: not mandatory, may return an empty string
- // If so, AsBytes() is the only way to retrieve the object
- AsURL() string
+ // URL(): not mandatory, may return an empty string
+ // If so, Read() is the only way to retrieve the object
+ URL() string
}
diff --git a/connector/mediaobject.go b/connector/mediaobject.go
new file mode 100644
index 0000000..75635ee
--- /dev/null
+++ b/connector/mediaobject.go
@@ -0,0 +1,50 @@
+package connector
+
+import (
+ "io"
+ "net/http"
+ "os"
+ "path/filepath"
+
+ log "github.com/sirupsen/logrus"
+)
+
+type FileMediaObject struct {
+ Path string
+}
+
+func (m *FileMediaObject) Filename() string {
+ return filepath.Base(m.Path)
+}
+
+func (m *FileMediaObject) Size() int64 {
+ fi, err := os.Stat(m.Path)
+ if err != nil {
+ log.Fatal(err)
+ }
+ return fi.Size()
+}
+
+func (m *FileMediaObject) Mimetype() string {
+ f, err := os.Open(m.Path)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer f.Close()
+
+ buffer := make([]byte, 512)
+ _, err = f.Read(buffer)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ return http.DetectContentType(buffer)
+}
+
+func (m *FileMediaObject) Read() (io.ReadCloser, error) {
+ return os.Open(m.Path)
+}
+
+func (m *FileMediaObject) URL() string {
+ return ""
+}