connector/matrix: check the telegram image size limits

Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
Sumner Evans
2024-11-06 08:47:02 -07:00
parent 1b4416f291
commit ada41742a1
3 changed files with 13 additions and 5 deletions
-2
View File
@@ -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")
-2
View File
@@ -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
+13 -1
View File
@@ -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())