aboutsummaryrefslogtreecommitdiff
path: root/connector
diff options
context:
space:
mode:
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 ""
+}