From 33bd60528be4eaf28ffd1065517ea41fde9ea62b Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Fri, 8 Jun 2018 22:30:35 +0200 Subject: Add config option MediaDownloadPath (#443) * Add config option MediaUploadPath MediaDownloadPath 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. * Limit length of hash in MediaServer urls to 8chars Full SHA256 is unnecessary for uniqueness. Also; if a file has the same first 8 charachters of the SHA256 hash, it's still not a problem, as long as the filename is not the same. --- gateway/gateway.go | 97 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 28 deletions(-) (limited to 'gateway') diff --git a/gateway/gateway.go b/gateway/gateway.go index eb5bbe92..b926912a 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 { @@ -411,46 +415,83 @@ func (gw *Gateway) modifyMessage(msg *config.Message) { } } +// handleFiles uploads or places all files on the given msg to the MediaServer and +// adds the new URL of the file on the MediaServer onto the given msg. 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 we don't have a attachfield or we don't have a mediaserver configured return + if msg.Extra == nil || (gw.Config.General.MediaServerUpload == "" && gw.Config.General.MediaDownloadPath == "") { return } - // if we actually have files, start uploading them to the mediaserver - if len(msg.Extra["file"]) > 0 { - client := &http.Client{ - Timeout: time.Second * 5, - } - for i, f := range msg.Extra["file"] { - fi := f.(config.FileInfo) - ext := filepath.Ext(fi.Name) - fi.Name = fi.Name[0 : len(fi.Name)-len(ext)] - fi.Name = reg.ReplaceAllString(fi.Name, "_") - fi.Name = fi.Name + ext - sha1sum := fmt.Sprintf("%x", sha1.Sum(*fi.Data)) - reader := bytes.NewReader(*fi.Data) + // If we don't have files, nothing to upload. + if len(msg.Extra["file"]) == 0 { + return + } + + client := &http.Client{ + Timeout: time.Second * 5, + } + + for i, f := range msg.Extra["file"] { + fi := f.(config.FileInfo) + ext := filepath.Ext(fi.Name) + fi.Name = fi.Name[0 : len(fi.Name)-len(ext)] + fi.Name = reg.ReplaceAllString(fi.Name, "_") + fi.Name = fi.Name + ext + + sha1sum := fmt.Sprintf("%x", sha1.Sum(*fi.Data))[:8] + + if gw.Config.General.MediaServerUpload != "" { + // Use MediaServerUpload. Upload using a PUT HTTP request and basicauth. + 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 - req, err := http.NewRequest("PUT", url, reader) + + req, err := http.NewRequest("PUT", url, bytes.NewReader(*fi.Data)) if err != nil { - flog.Errorf("mediaserver upload failed: %#v", err) + 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: %#v", err) + flog.Errorf("mediaserver upload failed, could not Do request: %#v", err) + continue + } + } else { + // Use MediaServerPath. Place the file on the current filesystem. + + dir := gw.Config.General.MediaDownloadPath + "/" + 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 } - flog.Debugf("mediaserver download URL = %s", durl) - // we uploaded the file successfully. Add the SHA - extra.SHA = sha1sum - msg.Extra["file"][i] = extra } + + // Download URL. + durl := gw.Config.General.MediaServerDownload + "/" + sha1sum + "/" + fi.Name + + flog.Debugf("mediaserver download URL = %s", durl) + + // We uploaded/placed the file successfully. Add the SHA and URL. + extra := msg.Extra["file"][i].(config.FileInfo) + extra.URL = durl + extra.SHA = sha1sum + msg.Extra["file"][i] = extra } } -- cgit v1.2.3