From f2ef0bfcebd372e8e8c9655cd45d79ad5c27f453 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 26 Jan 2022 12:24:45 +0100 Subject: [PATCH] Make upload_file private --- readthedocs/misc/v2-migration-guide.rst | 2 + telethon/_client/messages.py | 4 +- telethon/_client/telegramclient.py | 103 +++---------------- telethon/_client/uploads.py | 74 +++++++++++++ telethon_generator/data/friendly.csv | 1 - telethon_generator/parsers/tlobject/tlarg.py | 6 +- 6 files changed, 95 insertions(+), 95 deletions(-) diff --git a/readthedocs/misc/v2-migration-guide.rst b/readthedocs/misc/v2-migration-guide.rst index 337377cb..79681ff0 100644 --- a/readthedocs/misc/v2-migration-guide.rst +++ b/readthedocs/misc/v2-migration-guide.rst @@ -777,3 +777,5 @@ self-produced updates like getmessage now also trigger a handler input_peer removed from get_me; input peers should remain mostly an impl detail raw api types and fns are now immutable. this can enable optimizations in the future. + +upload_file has been removed from the public methods. it's a low-level method users should not need to use. diff --git a/telethon/_client/messages.py b/telethon/_client/messages.py index 250582d0..0997a643 100644 --- a/telethon/_client/messages.py +++ b/telethon/_client/messages.py @@ -476,10 +476,10 @@ async def send_message( # TODO album if message._file._should_upload_thumb(): - message._file._set_uploaded_thumb(await self.upload_file(message._file._thumb)) + message._file._set_uploaded_thumb(await self._upload_file(message._file._thumb)) if message._file._should_upload_file(): - message._file._set_uploaded_file(await self.upload_file(message._file._file)) + message._file._set_uploaded_file(await self._upload_file(message._file._file)) request = _tl.fn.messages.SendMedia( entity, message._file._media, reply_to_msg_id=reply_to, message=message._text, diff --git a/telethon/_client/telegramclient.py b/telethon/_client/telegramclient.py index 3196c1eb..67141622 100644 --- a/telethon/_client/telegramclient.py +++ b/telethon/_client/telegramclient.py @@ -2983,8 +2983,6 @@ class TelegramClient: message with media before, you can use its ``message.media`` as a file here). - * A handle to an uploaded file (from `upload_file`). - * A :tl:`InputMedia` instance. For example, if you want to send a dice use :tl:`InputMediaDice`, or if you want to send a contact use :tl:`InputMediaContact`. @@ -3157,93 +3155,6 @@ class TelegramClient: )) """ - @forward_call(uploads.upload_file) - async def upload_file( - self: 'TelegramClient', - file: 'hints.FileLike', - *, - part_size_kb: float = None, - file_size: int = None, - file_name: str = None, - use_cache: type = None, - key: bytes = None, - iv: bytes = None, - progress_callback: 'hints.ProgressCallback' = None) -> '_tl.TypeInputFile': - """ - Uploads a file to Telegram's servers, without sending it. - - .. note:: - - Generally, you want to use `send_file` instead. - - This method returns a handle (an instance of :tl:`InputFile` or - :tl:`InputFileBig`, as required) which can be later used before - it expires (they are usable during less than a day). - - Uploading a file will simply return a "handle" to the file stored - remotely in the Telegram servers, which can be later used on. This - will **not** upload the file to your own chat or any chat at all. - - Arguments - file (`str` | `bytes` | `file`): - The path of the file, byte array, or stream that will be sent. - Note that if a byte array or a stream is given, a filename - or its type won't be inferred, and it will be sent as an - "unnamed application/octet-stream". - - part_size_kb (`int`, optional): - Chunk size when uploading files. The larger, the less - requests will be made (up to 512KB maximum). - - file_size (`int`, optional): - The size of the file to be uploaded, which will be determined - automatically if not specified. - - If the file size can't be determined beforehand, the entire - file will be read in-memory to find out how large it is. - - file_name (`str`, optional): - The file name which will be used on the resulting InputFile. - If not specified, the name will be taken from the ``file`` - and if this is not a `str`, it will be ``"unnamed"``. - - use_cache (`type`, optional): - This parameter currently does nothing, but is kept for - backward-compatibility (and it may get its use back in - the future). - - key ('bytes', optional): - In case of an encrypted upload (secret chats) a key is supplied - - iv ('bytes', optional): - In case of an encrypted upload (secret chats) an iv is supplied - - progress_callback (`callable`, optional): - A callback function accepting two parameters: - ``(sent bytes, total)``. - - Returns - :tl:`InputFileBig` if the file size is larger than 10MB, - `InputSizedFile ` - (subclass of :tl:`InputFile`) otherwise. - - Example - .. code-block:: python - - # Photos as photo and document - file = await client.upload_file('photo.jpg') - await client.send_file(chat, file) # sends as photo - await client.send_file(chat, file, force_document=True) # sends as document - - file.name = 'not a photo.jpg' - await client.send_file(chat, file, force_document=True) # document, new name - - # As song or as voice note - file = await client.upload_file('song.ogg') - await client.send_file(chat, file) # sends as song - await client.send_file(chat, file, voice_note=True) # sends as voice note - """ - # endregion Uploads # region Users @@ -3508,4 +3419,18 @@ class TelegramClient: async def _replace_session_state(self, *, save=True, **changes): pass + @forward_call(uploads.upload_file) + async def _upload_file( + self: 'TelegramClient', + file: 'hints.FileLike', + *, + part_size_kb: float = None, + file_size: int = None, + file_name: str = None, + use_cache: type = None, + key: bytes = None, + iv: bytes = None, + progress_callback: 'hints.ProgressCallback' = None) -> '_tl.TypeInputFile': + pass + # endregion Private diff --git a/telethon/_client/uploads.py b/telethon/_client/uploads.py index a92df8f4..218e6351 100644 --- a/telethon/_client/uploads.py +++ b/telethon/_client/uploads.py @@ -235,6 +235,80 @@ async def upload_file( key: bytes = None, iv: bytes = None, progress_callback: 'hints.ProgressCallback' = None) -> '_tl.TypeInputFile': + """ + Uploads a file to Telegram's servers, without sending it. + + .. note:: + + Generally, you want to use `send_file` instead. + + This method returns a handle (an instance of :tl:`InputFile` or + :tl:`InputFileBig`, as required) which can be later used before + it expires (they are usable during less than a day). + + Uploading a file will simply return a "handle" to the file stored + remotely in the Telegram servers, which can be later used on. This + will **not** upload the file to your own chat or any chat at all. + + Arguments + file (`str` | `bytes` | `file`): + The path of the file, byte array, or stream that will be sent. + Note that if a byte array or a stream is given, a filename + or its type won't be inferred, and it will be sent as an + "unnamed application/octet-stream". + + part_size_kb (`int`, optional): + Chunk size when uploading files. The larger, the less + requests will be made (up to 512KB maximum). + + file_size (`int`, optional): + The size of the file to be uploaded, which will be determined + automatically if not specified. + + If the file size can't be determined beforehand, the entire + file will be read in-memory to find out how large it is. + + file_name (`str`, optional): + The file name which will be used on the resulting InputFile. + If not specified, the name will be taken from the ``file`` + and if this is not a `str`, it will be ``"unnamed"``. + + use_cache (`type`, optional): + This parameter currently does nothing, but is kept for + backward-compatibility (and it may get its use back in + the future). + + key ('bytes', optional): + In case of an encrypted upload (secret chats) a key is supplied + + iv ('bytes', optional): + In case of an encrypted upload (secret chats) an iv is supplied + + progress_callback (`callable`, optional): + A callback function accepting two parameters: + ``(sent bytes, total)``. + + Returns + :tl:`InputFileBig` if the file size is larger than 10MB, + `InputSizedFile ` + (subclass of :tl:`InputFile`) otherwise. + + Example + .. code-block:: python + + # Photos as photo and document + file = await client.upload_file('photo.jpg') + await client.send_file(chat, file) # sends as photo + await client.send_file(chat, file, force_document=True) # sends as document + + file.name = 'not a photo.jpg' + await client.send_file(chat, file, force_document=True) # document, new name + + # As song or as voice note + file = await client.upload_file('song.ogg') + await client.send_file(chat, file) # sends as song + await client.send_file(chat, file, voice_note=True) # sends as voice note + """ if isinstance(file, (_tl.InputFile, _tl.InputFileBig)): return file # Already uploaded diff --git a/telethon_generator/data/friendly.csv b/telethon_generator/data/friendly.csv index 950a8bd7..4d53c862 100644 --- a/telethon_generator/data/friendly.csv +++ b/telethon_generator/data/friendly.csv @@ -23,5 +23,4 @@ messages.MessageMethods,delete_messages,channels.deleteMessages messages.deleteM messages.MessageMethods,send_read_acknowledge,messages.readMentions channels.readHistory messages.readHistory updates.UpdateMethods,catch_up,updates.getDifference updates.getChannelDifference uploads.UploadMethods,send_file,messages.sendMedia messages.sendMultiMedia messages.uploadMedia -uploads.UploadMethods,upload_file,upload.saveFilePart upload.saveBigFilePart users.UserMethods,get_entity,users.getUsers messages.getChats channels.getChannels contacts.resolveUsername diff --git a/telethon_generator/parsers/tlobject/tlarg.py b/telethon_generator/parsers/tlobject/tlarg.py index 4a86fca9..0c4c7464 100644 --- a/telethon_generator/parsers/tlobject/tlarg.py +++ b/telethon_generator/parsers/tlobject/tlarg.py @@ -37,7 +37,7 @@ KNOWN_NAMED_EXAMPLES = { ('lang_code', 'string'): "'en'", ('chat_id', 'int'): '478614198', ('client_id', 'long'): 'random.randrange(-2**63, 2**63)', - ('video', 'InputFile'): "client.upload_file('/path/to/file.mp4')", + ('video', 'InputFile'): "client._upload_file('/path/to/file.mp4')", } KNOWN_TYPED_EXAMPLES = { @@ -50,8 +50,8 @@ KNOWN_TYPED_EXAMPLES = { 'double': '7.13', 'Bool': 'False', 'true': 'True', - 'InputChatPhoto': "client.upload_file('/path/to/photo.jpg')", - 'InputFile': "client.upload_file('/path/to/file.jpg')", + 'InputChatPhoto': "client._upload_file('/path/to/photo.jpg')", + 'InputFile': "client._upload_file('/path/to/file.jpg')", 'InputPeer': "'username'" }