Add get_file_url

This commit is contained in:
Gabben 2020-03-18 11:45:16 +05:00
parent a7883084c2
commit bea1ba6243
3 changed files with 22 additions and 2 deletions

View file

@ -120,7 +120,7 @@ class BaseBot(ContextInstanceMixin, DataMixin):
if destination is None:
destination = io.BytesIO()
url = self.session.api.file_url(token=self.__token, path=file_path)
url = self.get_file_url(file_path)
stream = self.session.stream_content(url=url, timeout=timeout, chunk_size=chunk_size)
if isinstance(destination, (str, pathlib.Path)):
@ -130,6 +130,19 @@ class BaseBot(ContextInstanceMixin, DataMixin):
destination=destination, seek=seek, stream=stream
)
def get_file_url(self, file_path: str) -> str:
"""
Get file url
Attention!!
This method has security vulnerabilities for the reason that result
contains bot's *access token* in open form. Use at your own risk!
:param file_path: File path on Telegram server (You can get it from :obj:`aiogram.types.File`)
:type file_path: str
"""
return self.session.api.file_url(self.__token, file_path)
def __hash__(self) -> int:
"""
Get hash for the token

View file

@ -83,7 +83,7 @@ class Downloadable:
from aiogram.api.client.bot import Bot
file = await self.get_file()
return Bot.get_current(no_error=False).session.api.file_url(file.file_path)
return Bot.get_current(no_error=False).get_file_url(file.file_path)
def __hash__(self):
return hash(self.file_id) # type: ignore

View file

@ -6,6 +6,7 @@ from aiofiles import threadpool
from aresponses import ResponsesMockServer
from aiogram.api.client.base import BaseBot
from aiogram.api.client.telegram import PRODUCTION
from aiogram.api.client.session.aiohttp import AiohttpSession
from aiogram.api.methods import GetMe
@ -115,3 +116,9 @@ class TestBaseBot:
assert isinstance(result, io.BytesIO)
assert result is custom
assert result.read() == b"\f" * 10
def test_get_file_url(self):
base_bot = BaseBot("42:TEST")
file_url = PRODUCTION.file_url(token="42:TEST", path="path")
assert file_url == base_bot.get_file_url("path")