summaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
Diffstat (limited to 'bridge')
-rw-r--r--bridge/config/config.go1
-rw-r--r--bridge/helper/helper.go19
-rw-r--r--bridge/helper/helper_test.go21
-rw-r--r--bridge/telegram/handlers.go9
4 files changed, 50 insertions, 0 deletions
diff --git a/bridge/config/config.go b/bridge/config/config.go
index 61ffe913..230ddb9e 100644
--- a/bridge/config/config.go
+++ b/bridge/config/config.go
@@ -93,6 +93,7 @@ type Protocol struct {
MediaDownloadSize int // all protocols
MediaServerDownload string
MediaServerUpload string
+ MediaConvertWebPToPNG bool // telegram
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/bridge/helper/helper.go b/bridge/helper/helper.go
index 3836556f..c336fd13 100644
--- a/bridge/helper/helper.go
+++ b/bridge/helper/helper.go
@@ -3,6 +3,7 @@ package helper
import (
"bytes"
"fmt"
+ "image/png"
"io"
"net/http"
"regexp"
@@ -10,6 +11,8 @@ import (
"time"
"unicode/utf8"
+ "golang.org/x/image/webp"
+
"github.com/42wim/matterbridge/bridge/config"
"github.com/sirupsen/logrus"
"gitlab.com/golang-commonmark/markdown"
@@ -177,3 +180,19 @@ func ParseMarkdown(input string) string {
md := markdown.New(markdown.XHTMLOutput(true), markdown.Breaks(true))
return (md.RenderToString([]byte(input)))
}
+
+// ConvertWebPToPNG convert input data (which should be WebP format to PNG format)
+func ConvertWebPToPNG(data *[]byte) error {
+ r := bytes.NewReader(*data)
+ m, err := webp.Decode(r)
+ if err != nil {
+ return err
+ }
+ var output []byte
+ w := bytes.NewBuffer(output)
+ if err := png.Encode(w, m); err != nil {
+ return err
+ }
+ *data = w.Bytes()
+ return nil
+}
diff --git a/bridge/helper/helper_test.go b/bridge/helper/helper_test.go
index 1770acd9..48f33b10 100644
--- a/bridge/helper/helper_test.go
+++ b/bridge/helper/helper_test.go
@@ -1,6 +1,8 @@
package helper
import (
+ "io/ioutil"
+ "os"
"testing"
"github.com/stretchr/testify/assert"
@@ -103,3 +105,22 @@ func TestGetSubLines(t *testing.T) {
assert.Equalf(t, testcase.nonSplitOutput, nonSplitLines, "'%s' testcase should give expected lines without splitting.", testname)
}
}
+
+func TestConvertWebPToPNG(t *testing.T) {
+ if os.Getenv("LOCAL_TEST") == "" {
+ t.Skip()
+ }
+ input, err := ioutil.ReadFile("test.webp")
+ if err != nil {
+ t.Fail()
+ }
+ d := &input
+ err = ConvertWebPToPNG(d)
+ if err != nil {
+ t.Fail()
+ }
+ err = ioutil.WriteFile("test.png", *d, 0644)
+ if err != nil {
+ t.Fail()
+ }
+}
diff --git a/bridge/telegram/handlers.go b/bridge/telegram/handlers.go
index e87466ca..93576fb4 100644
--- a/bridge/telegram/handlers.go
+++ b/bridge/telegram/handlers.go
@@ -245,6 +245,15 @@ func (b *Btelegram) handleDownload(rmsg *config.Message, message *tgbotapi.Messa
if err != nil {
return err
}
+ if strings.HasSuffix(name, ".webp") && b.GetBool("MediaConvertWebPToPNG") {
+ b.Log.Debugf("WebP to PNG conversion enabled, converting %s", name)
+ err := helper.ConvertWebPToPNG(data)
+ if err != nil {
+ b.Log.Errorf("conversion failed: %s", err)
+ } else {
+ name = strings.Replace(name, ".webp", ".png", 1)
+ }
+ }
helper.HandleDownloadData(b.Log, rmsg, name, message.Caption, "", data, b.General)
return nil
}