From adb81d106905f3328ba93e16969122c384709eac Mon Sep 17 00:00:00 2001 From: badgerbees <93577481+badgerbees@users.noreply.github.com> Date: Sat, 21 Mar 2026 07:14:58 +0700 Subject: [PATCH] feat(channels): enforce MediaMaxBytes for outbound media uploads in Telegram and Discord (#317) --- internal/channels/discord/send_media.go | 9 +++++++++ internal/channels/telegram/send.go | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/internal/channels/discord/send_media.go b/internal/channels/discord/send_media.go index 1566d554..99bc582b 100644 --- a/internal/channels/discord/send_media.go +++ b/internal/channels/discord/send_media.go @@ -21,12 +21,21 @@ func (c *Channel) sendMediaMessage(channelID string, content string, mediaList [ continue } + maxBytes := c.config.MediaMaxBytes + if maxBytes <= 0 { + maxBytes = defaultMediaMaxBytes + } + f, err := os.Open(filePath) if err != nil { return fmt.Errorf("open media file %s: %w", filePath, err) } defer f.Close() + if info, err := f.Stat(); err == nil && info.Size() > maxBytes { + return fmt.Errorf("outbound media too large: %d bytes (limit %d)", info.Size(), maxBytes) + } + ct := att.ContentType if ct == "" { ct = "application/octet-stream" diff --git a/internal/channels/telegram/send.go b/internal/channels/telegram/send.go index 628db4ef..e0754df6 100644 --- a/internal/channels/telegram/send.go +++ b/internal/channels/telegram/send.go @@ -282,6 +282,20 @@ func (c *Channel) sendMediaMessage(ctx context.Context, chatID int64, msg bus.Ou } } + // Honor MediaMaxBytes for outbound sends. + // Prevents attempting to upload huge files that would fail via Telegram Bot API or local proxy. + maxBytes := c.config.MediaMaxBytes + if maxBytes == 0 { + if c.config.APIServer != "" { + maxBytes = localAPIDefaultMaxBytes + } else { + maxBytes = defaultMediaMaxBytes + } + } + if info, err := os.Stat(media.URL); err == nil && info.Size() > maxBytes { + return fmt.Errorf("outbound media too large: %d bytes (limit %d)", info.Size(), maxBytes) + } + // Send based on content type. // Large images (>photoSizeThreshold) are sent as documents to avoid Telegram compression. ct := strings.ToLower(media.ContentType)