From 1efe40add50a76b7a0ad95187f6034cc75845d16 Mon Sep 17 00:00:00 2001 From: Wim Date: Fri, 24 Nov 2017 22:36:19 +0100 Subject: Add initial support for an external mediaserver. #278 Add 2 extra options `MediaServerUpload` and `MediaServerDownload`, where the URL for upload and download can be specified. See https://github.com/42wim/matterbridge/wiki/Mediaserver-setup-%5Badvanced%5D for an example with caddy --- gateway/gateway.go | 31 +++++++++++++++++++++++++++++++ gateway/router.go | 1 + 2 files changed, 32 insertions(+) (limited to 'gateway') diff --git a/gateway/gateway.go b/gateway/gateway.go index e5d74ff2..8b1223f0 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -1,13 +1,16 @@ package gateway import ( + "bytes" "fmt" "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/config" log "github.com/Sirupsen/logrus" // "github.com/davecgh/go-spew/spew" + "crypto/sha1" "github.com/hashicorp/golang-lru" "github.com/peterhellberg/emojilib" + "net/http" "regexp" "strings" "time" @@ -318,6 +321,34 @@ func (gw *Gateway) modifyMessage(msg *config.Message) { msg.Gateway = gw.Name } +func (gw *Gateway) handleFiles(msg *config.Message) { + if msg.Extra == nil || gw.Config.General.MediaServerUpload == "" { + return + } + if len(msg.Extra["file"]) > 0 { + client := &http.Client{ + Timeout: time.Second * 5, + } + for i, f := range msg.Extra["file"] { + fi := f.(config.FileInfo) + sha1sum := fmt.Sprintf("%x", sha1.Sum(*fi.Data)) + reader := bytes.NewReader(*fi.Data) + url := gw.Config.General.MediaServerUpload + "/" + sha1sum + "/" + fi.Name + durl := gw.Config.General.MediaServerDownload + "/" + sha1sum + "/" + fi.Name + extra := msg.Extra["file"][i].(config.FileInfo) + extra.URL = durl + msg.Extra["file"][i] = extra + req, _ := http.NewRequest("PUT", url, reader) + req.Header.Set("Content-Type", "binary/octet-stream") + _, err := client.Do(req) + if err != nil { + log.Errorf("mediaserver upload failed: %#v", err) + } + log.Debugf("mediaserver download URL = %s", durl) + } + } +} + func getChannelID(msg config.Message) string { return msg.Channel + msg.Account } diff --git a/gateway/router.go b/gateway/router.go index 28f81335..192fa9a3 100644 --- a/gateway/router.go +++ b/gateway/router.go @@ -99,6 +99,7 @@ func (r *Router) handleReceive() { if !gw.ignoreMessage(&msg) { msg.Timestamp = time.Now() gw.modifyMessage(&msg) + gw.handleFiles(&msg) for _, br := range gw.Bridges { msgIDs = append(msgIDs, gw.handleMessage(msg, br)...) } -- cgit v1.2.3