mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-13 22:15:47 +00:00
feat(channels): enforce MediaMaxBytes for outbound media uploads in Telegram and Discord (#317)
This commit is contained in:
1 parent
66a8029d26
commit
adb81d1069
2 files changed
+23
No files matched your search
@@ -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"
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user