2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Add support for editing photousing file IDs

This commit is contained in:
Dan 2018-08-03 18:38:26 +02:00
parent a3be6a9355
commit 51eb2f90b9

View File

@ -16,9 +16,12 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import binascii
import os import os
import struct
from pyrogram.api import functions, types from pyrogram.api import functions, types
from pyrogram.api.errors import FileIdInvalid
from pyrogram.client.ext import BaseClient, utils from pyrogram.client.ext import BaseClient, utils
from pyrogram.client.types import ( from pyrogram.client.types import (
InputMediaPhoto InputMediaPhoto
@ -55,6 +58,28 @@ class EditMessageMedia(BaseClient):
media = types.InputMediaPhotoExternal( media = types.InputMediaPhotoExternal(
url=media.media url=media.media
) )
else:
try:
decoded = utils.decode(media.media)
fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
unpacked = struct.unpack(fmt, decoded)
except (AssertionError, binascii.Error, struct.error):
raise FileIdInvalid from None
else:
if unpacked[0] != 2:
media_type = BaseClient.MEDIA_TYPE_ID.get(unpacked[0], None)
if media_type:
raise FileIdInvalid("The file_id belongs to a {}".format(media_type))
else:
raise FileIdInvalid("Unknown media type: {}".format(unpacked[0]))
media = types.InputMediaPhoto(
id=types.InputPhoto(
id=unpacked[2],
access_hash=unpacked[3]
)
)
r = self.send( r = self.send(
functions.messages.EditMessage( functions.messages.EditMessage(