mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Pass Bot instance explicitly to the URLInputFile
This commit is contained in:
parent
31c151d4a0
commit
08b263dec3
1 changed files with 22 additions and 3 deletions
|
|
@ -4,10 +4,21 @@ import io
|
|||
import os
|
||||
from abc import ABC, abstractmethod
|
||||
from pathlib import Path
|
||||
from typing import Any, AsyncGenerator, AsyncIterator, Dict, Iterator, Optional, Union
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
AsyncGenerator,
|
||||
AsyncIterator,
|
||||
Dict,
|
||||
Optional,
|
||||
Union,
|
||||
)
|
||||
|
||||
import aiofiles
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from aiogram.client.bot import Bot
|
||||
|
||||
DEFAULT_CHUNK_SIZE = 64 * 1024 # 64 kb
|
||||
|
||||
|
||||
|
|
@ -114,6 +125,7 @@ class URLInputFile(InputFile):
|
|||
filename: Optional[str] = None,
|
||||
chunk_size: int = DEFAULT_CHUNK_SIZE,
|
||||
timeout: int = 30,
|
||||
bot: "Bot" = None,
|
||||
):
|
||||
"""
|
||||
Represents object for streaming files from internet
|
||||
|
|
@ -122,6 +134,9 @@ class URLInputFile(InputFile):
|
|||
:param headers: HTTP Headers
|
||||
:param filename: Filename to be propagated to telegram.
|
||||
:param chunk_size: Uploading chunk size
|
||||
:param timeout: Timeout for downloading
|
||||
:param bot: Bot instance to use HTTP session from.
|
||||
If not specified, will be used current bot from context.
|
||||
"""
|
||||
super().__init__(filename=filename, chunk_size=chunk_size)
|
||||
if headers is None:
|
||||
|
|
@ -130,11 +145,15 @@ class URLInputFile(InputFile):
|
|||
self.url = url
|
||||
self.headers = headers
|
||||
self.timeout = timeout
|
||||
self.bot = bot
|
||||
|
||||
async def read(self, chunk_size: int) -> AsyncGenerator[bytes, None]:
|
||||
from aiogram.client.bot import Bot
|
||||
bot = self.bot
|
||||
if bot is None:
|
||||
from aiogram.client.bot import Bot
|
||||
|
||||
bot = Bot.get_current(no_error=False)
|
||||
|
||||
bot = Bot.get_current(no_error=False)
|
||||
stream = bot.session.stream_content(
|
||||
url=self.url,
|
||||
headers=self.headers,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue