2017-11-04 06:55:25 -07:00
|
|
|
package helper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-02-02 16:11:11 -08:00
|
|
|
"fmt"
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-02-23 14:07:23 -08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-11-04 06:55:25 -07:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2018-04-19 04:04:12 -07:00
|
|
|
"strings"
|
2017-11-04 06:55:25 -07:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func DownloadFile(url string) (*[]byte, error) {
|
2018-02-24 14:35:53 -08:00
|
|
|
return DownloadFileAuth(url, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func DownloadFileAuth(url string, auth string) (*[]byte, error) {
|
2017-11-04 06:55:25 -07:00
|
|
|
var buf bytes.Buffer
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: time.Second * 5,
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
2018-02-24 14:35:53 -08:00
|
|
|
if auth != "" {
|
|
|
|
req.Header.Add("Authorization", auth)
|
|
|
|
}
|
2017-11-04 06:55:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-06 15:04:02 -08:00
|
|
|
defer resp.Body.Close()
|
2017-11-04 06:55:25 -07:00
|
|
|
io.Copy(&buf, resp.Body)
|
|
|
|
data := buf.Bytes()
|
|
|
|
return &data, nil
|
|
|
|
}
|
2017-11-24 14:27:13 -08:00
|
|
|
|
|
|
|
func SplitStringLength(input string, length int) string {
|
|
|
|
a := []rune(input)
|
|
|
|
str := ""
|
|
|
|
for i, r := range a {
|
|
|
|
str = str + string(r)
|
|
|
|
if i > 0 && (i+1)%length == 0 {
|
|
|
|
str += "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
2018-02-02 16:11:11 -08:00
|
|
|
|
|
|
|
// handle all the stuff we put into extra
|
|
|
|
func HandleExtra(msg *config.Message, general *config.Protocol) []config.Message {
|
|
|
|
extra := msg.Extra
|
|
|
|
rmsg := []config.Message{}
|
|
|
|
if len(extra[config.EVENT_FILE_FAILURE_SIZE]) > 0 {
|
|
|
|
for _, f := range extra[config.EVENT_FILE_FAILURE_SIZE] {
|
|
|
|
fi := f.(config.FileInfo)
|
|
|
|
text := fmt.Sprintf("file %s too big to download (%#v > allowed size: %#v)", fi.Name, fi.Size, general.MediaDownloadSize)
|
|
|
|
rmsg = append(rmsg, config.Message{Text: text, Username: "<system> ", Channel: msg.Channel})
|
|
|
|
}
|
|
|
|
return rmsg
|
|
|
|
}
|
|
|
|
return rmsg
|
|
|
|
}
|
2018-02-19 15:54:35 -08:00
|
|
|
|
|
|
|
func GetAvatar(av map[string]string, userid string, general *config.Protocol) string {
|
|
|
|
if sha, ok := av[userid]; ok {
|
2018-02-20 08:15:54 -08:00
|
|
|
return general.MediaServerDownload + "/" + sha + "/" + userid + ".png"
|
2018-02-19 15:54:35 -08:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2018-02-23 14:07:23 -08:00
|
|
|
|
|
|
|
func HandleDownloadSize(flog *log.Entry, msg *config.Message, name string, size int64, general *config.Protocol) error {
|
|
|
|
flog.Debugf("Trying to download %#v with size %#v", name, size)
|
|
|
|
if int(size) > general.MediaDownloadSize {
|
|
|
|
msg.Event = config.EVENT_FILE_FAILURE_SIZE
|
|
|
|
msg.Extra[msg.Event] = append(msg.Extra[msg.Event], config.FileInfo{Name: name, Comment: msg.Text, Size: size})
|
|
|
|
return fmt.Errorf("File %#v to large to download (%#v). MediaDownloadSize is %#v", name, size, general.MediaDownloadSize)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleDownloadData(flog *log.Entry, msg *config.Message, name, comment, url string, data *[]byte, general *config.Protocol) {
|
|
|
|
var avatar bool
|
|
|
|
flog.Debugf("Download OK %#v %#v", name, len(*data))
|
|
|
|
if msg.Event == config.EVENT_AVATAR_DOWNLOAD {
|
|
|
|
avatar = true
|
|
|
|
}
|
|
|
|
msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: name, Data: data, URL: url, Comment: comment, Avatar: avatar})
|
|
|
|
}
|
2018-04-19 03:53:49 -07:00
|
|
|
|
|
|
|
func RemoveEmptyNewLines(msg string) string {
|
|
|
|
lines := ""
|
|
|
|
for _, line := range strings.Split(msg, "\n") {
|
|
|
|
if line != "" {
|
|
|
|
lines += line + "\n"
|
|
|
|
}
|
|
|
|
}
|
2018-04-19 13:05:00 -07:00
|
|
|
lines = strings.TrimRight(lines, "\n")
|
2018-04-19 03:53:49 -07:00
|
|
|
return lines
|
|
|
|
}
|