5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 12:50:27 +00:00

Use only alphanumeric for file uploads to mediaserver. Closes #416

This commit is contained in:
Wim 2018-05-06 20:32:09 +02:00
parent 467b373c43
commit c6c92e273d

View File

@ -23,6 +23,7 @@ import (
"github.com/hashicorp/golang-lru" "github.com/hashicorp/golang-lru"
"github.com/peterhellberg/emojilib" "github.com/peterhellberg/emojilib"
"net/http" "net/http"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -409,6 +410,7 @@ func (gw *Gateway) modifyMessage(msg *config.Message) {
} }
func (gw *Gateway) handleFiles(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 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 == "" {
return return
@ -421,6 +423,10 @@ func (gw *Gateway) handleFiles(msg *config.Message) {
} }
for i, f := range msg.Extra["file"] { for i, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) 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)) sha1sum := fmt.Sprintf("%x", sha1.Sum(*fi.Data))
reader := bytes.NewReader(*fi.Data) reader := bytes.NewReader(*fi.Data)
url := gw.Config.General.MediaServerUpload + "/" + sha1sum + "/" + fi.Name url := gw.Config.General.MediaServerUpload + "/" + sha1sum + "/" + fi.Name