Skip to content

Commit 35c1419

Browse files
committed
Fix BytesIO support by @doggyhaha
1 parent b592fec commit 35c1419

7 files changed

Lines changed: 18 additions & 16 deletions

File tree

pyrogram/methods/messages/edit_inline_media.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import os
2020
import re
21+
import io
2122

2223
from pyrogram import raw
2324
from pyrogram import types
@@ -73,7 +74,7 @@ async def edit_inline_media(
7374
parse_mode = media.parse_mode
7475

7576
if isinstance(media, types.InputMediaPhoto):
76-
if os.path.isfile(media.media):
77+
if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media):
7778
media = raw.types.InputMediaUploadedPhoto(
7879
file=await self.save_file(media.media)
7980
)
@@ -84,7 +85,7 @@ async def edit_inline_media(
8485
else:
8586
media = utils.get_input_media_from_file_id(media.media, FileType.PHOTO)
8687
elif isinstance(media, types.InputMediaVideo):
87-
if os.path.isfile(media.media):
88+
if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media):
8889
media = raw.types.InputMediaUploadedDocument(
8990
mime_type=self.guess_mime_type(media.media) or "video/mp4",
9091
thumb=await self.save_file(media.thumb),
@@ -108,7 +109,7 @@ async def edit_inline_media(
108109
else:
109110
media = utils.get_input_media_from_file_id(media.media, FileType.VIDEO)
110111
elif isinstance(media, types.InputMediaAudio):
111-
if os.path.isfile(media.media):
112+
if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media):
112113
media = raw.types.InputMediaUploadedDocument(
113114
mime_type=self.guess_mime_type(media.media) or "audio/mpeg",
114115
thumb=await self.save_file(media.thumb),
@@ -131,7 +132,7 @@ async def edit_inline_media(
131132
else:
132133
media = utils.get_input_media_from_file_id(media.media, FileType.AUDIO)
133134
elif isinstance(media, types.InputMediaAnimation):
134-
if os.path.isfile(media.media):
135+
if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media):
135136
media = raw.types.InputMediaUploadedDocument(
136137
mime_type=self.guess_mime_type(media.media) or "video/mp4",
137138
thumb=await self.save_file(media.thumb),
@@ -156,7 +157,7 @@ async def edit_inline_media(
156157
else:
157158
media = utils.get_input_media_from_file_id(media.media, FileType.ANIMATION)
158159
elif isinstance(media, types.InputMediaDocument):
159-
if os.path.isfile(media.media):
160+
if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media):
160161
media = raw.types.InputMediaUploadedDocument(
161162
mime_type=self.guess_mime_type(media.media) or "application/zip",
162163
thumb=await self.save_file(media.thumb),

pyrogram/methods/messages/edit_message_media.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import os
2020
import re
21+
import io
2122
from typing import Union
2223

2324
from pyrogram import raw
@@ -86,7 +87,7 @@ async def edit_message_media(
8687
message, entities = (await self.parser.parse(caption, parse_mode)).values()
8788

8889
if isinstance(media, types.InputMediaPhoto):
89-
if os.path.isfile(media.media):
90+
if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media):
9091
media = await self.send(
9192
raw.functions.messages.UploadMedia(
9293
peer=await self.resolve_peer(chat_id),
@@ -110,7 +111,7 @@ async def edit_message_media(
110111
else:
111112
media = utils.get_input_media_from_file_id(media.media, FileType.PHOTO)
112113
elif isinstance(media, types.InputMediaVideo):
113-
if os.path.isfile(media.media):
114+
if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media):
114115
media = await self.send(
115116
raw.functions.messages.UploadMedia(
116117
peer=await self.resolve_peer(chat_id),
@@ -147,7 +148,7 @@ async def edit_message_media(
147148
else:
148149
media = utils.get_input_media_from_file_id(media.media, FileType.VIDEO)
149150
elif isinstance(media, types.InputMediaAudio):
150-
if os.path.isfile(media.media):
151+
if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media):
151152
media = await self.send(
152153
raw.functions.messages.UploadMedia(
153154
peer=await self.resolve_peer(chat_id),
@@ -183,7 +184,7 @@ async def edit_message_media(
183184
else:
184185
media = utils.get_input_media_from_file_id(media.media, FileType.AUDIO)
185186
elif isinstance(media, types.InputMediaAnimation):
186-
if os.path.isfile(media.media):
187+
if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media):
187188
media = await self.send(
188189
raw.functions.messages.UploadMedia(
189190
peer=await self.resolve_peer(chat_id),
@@ -221,7 +222,7 @@ async def edit_message_media(
221222
else:
222223
media = utils.get_input_media_from_file_id(media.media, FileType.ANIMATION)
223224
elif isinstance(media, types.InputMediaDocument):
224-
if os.path.isfile(media.media):
225+
if isinstance(media.media, io.BytesIO) or os.path.isfile(media.media):
225226
media = await self.send(
226227
raw.functions.messages.UploadMedia(
227228
peer=await self.resolve_peer(chat_id),

pyrogram/types/input_media/input_media.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from typing import List
19+
from typing import List, Union, BinaryIO
2020

2121
from ..messages_and_media import MessageEntity
2222
from ..object import Object
@@ -36,7 +36,7 @@ class InputMedia(Object):
3636

3737
def __init__(
3838
self,
39-
media: str,
39+
media: Union[str, BinaryIO],
4040
caption: str = "",
4141
parse_mode: str = None,
4242
caption_entities: List[MessageEntity] = None

pyrogram/types/input_media/input_media_animation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class InputMediaAnimation(InputMedia):
2626
"""An animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent inside an album.
2727
2828
Parameters:
29-
media (``str``):
29+
media (``str`` | ``BinaryIO``):
3030
Animation to send.
3131
Pass a file_id as string to send a file that exists on the Telegram servers or
3232
pass a file path as string to upload a new file that exists on your local machine or

pyrogram/types/input_media/input_media_audio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class InputMediaAudio(InputMedia):
2828
It is intended to be used with :meth:`~pyrogram.Client.send_media_group`.
2929
3030
Parameters:
31-
media (``str``):
31+
media (``str`` | ``BinaryIO``):
3232
Audio to send.
3333
Pass a file_id as string to send an audio that exists on the Telegram servers or
3434
pass a file path as string to upload a new audio that exists on your local machine or

pyrogram/types/input_media/input_media_document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class InputMediaDocument(InputMedia):
2626
"""A generic file to be sent inside an album.
2727
2828
Parameters:
29-
media (``str``):
29+
media (``str`` | ``BinaryIO``):
3030
File to send.
3131
Pass a file_id as string to send a file that exists on the Telegram servers or
3232
pass a file path as string to upload a new file that exists on your local machine or

pyrogram/types/input_media/input_media_video.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class InputMediaVideo(InputMedia):
2727
It is intended to be used with :obj:`~pyrogram.Client.send_media_group`.
2828
2929
Parameters:
30-
media (``str``):
30+
media (``str`` | ``BinaryIO``):
3131
Video to send.
3232
Pass a file_id as string to send a video that exists on the Telegram servers or
3333
pass a file path as string to upload a new video that exists on your local machine or

0 commit comments

Comments
 (0)