diff options
Diffstat (limited to 'bridge/telegram')
-rw-r--r-- | bridge/telegram/handlers.go | 54 | ||||
-rw-r--r-- | bridge/telegram/telegram.go | 13 |
2 files changed, 59 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 } diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 29f2f291..f1c7168c 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -2,6 +2,7 @@ package btelegram import ( "html" + "log" "strconv" "strings" @@ -16,6 +17,8 @@ const ( HTMLFormat = "HTML" HTMLNick = "htmlnick" MarkdownV2 = "MarkdownV2" + FormatPng = "png" + FormatWebp = "webp" ) type Btelegram struct { @@ -25,6 +28,16 @@ type Btelegram struct { } func New(cfg *bridge.Config) bridge.Bridger { + tgsConvertFormat := cfg.GetString("MediaConvertTgs") + if tgsConvertFormat != "" { + err := helper.CanConvertTgsToX() + if err != nil { + log.Fatalf("Telegram bridge configured to convert .tgs files to '%s', but lottie does not appear to work:\n%#v", tgsConvertFormat, err) + } + if tgsConvertFormat != FormatPng && tgsConvertFormat != FormatWebp { + log.Fatalf("Telegram bridge configured to convert .tgs files to '%s', but only '%s' and '%s' are supported.", FormatPng, FormatWebp, tgsConvertFormat) + } + } return &Btelegram{Config: cfg, avatarMap: make(map[string]string)} } |