Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 31127866bd7235b6bca23b9c9b17e4066e70affa Author: Wim <wim@42.be> Date: Wed Feb 27 00:41:50 2019 +0000 Add MediaConvertWebPToPNG option (telegram). (#741) * Add MediaConvertWebPToPNG option (telegram). When enabled matterbridge will convert .webp files to .png files before uploading them to the mediaserver of the other bridges. Fixes #398 diff --git a/.travis.yml b/.travis.yml index 7ee35d7..295540a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -486 +488 @@ after_script:   - ./cc-test-reporter after-build --exit-code ${TRAVIS_TEST_RESULT}    deploy: + on: + all_branches: true   provider: bintray   on: all_branches: true diff --git a/bridge/config/config.go b/bridge/config/config.go index 61ffe91..230ddb9 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -936 +937 @@ 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 3836556..c336fd1 100644 --- a/bridge/helper/helper.go +++ b/bridge/helper/helper.go @@ -36 +37 @@ package helper  import (   "bytes"   "fmt" + "image/png"   "io"   "net/http"   "regexp" @@ -106 +118 @@ import (   "time"   "unicode/utf8"   + "golang.org/x/image/webp" +   "github.com/42wim/matterbridge/bridge/config"   "github.com/sirupsen/logrus"   "gitlab.com/golang-commonmark/markdown" @@ -1773 +18019 @@ 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 1770acd..48f33b1 100644 --- a/bridge/helper/helper_test.go +++ b/bridge/helper/helper_test.go @@ -16 +18 @@  package helper    import ( + "io/ioutil" + "os"   "testing"     "github.com/stretchr/testify/assert" @@ -1033 +10522 @@ 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 e87466c..93576fb 100644 --- a/bridge/telegram/handlers.go +++ b/bridge/telegram/handlers.go @@ -2456 +24515 @@ 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  } diff --git a/go.mod b/go.mod index d426a1a..b25c342 100644 --- a/go.mod +++ b/go.mod @@ -666 +667 @@ require (   go.uber.org/atomic v1.3.2 // indirect   go.uber.org/multierr v1.1.0 // indirect   go.uber.org/zap v1.9.1 // indirect + golang.org/x/image v0.0.0-20190220214146-31aff87c08e9   gopkg.in/fsnotify.v1 v1.4.7 // indirect   gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect   gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect diff --git a/go.sum b/go.sum index eaa1371..6b2ee7b 100644 --- a/go.sum +++ b/go.sum @@ -2056 +2058 @@ golang.org/x/crypto v0.0.0-20190130090550-b01c7a725664 h1:YbZJ76lQ1BqNhVe7dKTSB6  golang.org/x/crypto v0.0.0-20190130090550-b01c7a725664/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=  golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613 h1:MQ/ZZiDsUapFFiMS+vzwXkCTeEKaum+Do5rINYJDmxc=  golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/image v0.0.0-20190220214146-31aff87c08e9 h1:+vH8qNweCrORN49012OX3h0oWEXO3p+rRnpAGQinddk= +golang.org/x/image v0.0.0-20190220214146-31aff87c08e9/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=  golang.org/x/net v0.0.0-20190110200230-915654e7eabc h1:Yx9JGxI1SBhVLFjpAkWMaO1TF+xyqtHLjZpvQboJGiM=  golang.org/x/net v0.0.0-20190110200230-915654e7eabc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=  golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 19cb09b..1c517a5 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -9136 +91311 @@ QuoteDisable=false  #OPTIONAL (default "{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})")  QuoteFormat="{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})"   +#Convert WebP images to PNG before upload. +#https://github.com/42wim/matterbridge/issues/398 +#OPTIONAL (default false) +MediaConvertWebPToPNG=false +  #Disable sending of edits to other bridges  #OPTIONAL (default false)  EditDisable=false