2021-11-01 05:02:46 +01:00
|
|
|
from typing import Union
|
2021-06-13 13:01:23 +02:00
|
|
|
from urllib.parse import quote
|
|
|
|
|
2021-11-01 05:02:46 +01:00
|
|
|
from telethon.tl.custom import Message
|
2021-06-13 13:01:23 +02:00
|
|
|
|
2021-11-01 05:02:46 +01:00
|
|
|
def get_file_name(message: Message, quote_name: bool = True) -> str:
|
2020-08-10 09:27:52 +02:00
|
|
|
if message.file.name:
|
2021-06-13 13:01:23 +02:00
|
|
|
name = message.file.name
|
|
|
|
else:
|
|
|
|
ext = message.file.ext or ""
|
|
|
|
name = f"{message.date.strftime('%Y-%m-%d_%H:%M:%S')}{ext}"
|
2021-07-17 05:21:54 +02:00
|
|
|
return quote(name) if quote_name else name
|
2020-08-10 09:27:52 +02:00
|
|
|
|
2021-11-01 05:02:46 +01:00
|
|
|
def get_human_size(num: Union[int, float]) -> str:
|
2020-08-10 09:27:52 +02:00
|
|
|
base = 1024.0
|
2021-06-13 13:01:23 +02:00
|
|
|
sufix_list = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
|
2020-08-10 09:27:52 +02:00
|
|
|
for unit in sufix_list:
|
|
|
|
if abs(num) < base:
|
|
|
|
return f"{round(num, 2)} {unit}"
|
2021-06-13 13:01:23 +02:00
|
|
|
num /= base
|