diff --git a/bridge/config/config.go b/bridge/config/config.go index 6e99066c3a..5027cde9de 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -100,6 +100,8 @@ type Protocol struct { MediaDownloadSize int // all protocols MediaServerDownload string MediaServerUpload string + MediaConvertTgsToPNG bool // telegram + MediaConvertTgsToWebP bool // telegram MediaConvertWebPToPNG bool // telegram MessageDelay int // IRC, time in millisecond to wait between messages MessageFormat string // telegram diff --git a/bridge/helper/helper.go b/bridge/helper/helper.go index 412447661a..f8f85557bf 100644 --- a/bridge/helper/helper.go +++ b/bridge/helper/helper.go @@ -5,7 +5,10 @@ import ( "fmt" "image/png" "io" + "io/ioutil" "net/http" + "os" + "os/exec" "regexp" "strings" "time" @@ -192,7 +195,7 @@ func ParseMarkdown(input string) string { return res } -// ConvertWebPToPNG convert input data (which should be WebP format to PNG format) +// ConvertWebPToPNG converts input data (which should be WebP format) to PNG format func ConvertWebPToPNG(data *[]byte) error { r := bytes.NewReader(*data) m, err := webp.Decode(r) @@ -207,3 +210,41 @@ func ConvertWebPToPNG(data *[]byte) error { *data = w.Bytes() return nil } + +func maybeCleanup(name string, logger *logrus.Entry) { + if err := os.Remove(name); err != nil { + logger.Errorf("Could not delete temporary file %s: %v", name, err) + } +} + +// ConvertTgsToWebP convert input data (which should be tgs format) to WebP format +// This relies on an external command, which is ugly, but works. +func ConvertTgsToX(data *[]byte, outputFormat string, logger *logrus.Entry) error { + // lottie can't handle input from a pipe, so write to a temporary file: + tmpFile, err := ioutil.TempFile(os.TempDir(), "matterbridge-lottie-*.tgs") + if err != nil { + return err + } + tmpFileName := tmpFile.Name() + defer maybeCleanup(tmpFileName, logger) + if _, writeErr := tmpFile.Write(*data); writeErr != nil { + return writeErr + } + // Must close before calling lottie to avoid data races: + if closeErr := tmpFile.Close(); closeErr != nil { + return closeErr + } + + // Call lottie to transform: + cmd := exec.Command("lottie_convert.py", "--input-format", "lottie", "--output-format", outputFormat, tmpFileName, "/dev/stdout") + cmd.Stderr = nil + // NB: lottie writes progress into to stderr in all cases. + stdout, stderr := cmd.Output() + if stderr != nil { + // 'stderr' already contains some parts of Stderr, because it was set to 'nil'. + return stderr + } + + *data = stdout + return nil +} diff --git a/bridge/telegram/handlers.go b/bridge/telegram/handlers.go index 5c60f74be9..e94617ad7f 100644 --- a/bridge/telegram/handlers.go +++ b/bridge/telegram/handlers.go @@ -217,6 +217,46 @@ func (b *Btelegram) handleDownloadAvatar(userid int, channel string) { } } +func (b *Btelegram) maybeConvertTgs(name *string, data *[]byte) { + var format string + switch { + case b.GetBool("MediaConvertTgsToWebP"): + b.Log.Debugf("Tgs to WebP conversion enabled, converting %v", name) + format = "webp" + case b.GetBool("MediaConvertTgsToPNG"): + // The WebP to PNG converter can't handle animated webp files yet, + // and I'm not going to write a path for x/image/webp. + // The error message would be: + // conversion failed: webp: non-Alpha VP8X is not implemented + // So instead, we tell lottie to directly go to PNG: + b.Log.Debugf("Tgs to PNG conversion enabled, converting %v", name) + format = "png" + default: + // Otherwise, no conversion was requested. Trying to run the usual webp + // converter would fail, because '.tgs.webp' is actually a gzipped JSON + // file, and has nothing to do with WebP. + return + } + err := helper.ConvertTgsToX(data, format, b.Log) + if err != nil { + b.Log.Errorf("conversion failed: %v", err) + } else { + *name = strings.Replace(*name, "tgs.webp", format, 1) + } +} + +func (b *Btelegram) maybeConvertWebp(name *string, data *[]byte) { + if b.GetBool("MediaConvertWebPToPNG") { + b.Log.Debugf("WebP to PNG conversion enabled, converting %v", name) + err := helper.ConvertWebPToPNG(data) + if err != nil { + b.Log.Errorf("conversion failed: %v", err) + } else { + *name = strings.Replace(*name, ".webp", ".png", 1) + } + } +} + // handleDownloadFile handles file download func (b *Btelegram) handleDownload(rmsg *config.Message, message *tgbotapi.Message) error { size := 0 @@ -264,15 +304,13 @@ 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) - } + + if strings.HasSuffix(name, ".tgs.webp") { + b.maybeConvertTgs(&name, data) + } else if strings.HasSuffix(name, ".webp") { + b.maybeConvertWebp(&name, data) } + helper.HandleDownloadData(b.Log, rmsg, name, message.Caption, "", data, b.General) return nil }