diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx.de> | 2020-08-23 22:34:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-23 22:34:28 +0200 |
commit | b2af76e7dc90317369c767a686d87937aff54a20 (patch) | |
tree | 79bf0cdc782d225631ceaad3f4eaff28501503f7 /bridge/telegram/handlers.go | |
parent | 491fe35397f2287b94f97d2541c7e84e1572df88 (diff) | |
download | matterbridge-msglm-b2af76e7dc90317369c767a686d87937aff54a20.tar.gz matterbridge-msglm-b2af76e7dc90317369c767a686d87937aff54a20.tar.bz2 matterbridge-msglm-b2af76e7dc90317369c767a686d87937aff54a20.zip |
Support Telegram animated stickers (tgs) format (#1173)
This is half a fix for #874
This patch introduces a new config flag:
- MediaConvertTgs
These need to be treated independently from the existing
MediaConvertWebPToPNG flag because Tgs→WebP results in an
*animated* WebP, and the WebP→PNG converter can't handle
animated WebP files yet.
Furthermore, some platforms (like discord) don't even support
animated WebP files, so the user may want to fall back to
static PNGs (not APNGs).
The final reason why this is only half a fix is that this
introduces an external dependency, namely lottie, to be
installed like this:
$ pip3 install lottie cairosvg
This patch works by writing the tgs to a temporary file in /tmp,
calling lottie to convert it (this conversion may take several seconds!),
and then deleting the temporary file.
The temporary file is absolutely necessary, as lottie refuses to
work on non-seekable files.
If anyone comes up with a reasonable use case where /tmp is
unavailable, I can add yet another config option for that, if desired.
Telegram will bail out if the option is configured but lottie isn't found.
Diffstat (limited to 'bridge/telegram/handlers.go')
-rw-r--r-- | bridge/telegram/handlers.go | 54 |
1 files changed, 46 insertions, 8 deletions
diff --git a/bridge/telegram/handlers.go b/bridge/telegram/handlers.go index 5c60f74b..ee087524 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 b.GetString("MediaConvertTgs") { + case FormatWebp: + b.Log.Debugf("Tgs to WebP conversion enabled, converting %v", name) + format = FormatWebp + case FormatPng: + // 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 = FormatPng + 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 } |