1414from fastapi .responses import StreamingResponse , HTMLResponse
1515from telethon import TelegramClient , utils
1616from telethon .tl import types
17+ from telethon .tl .types import PeerChannel
1718from telethon .errors import SessionPasswordNeededError , PhoneCodeInvalidError
1819import uvicorn
1920
@@ -426,6 +427,8 @@ async def upload_large_file(
426427 if not target_channel :
427428 raise HTTPException (status_code = 400 , detail = "Channel ID required" )
428429
430+ temp_path = None # Initialize to avoid UnboundLocalError
431+
429432 try :
430433 # Save uploaded file temporarily
431434 temp_path = f"/tmp/{ file .filename } "
@@ -481,7 +484,7 @@ async def progress_callback(current, total):
481484
482485 except Exception as e :
483486 logger .error (f"Upload failed: { e } " , exc_info = True )
484- if os .path .exists (temp_path ):
487+ if temp_path and os .path .exists (temp_path ):
485488 os .remove (temp_path )
486489 raise HTTPException (status_code = 500 , detail = f"Upload failed: { str (e )} " )
487490
@@ -498,11 +501,18 @@ async def download_large_file(channel_id: str, message_id: int):
498501 if not client or not await client .is_user_authorized ():
499502 raise HTTPException (status_code = 503 , detail = "Telegram client not ready" )
500503
504+ temp_path = None # Initialize to avoid UnboundLocalError
505+
501506 try :
502507 logger .info (f"Fetching message { message_id } from channel { channel_id } ..." )
503508
509+ # Convert channel_id string to integer and create PeerChannel
510+ # This helps Telethon resolve the entity correctly
511+ channel_id_int = int (channel_id )
512+ peer = PeerChannel (utils .resolve_id (channel_id_int )[0 ])
513+
504514 # Get the message
505- message = await client .get_messages (channel_id , ids = message_id )
515+ message = await client .get_messages (peer , ids = message_id )
506516
507517 if not message or not message .document :
508518 raise HTTPException (status_code = 404 , detail = "File not found" )
@@ -554,7 +564,7 @@ def file_iterator():
554564 raise
555565 except Exception as e :
556566 logger .error (f"Download failed: { e } " , exc_info = True )
557- if os .path .exists (temp_path ):
567+ if temp_path and os .path .exists (temp_path ):
558568 os .remove (temp_path )
559569 raise HTTPException (status_code = 500 , detail = f"Download failed: { str (e )} " )
560570
@@ -566,7 +576,11 @@ async def get_file_info(channel_id: str, message_id: int):
566576 raise HTTPException (status_code = 503 , detail = "Telegram client not ready" )
567577
568578 try :
569- message = await client .get_messages (channel_id , ids = message_id )
579+ # Convert channel_id string to integer and create PeerChannel
580+ channel_id_int = int (channel_id )
581+ peer = PeerChannel (utils .resolve_id (channel_id_int )[0 ])
582+
583+ message = await client .get_messages (peer , ids = message_id )
570584
571585 if not message or not message .document :
572586 raise HTTPException (status_code = 404 , detail = "File not found" )
0 commit comments