From 130631f4ae434a0087f3bf053ad1b01081b03827 Mon Sep 17 00:00:00 2001 From: Ivan Volynkin Date: Tue, 31 Mar 2026 20:44:21 +0300 Subject: [PATCH] fix: strip extension in download_media to auto-detect real file type When a caller passes a file_path with an explicit extension (e.g. ticket.jpg), Telethon honours that extension verbatim even if the actual media is a PDF. Stripping the suffix before calling client.download_media() lets Telethon append the correct extension based on the real file content. Co-Authored-By: Claude Sonnet 4.6 --- main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index bc36cae..22e4f98 100644 --- a/main.py +++ b/main.py @@ -2157,7 +2157,12 @@ async def download_media( if path_error: return path_error - downloaded = await client.download_media(msg, file=str(out_path)) + # Strip user-supplied extension so Telethon auto-detects the real media type. + # If a path with extension is passed (e.g. ticket.jpg), Telethon writes to that + # exact path even if the file is actually a PDF. Stripping the suffix lets + # Telethon append the correct extension based on the actual file content. + out_path_for_dl = out_path.with_suffix("") + downloaded = await client.download_media(msg, file=str(out_path_for_dl)) if not downloaded: return f"Download failed for message {message_id}."