diff --git a/telethon/utils.py b/telethon/utils.py index a5f95e9a..8d9cadc6 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -15,8 +15,10 @@ import os import pathlib import re import struct +from tg_file_id.file_id import FileId, DocumentFileId, PhotoFileId from collections import namedtuple from mimetypes import guess_extension +from typing import Union from types import GeneratorType from .extensions import markdown, html @@ -1101,104 +1103,85 @@ def resolve_bot_file_id(file_id): For thumbnails, the photo ID and hash will always be zero. """ - data = _rle_decode(_decode_telegram_base64(file_id)) - if not data: + if not file_id: return None - - # This isn't officially documented anywhere, but - # we assume the last byte is some kind of "version". - data, version = data[:-1], data[-1] - if version not in (2, 4): + # end if + if isinstance(file_id, str): + file = FileId.from_file_id(file_id) + elif not isinstance(file_id, FileId): return None + # end if - if (version == 2 and len(data) == 24) or (version == 4 and len(data) == 25): - if version == 2: - file_type, dc_id, media_id, access_hash = struct.unpack(' no attribute + elif file.type_id == FileId.TYPE_STICKER: attributes.append(types.DocumentAttributeSticker( alt='', stickerset=types.InputStickerSetEmpty() )) - elif file_type == 10: + elif file.type_id == FileId.TYPE_ANIMATION: attributes.append(types.DocumentAttributeAnimated()) + # end if return types.Document( - id=media_id, - access_hash=access_hash, + id=file.id, + access_hash=file.access_hash, date=None, mime_type='', size=0, thumbs=None, - dc_id=dc_id, + dc_id=file.dc_id, attributes=attributes, - file_reference=b'' + file_reference=file.file_reference if file.has_reference else b'' ) - elif (version == 2 and len(data) == 44) or (version == 4 and len(data) in (49, 77)): - if version == 2: - (file_type, dc_id, media_id, access_hash, - volume_id, secret, local_id) = struct.unpack('