From bea1ba62439b72f1e8f3dcb1cdbeebe6c07431db Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Wed, 18 Mar 2020 11:45:16 +0500 Subject: [PATCH] :sparkles: Add get_file_url --- aiogram/api/client/base.py | 15 ++++++++++++++- aiogram/api/types/mixins.py | 2 +- tests/test_api/test_client/test_base_bot.py | 7 +++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/aiogram/api/client/base.py b/aiogram/api/client/base.py index c21403e7..12c6bcb4 100644 --- a/aiogram/api/client/base.py +++ b/aiogram/api/client/base.py @@ -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 diff --git a/aiogram/api/types/mixins.py b/aiogram/api/types/mixins.py index 1f478ce0..2b295aef 100644 --- a/aiogram/api/types/mixins.py +++ b/aiogram/api/types/mixins.py @@ -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 diff --git a/tests/test_api/test_client/test_base_bot.py b/tests/test_api/test_client/test_base_bot.py index be5ce795..8e0738db 100644 --- a/tests/test_api/test_client/test_base_bot.py +++ b/tests/test_api/test_client/test_base_bot.py @@ -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")