File size: 1,613 Bytes
35e0008
af2b875
 
 
 
 
 
25061b6
af2b875
 
 
35e0008
 
af2b875
 
 
 
 
35e0008
 
 
 
 
 
 
 
 
 
 
25061b6
35e0008
 
 
 
 
 
 
af2b875
35e0008
 
af2b875
 
35e0008
af2b875
 
 
 
 
35e0008
25061b6
 
 
35e0008
 
24a3769
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from hydrogram.types import Message
from datetime import datetime
from mimetypes import guess_type
from bot import TelegramBot
from bot.config import Telegram
from bot.server.error import abort

async def get_message(message_id: int) -> Message | None:
    message = None
    
    try:
        message = await TelegramBot.get_messages(Telegram.CHANNEL_ID, message_ids=message_id)
        if message.empty: message = None
    except Exception:
        pass

    return message

async def send_message(msg: Message, send_to: int = Telegram.CHANNEL_ID) -> Message:
    return await TelegramBot.send_message(entity=send_to, message=msg)

def get_file_properties(msg: Message):
    attributes = (
        'document',
        'video',
        'audio',
        'voice',
        'photo',
        'video_note'
    )
    for attribute in attributes:
        media = getattr(msg, attribute, None)
        if media:
            file_type = attribute
            break

    if not media: abort(400, 'Unknown file type.')

    file_name = getattr(media, 'file_name', None)
    file_size = getattr(media, 'file_size', 0)

    if not file_name:
        file_format = {
            'video': 'mp4',
            'audio': 'mp3',
            'voice': 'ogg',
            'photo': 'jpg',
            'video_note': 'mp4'
        }.get(file_type)
        date = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        file_name = f'{file_type}-{date}.{file_format}'
    
    mime_type = guess_type(file_name)[0] or 'application/octet-stream'

    return file_name, file_size, mime_type