1515
1616from datetime import datetime
1717from telethon import TelegramClient , events
18+ from telethon .tl import types
1819from telethon .tl .patched import Message
1920from telethon .tl .types import User , Chat , Channel
2021from telethon .utils import get_display_name
2122
2223LOG_LEVEL_INFO = 35
2324
2425
26+ class FileSize :
27+ units = ["K" , "M" , "G" , "T" , "P" ]
28+
29+ @staticmethod
30+ def human_readable_to_bytes (size_string : str ):
31+ # Convert to uppercase and strip "B" suffix (i.e. "mb" or "MB" will be "M")
32+ size_string = size_string .upper ().rstrip ("B" )
33+
34+ # Size is already in bytes
35+ if size_string .isdigit ():
36+ return int (size_string )
37+
38+ size_bytes = size_string [:- 1 ]
39+ unit_index = FileSize .units .index (size_string [- 1 ]) + 1
40+
41+ return int (float (size_bytes ) * pow (1024 , unit_index ))
42+
43+ @staticmethod
44+ def bytes_to_human_readable (size_bytes : int ):
45+ unit = ""
46+
47+ for unit in ["" ] + FileSize .units :
48+ if abs (size_bytes ) < 1024 :
49+ return f"{ size_bytes :3.1f} { unit } B"
50+ size_bytes /= 1024
51+
52+ return f"{ size_bytes :3.1f} { unit } B"
53+
54+
2555class DottedPathDict (dict ):
2656 def get (self , path , default = None ):
2757 path = path .split ("." , 1 )
@@ -218,6 +248,8 @@ async def download_media(self, message):
218248 else :
219249 original_filename = Path (message .file .name ).stem
220250
251+ full_original_filename = f"{ original_filename } { message .file .ext } "
252+
221253 filename_pattern_map = {
222254 "date" : {
223255 "year" : message .date .year ,
@@ -242,6 +274,27 @@ async def download_media(self, message):
242274 filename = file_pattern .format_map (filename_pattern_map )
243275 filepath = download_path .joinpath (filename )
244276
277+ if isinstance (message .media , types .MessageMediaPhoto ):
278+ media_type = "photo"
279+ else :
280+ media_type = "file"
281+
282+ media_type_config = self .media_config .get ("types" , {}).get (media_type , {})
283+
284+ if not media_type_config .get ("enabled" , True ):
285+ logging .debug (f"Skipping media download for '{ full_original_filename } ' with type { media_type } as it is disabled" )
286+ return
287+
288+ file_size_string = FileSize .bytes_to_human_readable (message .file .size )
289+
290+ max_size = media_type_config .get ("max_size" , self .media_config .get ("max_size" , "" ))
291+ if max_size != "" :
292+ max_size_bytes = FileSize .human_readable_to_bytes (max_size )
293+ if message .file .size > max_size_bytes :
294+ logging .debug (f"Skipping media download for '{ full_original_filename } ' as it exceeds the configured max size ({ file_size_string } > { max_size } )" )
295+ return
296+
297+ logging .debug (f"Downloading media file '{ full_original_filename } ' with type '{ media_type } ' to { filepath } ({ file_size_string } )" )
245298 await message .download_media (file = filepath )
246299
247300 return DownloadedMedia (filepath = filepath , filename = filename )
0 commit comments