fix(discord): preflight attachment size before upload
Reject oversized local attachments before channel.send(file=...) so users get an explicit notice instead of a doomed 413 round-trip. Fixes #50846
This commit is contained in:
@@ -170,6 +170,10 @@ _DISCORD_SELECT_MAX_ROWS = 5
|
||||
# Model-select capacity: keep 2 rows for Back/Cancel, fill the rest with selects.
|
||||
_DISCORD_MODEL_SELECT_CAPACITY = (_DISCORD_SELECT_MAX_ROWS - 2) * _DISCORD_SELECT_MAX_OPTIONS
|
||||
_DISCORD_BUTTON_LABEL_LIMIT = 80
|
||||
# Default Discord attachment cap for DMs / channels without a guild boost
|
||||
# context. Guild channels expose the effective limit via
|
||||
# ``guild.filesize_limit`` (boost tier may raise it). See issue #50846.
|
||||
_DISCORD_DEFAULT_UPLOAD_LIMIT_BYTES = 25 * 1024 * 1024
|
||||
_DISCORD_ELLIPSIS = "\u2026"
|
||||
_DISCORD_NONCONVERSATIONAL_METADATA_KEYS = frozenset({
|
||||
"non_conversational", "non_conversational_history",
|
||||
@@ -3157,7 +3161,19 @@ class DiscordAdapter(DiscordMediaMixin, BasePlatformAdapter):
|
||||
success=True, message_id=last_id, continuation_message_ids=tuple(continuation_ids),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _discord_upload_limit_bytes(channel: Any) -> int:
|
||||
"""Return the effective Discord attachment size limit for *channel*.
|
||||
|
||||
Prefer the guild's boost-aware ``filesize_limit`` when present; fall
|
||||
back to the platform default for DMs / group DMs without a guild.
|
||||
"""
|
||||
guild = getattr(channel, "guild", None)
|
||||
if guild is not None:
|
||||
limit = getattr(guild, "filesize_limit", None)
|
||||
if isinstance(limit, int) and limit > 0:
|
||||
return limit
|
||||
return _DISCORD_DEFAULT_UPLOAD_LIMIT_BYTES
|
||||
|
||||
async def play_tts(self, chat_id: str, audio_path: str, **kwargs) -> SendResult:
|
||||
"""Play auto-TTS audio: in the guild's VC if joined, else as a file attachment."""
|
||||
|
||||
@@ -33,6 +33,35 @@ class DiscordMediaMixin:
|
||||
if not channel:
|
||||
return SendResult(success=False, error=f"Channel {chat_id} not found")
|
||||
filename = file_name or os.path.basename(file_path)
|
||||
try:
|
||||
file_size = os.path.getsize(file_path)
|
||||
except OSError as exc:
|
||||
return SendResult(success=False, error=f"Cannot stat file {filename}: {exc}")
|
||||
# Reject oversized files before upload (#50846): no doomed 413 round-trip,
|
||||
# and the user gets an explicit notice instead of a silent failure.
|
||||
limit = self._discord_upload_limit_bytes(channel)
|
||||
if file_size > limit:
|
||||
size_mb = file_size / (1024 * 1024)
|
||||
limit_mb = limit / (1024 * 1024)
|
||||
error = (
|
||||
f"File too large for Discord upload: {filename} is "
|
||||
f"{size_mb:.1f} MB (limit {limit_mb:.0f} MB)"
|
||||
)
|
||||
logger.warning("[%s] %s", self.name, error)
|
||||
notice = (
|
||||
f"⚠️ Could not attach `{filename}` — {size_mb:.1f} MB exceeds "
|
||||
f"Discord's {limit_mb:.0f} MB upload limit for this channel. "
|
||||
f"Compress the file or share a link instead."
|
||||
)
|
||||
try:
|
||||
if not self._is_forum_parent(channel):
|
||||
await channel.send(content=notice)
|
||||
except Exception:
|
||||
logger.debug(
|
||||
"[%s] Failed to send oversized-file notice for %s",
|
||||
self.name, filename, exc_info=True,
|
||||
)
|
||||
return SendResult(success=False, error=error)
|
||||
logger.info(
|
||||
"[%s] Sending file attachment %s (%s) to %s", self.name, filename,
|
||||
os.path.splitext(filename)[1].lower() or "no-ext", chat_id,
|
||||
@@ -96,6 +125,7 @@ class DiscordMediaMixin:
|
||||
await asyncio.sleep(human_delay)
|
||||
files: List[Any] = []
|
||||
captions: List[str] = []
|
||||
skip_notices: List[str] = []
|
||||
aiohttp_session = None
|
||||
try:
|
||||
for image_url, alt_text in chunk:
|
||||
@@ -106,6 +136,30 @@ class DiscordMediaMixin:
|
||||
if not os.path.exists(local_path):
|
||||
logger.warning("[%s] Skipping missing image: %s", self.name, local_path)
|
||||
continue
|
||||
# Same preflight as _send_file_attachment (#50846): an oversized
|
||||
# local image would 413 the whole chunk and drop its siblings
|
||||
# into the fallback path.
|
||||
try:
|
||||
_img_size = os.path.getsize(local_path)
|
||||
except OSError as stat_err:
|
||||
logger.warning(
|
||||
"[%s] Skipping unreadable image %s: %s",
|
||||
self.name, local_path, stat_err,
|
||||
)
|
||||
continue
|
||||
_img_limit = self._discord_upload_limit_bytes(channel)
|
||||
if _img_size > _img_limit:
|
||||
logger.warning(
|
||||
"[%s] Skipping oversized image in batch: %s is %.1f MB (limit %.0f MB)",
|
||||
self.name, os.path.basename(local_path),
|
||||
_img_size / (1024 * 1024), _img_limit / (1024 * 1024),
|
||||
)
|
||||
skip_notices.append(
|
||||
f"⚠️ Skipped `{os.path.basename(local_path)}` — "
|
||||
f"{_img_size / (1024 * 1024):.1f} MB exceeds Discord's "
|
||||
f"{_img_limit / (1024 * 1024):.0f} MB upload limit."
|
||||
)
|
||||
continue
|
||||
files.append(_discord_mod.File(local_path, filename=os.path.basename(local_path)))
|
||||
else:
|
||||
if not is_safe_url(image_url):
|
||||
@@ -135,9 +189,21 @@ class DiscordMediaMixin:
|
||||
logger.warning("[%s] Download failed for %s: %s", self.name, image_url[:80], dl_err)
|
||||
continue
|
||||
if not files:
|
||||
# Everything in this chunk was skipped. Still surface any
|
||||
# oversized-file notices so the drop is not silent.
|
||||
if skip_notices and not self._is_forum_parent(channel):
|
||||
try:
|
||||
await channel.send(content="\n".join(skip_notices))
|
||||
except Exception:
|
||||
logger.debug(
|
||||
"[%s] Failed to send oversized-image notices",
|
||||
self.name, exc_info=True,
|
||||
)
|
||||
continue
|
||||
# Use the first caption if any (Discord only has one message body for the group)
|
||||
content = captions[0] if captions else None
|
||||
if skip_notices:
|
||||
content = "\n".join(([content] if content else []) + skip_notices)
|
||||
logger.info(
|
||||
"[%s] Sending %d image(s) as single Discord message (chunk %d/%d)",
|
||||
self.name, len(files), chunk_idx + 1, len(chunks),
|
||||
|
||||
Reference in New Issue
Block a user