From 92d380aa86dfd3e60f5b8d826ec96c0fbc17614a Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 21 Feb 2020 15:12:22 +0100 Subject: Easybridge has an avatar --- connector/connector.go | 19 +++++++++++------- connector/mediaobject.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 connector/mediaobject.go (limited to 'connector') 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 "" +} -- cgit v1.2.3