From ada41742a1a745789efaca5dfc26ccc0c6916bcb Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Wed, 6 Nov 2024 08:47:02 -0700 Subject: [PATCH] connector/matrix: check the telegram image size limits Signed-off-by: Sumner Evans --- pkg/connector/config.go | 2 -- pkg/connector/example-config.yaml | 2 -- pkg/connector/matrix.go | 14 +++++++++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/connector/config.go b/pkg/connector/config.go index b6651121..b678cfbb 100644 --- a/pkg/connector/config.go +++ b/pkg/connector/config.go @@ -48,7 +48,6 @@ type TelegramConfig struct { DeviceInfo DeviceInfo `yaml:"device_info"` AnimatedSticker media.AnimatedStickerConfig `yaml:"animated_sticker"` - ImageAsFileSize int `yaml:"image_as_file_size"` ImageAsFilePixels int `yaml:"image_as_file_pixels"` DisableViewOnce bool `yaml:"disable_view_once"` @@ -96,7 +95,6 @@ func upgradeConfig(helper up.Helper) { helper.Copy(up.Int, "animated_sticker", "args", "width") helper.Copy(up.Int, "animated_sticker", "args", "height") helper.Copy(up.Int, "animated_sticker", "args", "fps") - helper.Copy(up.Int, "image_as_file_size") helper.Copy(up.Int, "image_as_file_pixels") helper.Copy(up.Bool, "disable_view_once") helper.Copy(up.Bool, "disable_disappearing") diff --git a/pkg/connector/example-config.yaml b/pkg/connector/example-config.yaml index aea025e9..e880825b 100644 --- a/pkg/connector/example-config.yaml +++ b/pkg/connector/example-config.yaml @@ -30,8 +30,6 @@ animated_sticker: height: 256 fps: 25 # only for webm, webp and gif (2, 5, 10, 20 or 25 recommended) -# Maximum size of image in megabytes before sending to Telegram as a document. -image_as_file_size: 10 # Maximum number of pixels in an image before sending to Telegram as a # document. Defaults to 4096x4096 = 16777216. image_as_file_pixels: 16777216 diff --git a/pkg/connector/matrix.go b/pkg/connector/matrix.go index b6b14110..13e9026e 100644 --- a/pkg/connector/matrix.go +++ b/pkg/connector/matrix.go @@ -64,8 +64,20 @@ func (t *TelegramClient) transferMediaToTelegram(ctx context.Context, content *e return err } + // Telegram restricts photos in the following ways according to: + // https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1input_message_photo.html#ae1229ec5026a0b29dc398d87211bf572 + // + // * The photo must be at most 10 MB in size. + // * The photo's width and height must not exceed 10,000 in total + // * Width and height ratio must be at most 20. + // + // We also have the image_as_file_pixels configuration threshold to + // prevent Telegram from compressing the file. + aspectRatio := float64(max(cfg.Height, cfg.Width)) / float64(min(cfg.Height, cfg.Width)) forceDocument = cfg.Height*cfg.Width > t.main.Config.ImageAsFilePixels || - info.Size() > int64(t.main.Config.ImageAsFileSize*1024*1024) + info.Size() > int64(10*1024*1024) || + aspectRatio > 20 || + cfg.Height+cfg.Width > 10000 } uploader := uploader.NewUploader(t.client.API())