media: fix lottie stickers not being decompressed
This commit is contained in:
@@ -161,7 +161,7 @@ func (tc *TelegramConnector) Download(ctx context.Context, mediaID networkid.Med
|
|||||||
log.Debug().
|
log.Debug().
|
||||||
Int64("document_id", msgMedia.Document.GetID()).
|
Int64("document_id", msgMedia.Document.GetID()).
|
||||||
Bool("is_sticker", isSticker).
|
Bool("is_sticker", isSticker).
|
||||||
Msg("downloading photo")
|
Msg("downloading document")
|
||||||
readyTransferer = transferer.WithDocument(msgMedia.Document, info.Thumbnail)
|
readyTransferer = transferer.WithDocument(msgMedia.Document, info.Thumbnail)
|
||||||
case *tg.MessageMediaWebPage:
|
case *tg.MessageMediaWebPage:
|
||||||
webpage, ok := msgMedia.Webpage.(*tg.WebPage)
|
webpage, ok := msgMedia.Webpage.(*tg.WebPage)
|
||||||
@@ -172,7 +172,7 @@ func (tc *TelegramConnector) Download(ctx context.Context, mediaID networkid.Med
|
|||||||
if pc, ok := webpage.GetPhoto(); ok && pc.TypeID() == tg.PhotoTypeID {
|
if pc, ok := webpage.GetPhoto(); ok && pc.TypeID() == tg.PhotoTypeID {
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Int64("photo_id", pc.GetID()).
|
Int64("photo_id", pc.GetID()).
|
||||||
Msg("downloading photo")
|
Msg("downloading webpage photo")
|
||||||
readyTransferer = transferer.WithPhoto(pc)
|
readyTransferer = transferer.WithPhoto(pc)
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("not a photo: %T", pc.TypeName())
|
return nil, fmt.Errorf("not a photo: %T", pc.TypeName())
|
||||||
|
|||||||
@@ -18,9 +18,12 @@ package media
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/klauspost/compress/gzip"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"go.mau.fi/util/ffmpeg"
|
"go.mau.fi/util/ffmpeg"
|
||||||
"go.mau.fi/util/lottie"
|
"go.mau.fi/util/lottie"
|
||||||
@@ -107,13 +110,44 @@ func (c *AnimatedStickerConfig) convertWebm(ctx context.Context, src *os.File) *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AnimatedStickerConfig) convert(ctx context.Context, src *os.File) *ConvertedSticker {
|
func extractGZip(src *os.File) (*ConvertedSticker, error) {
|
||||||
if c.Target == "disable" {
|
reader, err := gzip.NewReader(src)
|
||||||
return nil
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create gzip reader: %w", err)
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = reader.Close()
|
||||||
|
}()
|
||||||
|
replFile, err := os.OpenFile(src.Name()+".json", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create temp file: %w", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = replFile.Close()
|
||||||
|
}()
|
||||||
|
n, err := io.Copy(replFile, reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to extract lottie gzip: %w", err)
|
||||||
|
}
|
||||||
|
return &ConvertedSticker{
|
||||||
|
Success: true,
|
||||||
|
NewPath: replFile.Name(),
|
||||||
|
MIMEType: "video/lottie+json",
|
||||||
|
Size: int(n),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *AnimatedStickerConfig) convert(ctx context.Context, src *os.File) *ConvertedSticker {
|
||||||
log := zerolog.Ctx(ctx).With().Str("animated_sticker_target", c.Target).Logger()
|
log := zerolog.Ctx(ctx).With().Str("animated_sticker_target", c.Target).Logger()
|
||||||
|
|
||||||
|
if c.Target == "disable" {
|
||||||
|
converted, err := extractGZip(src)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("Failed to extract lottie sticker")
|
||||||
|
}
|
||||||
|
return converted
|
||||||
|
}
|
||||||
|
|
||||||
if !lottie.Supported() {
|
if !lottie.Supported() {
|
||||||
log.Warn().Msg("Not converting lottie sticker as lottieconverter is not installed")
|
log.Warn().Msg("Not converting lottie sticker as lottieconverter is not installed")
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -293,7 +293,7 @@ func (t *ReadyTransferer) Transfer(ctx context.Context, store *store.Container,
|
|||||||
if t.inner.fileInfo.MimeType == "video/webm" {
|
if t.inner.fileInfo.MimeType == "video/webm" {
|
||||||
converted = t.inner.animatedStickerConfig.convertWebm(ctx, osFile)
|
converted = t.inner.animatedStickerConfig.convertWebm(ctx, osFile)
|
||||||
} else {
|
} else {
|
||||||
t.inner.fileInfo.MimeType = "video/lottie+json"
|
t.inner.fileInfo.MimeType = "application/x-tgsticker" // This is expected to get overridden by convert
|
||||||
converted = t.inner.animatedStickerConfig.convert(ctx, osFile)
|
converted = t.inner.animatedStickerConfig.convert(ctx, osFile)
|
||||||
}
|
}
|
||||||
if converted != nil {
|
if converted != nil {
|
||||||
@@ -407,7 +407,7 @@ func (t *ReadyTransferer) ToDirectMediaResponse(ctx context.Context) (mediaproxy
|
|||||||
if t.inner.fileInfo.MimeType == "video/webm" {
|
if t.inner.fileInfo.MimeType == "video/webm" {
|
||||||
converted = t.inner.animatedStickerConfig.convertWebm(ctx, w)
|
converted = t.inner.animatedStickerConfig.convertWebm(ctx, w)
|
||||||
} else {
|
} else {
|
||||||
t.inner.fileInfo.MimeType = "video/lottie+json"
|
t.inner.fileInfo.MimeType = "application/x-tgsticker" // This is expected to get overridden by convert
|
||||||
converted = t.inner.animatedStickerConfig.convert(ctx, w)
|
converted = t.inner.animatedStickerConfig.convert(ctx, w)
|
||||||
}
|
}
|
||||||
if converted == nil {
|
if converted == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user