From a67c04b2fae4f56c135ab3a3cf61ea918c401a3f Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Mon, 4 Jun 2018 21:32:09 +0200 Subject: [PATCH] Add config option MediaUploadPath MediaServerPath can be used instead of MediaServerUpload, for when your webserver is on the same system as matterbridge and matterbridge has write access to the serve dir. --- bridge/config/config.go | 10 +++--- gateway/gateway.go | 66 ++++++++++++++++++++++++++++------------ matterbridge.go | 5 +-- matterbridge.toml.sample | 8 +++-- 4 files changed, 62 insertions(+), 27 deletions(-) diff --git a/bridge/config/config.go b/bridge/config/config.go index 1627adc2..8368bc15 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -2,14 +2,15 @@ package config import ( "bytes" - "github.com/fsnotify/fsnotify" - log "github.com/sirupsen/logrus" - "github.com/spf13/viper" - prefixed "github.com/x-cray/logrus-prefixed-formatter" "os" "strings" "sync" "time" + + "github.com/fsnotify/fsnotify" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" + prefixed "github.com/x-cray/logrus-prefixed-formatter" ) const ( @@ -76,6 +77,7 @@ type Protocol struct { MediaDownloadSize int // all protocols MediaServerDownload string MediaServerUpload string + MediaServerPath string // Basically MediaServerUpload, but instead of uploading it, just write it to a file on the same server. MessageDelay int // IRC, time in millisecond to wait between messages MessageFormat string // telegram MessageLength int // IRC, max length of a message allowed diff --git a/gateway/gateway.go b/gateway/gateway.go index eb5bbe92..f93ccb57 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -3,6 +3,10 @@ package gateway import ( "bytes" "fmt" + "io/ioutil" + "net/http" + "os" + "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/api" "github.com/42wim/matterbridge/bridge/config" @@ -18,16 +22,16 @@ import ( "github.com/42wim/matterbridge/bridge/telegram" "github.com/42wim/matterbridge/bridge/xmpp" "github.com/42wim/matterbridge/bridge/zulip" + "github.com/hashicorp/golang-lru" log "github.com/sirupsen/logrus" // "github.com/davecgh/go-spew/spew" "crypto/sha1" - "github.com/hashicorp/golang-lru" - "github.com/peterhellberg/emojilib" - "net/http" "path/filepath" "regexp" "strings" "time" + + "github.com/peterhellberg/emojilib" ) type Gateway struct { @@ -414,7 +418,7 @@ func (gw *Gateway) modifyMessage(msg *config.Message) { func (gw *Gateway) handleFiles(msg *config.Message) { reg := regexp.MustCompile("[^a-zA-Z0-9]+") // if we don't have a attachfield or we don't have a mediaserver configured return - if msg.Extra == nil || gw.Config.General.MediaServerUpload == "" { + if msg.Extra == nil || (gw.Config.General.MediaServerUpload == "" && gw.Config.General.MediaServerPath == "") { return } @@ -430,26 +434,50 @@ func (gw *Gateway) handleFiles(msg *config.Message) { fi.Name = reg.ReplaceAllString(fi.Name, "_") fi.Name = fi.Name + ext 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 + if gw.Config.General.MediaServerUpload != "" { + url := gw.Config.General.MediaServerUpload + "/" + sha1sum + "/" + fi.Name + + req, err := http.NewRequest("PUT", url, bytes.NewReader(*fi.Data)) + if err != nil { + flog.Errorf("mediaserver upload failed, could not create request: %#v", err) + continue + } + + flog.Debugf("mediaserver upload url: %s", url) + + req.Header.Set("Content-Type", "binary/octet-stream") + _, err = client.Do(req) + if err != nil { + flog.Errorf("mediaserver upload failed, could not Do request: %#v", err) + continue + } + } else { + dir := gw.Config.General.MediaServerPath + "/" + sha1sum + err := os.Mkdir(dir, os.ModePerm) + if err != nil && !os.IsExist(err) { + flog.Errorf("mediaserver path failed, could not mkdir: %s %#v", err, err) + continue + } + + path := dir + "/" + fi.Name + flog.Debugf("mediaserver path placing file: %s", path) + + err = ioutil.WriteFile(path, *fi.Data, os.ModePerm) + if err != nil { + flog.Errorf("mediaserver path failed, could not writefile: %s %#v", err, err) + continue + } + } + + // We uploaded/placed the file successfully. Add the SHA and URL. extra := msg.Extra["file"][i].(config.FileInfo) extra.URL = durl - req, err := http.NewRequest("PUT", url, reader) - if err != nil { - flog.Errorf("mediaserver upload failed: %#v", err) - continue - } - req.Header.Set("Content-Type", "binary/octet-stream") - _, err = client.Do(req) - if err != nil { - flog.Errorf("mediaserver upload failed: %#v", err) - continue - } - flog.Debugf("mediaserver download URL = %s", durl) - // we uploaded the file successfully. Add the SHA extra.SHA = sha1sum msg.Extra["file"][i] = extra + + flog.Debugf("mediaserver download URL = %s", durl) } } } diff --git a/matterbridge.go b/matterbridge.go index f05a745b..df138028 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -3,13 +3,14 @@ package main import ( "flag" "fmt" + "os" + "strings" + "github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/gateway" "github.com/google/gops/agent" log "github.com/sirupsen/logrus" prefixed "github.com/x-cray/logrus-prefixed-formatter" - "os" - "strings" ) var ( diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index bf9e147b..159f0987 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -1305,10 +1305,12 @@ RemoteNickFormat="[{PROTOCOL}] <{NICK}> " StripNick=false -#MediaServerUpload and MediaServerDownload are used for uploading images/files/video to -#a remote "mediaserver" (a webserver like caddy for example). +#MediaServerUpload (or MediaServerPath) and MediaServerDownload are used for uploading +#images/files/video toa remote "mediaserver" (a webserver like caddy for example). #When configured images/files uploaded on bridges like mattermost,slack, telegram will be downloaded #and uploaded again to MediaServerUpload URL +#MediaServerPath can be used instead of MediaServerUpload, for when your webserver is on the +#same system as matterbridge and matterbridge has write access to the serve dir. #The MediaServerDownload will be used so that bridges without native uploading support: #gitter, irc and xmpp will be shown links to the files on MediaServerDownload # @@ -1316,6 +1318,8 @@ StripNick=false #OPTIONAL (default empty) MediaServerUpload="https://user:pass@yourserver.com/upload" #OPTIONAL (default empty) +MediaServerPath="/srv/http/yourserver.com/public/download" +#OPTIONAL (default empty) MediaServerDownload="https://youserver.com/download" #MediaDownloadSize is the maximum size of attachments, videos, images