
    Yi=                         S r SSKrSSKJr  SSKJr  SSKJr  SSK	J
r
JrJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJr  \
(       a  SSKJr   " S S\5      rg)z?This module contains an object that represents a Telegram File.    N)	b64decode)Path)TYPE_CHECKINGBinaryIOOptional)decrypt)TelegramObject)DEFAULT_NONE)is_local_file)FilePathInputJSONDictODVInput)FileCredentialsc                     ^  \ rS rSrSrSr  SSS.S\S\S\\   S	\\   S
\\	   4
U 4S jjjjr
S\4S jrS\S\4S jr S\\\\S.S\\   S\\   S\\   S\\   S\\   S\4S jjjr\\\\S.S\S\\   S\\   S\\   S\\   SS4S jjr S\\\\S.S\\   S\\   S\\   S\\   S\\   S\4S jjjrSS jrSrU =r$ )File$   a4  
This object represents a file ready to be downloaded. The file can be e.g. downloaded with
:attr:`download_to_drive`. It is guaranteed that the link will be valid for at least 1 hour.
When the link expires, a new one can be requested by calling :meth:`telegram.Bot.get_file`.

Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`file_unique_id` is equal.

.. versionchanged:: 20.0
    ``download`` was split into :meth:`download_to_drive` and :meth:`download_to_memory`.

Note:
    * Maximum file size to download is
      :tg-const:`telegram.constants.FileSizeLimit.FILESIZE_DOWNLOAD`.
    * If you obtain an instance of this class from :attr:`telegram.PassportFile.get_file`,
      then it will automatically be decrypted as it downloads when you call e.g.
      :meth:`download_to_drive`.

Args:
    file_id (:obj:`str`): Identifier for this file, which can be used to download
        or reuse the file.
    file_unique_id (:obj:`str`): Unique identifier for this file, which
        is supposed to be the same over time and for different bots.
        Can't be used to download or reuse the file.
    file_size (:obj:`int`, optional): File size in bytes, if known.
    file_path (:obj:`str`, optional): File path. Use e.g. :meth:`download_to_drive` to get the
        file.

Attributes:
    file_id (:obj:`str`): Identifier for this file, which can be used to download
        or reuse the file.
    file_unique_id (:obj:`str`): Unique identifier for this file, which
        is supposed to be the same over time and for different bots.
        Can't be used to download or reuse the file.
    file_size (:obj:`int`): Optional. File size in bytes, if known.
    file_path (:obj:`str`): Optional. File path. Use e.g. :meth:`download_to_drive` to get the
        file.
)_credentialsfile_id	file_path	file_sizefile_unique_idN
api_kwargsr   r   r   r   r   c                   > [         TU ]  US9  [        U5      U l        [        U5      U l        X0l        X@l        S U l        U R                  4U l        U R                  5         g )Nr   )
super__init__strr   r   r   r   r   	_id_attrs_freeze)selfr   r   r   r   r   	__class__s         ?/app/.venv/lib/python3.13/site-packages/telegram/_files/file.pyr   File.__init__T   sZ     	J/  L#&~#6(1(17;--/    returnc           	      <   [         R                  " [        U R                  5      5      n[         R                  " [         R
                  " UR                  UR                  [         R                  " UR                  5      UR                  UR                  5      5      $ )zPConvert any UTF-8 char in :obj:`File.file_path` into a url encoded ASCII string.)urllib_parseurlsplitr   r   
urlunsplitSplitResultschemenetlocquotepathqueryfragment)r    sress     r"   _get_encoded_urlFile._get_encoded_urll   si    $$S%89&&$$T[[,*<*<TYY*GUYUbUb
 	
r$   bufc                     [        [        U R                  R                  5      [        U R                  R                  5      U5      $ N)r   r   r   secrethash)r    r4   s     r"   _prepare_decryptFile._prepare_decryptu   s4    y!2!2!9!9:IdFWFWF\F\<]_bccr$   read_timeoutwrite_timeoutconnect_timeoutpool_timeoutcustom_pathr<   r=   r>   r?   c                  #    U R                   (       d  [        S5      e[        U R                   5      nU(       a  SOU R                  5       nU(       a  U R                  (       a  [        U R                   5      nU R                  UR                  5       5      n	Ub  [        U5      n
O.[        [        UR                  5      S-   UR                  -   5      n
U
R                  U	5        U
$ Ub<  U(       a5  [        R                  " U R                   [        U5      5        [        U5      $ U(       a  [        U5      nODU(       a  [        U R                   5      $ [        [        U R                   5      R                  5      nU R                  5       R                  R!                  UUUUUS9I Sh  vN n	U R                  (       a  U R                  U	5      n	UR                  U	5        U$  N97f)a
  
Download this file. By default, the file is saved in the current working directory with
:attr:`file_path` as file name. If :paramref:`custom_path` is supplied as a :obj:`str` or
:obj:`pathlib.Path`, it will be saved to that path.

Note:
    If :paramref:`custom_path` isn't provided and :attr:`file_path` is the path of a
    local file (which is the case when a Bot API Server is running in local mode), this
    method will just return the path.

    The only exception to this are encrypted files (e.g. a passport file). For these, a
    file with the prefix `decrypted_` will be created in the same directory as the
    original file in order to decrypt the file without changing the existing one
    in-place.

.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`

.. versionchanged:: 20.0

    * :paramref:`custom_path` parameter now also accepts :class:`pathlib.Path` as argument.
    * Returns :class:`pathlib.Path` object in cases where previously a :obj:`str` was
      returned.
    * This method was previously called ``download``. It was split into
      :meth:`download_to_drive` and :meth:`download_to_memory`.

.. versionchanged:: 21.7
    Raises :exc:`RuntimeError` if :attr:`file_path` is not set. Note that files without
    a :attr:`file_path` could never be downloaded, as this attribute is mandatory for that
    operation.

Args:
    custom_path (:class:`pathlib.Path` | :obj:`str` , optional): The path where the file
        will be saved to. If not specified, will be saved in the current working directory
        with :attr:`file_path` as file name or the :attr:`file_id` if :attr:`file_path`
        is not set.

Keyword Args:
    read_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.read_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
    write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.write_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
    connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.connect_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
    pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.pool_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.

Returns:
    :class:`pathlib.Path`: Returns the Path object the file was downloaded to.

Raises:
    RuntimeError: If :attr:`file_path` is not set.

9No `file_path` available for this file. Can not download.Nz/decrypted_r;   )r   RuntimeErrorr   r2   r   r   r9   
read_bytesr   parentnamewrite_bytesshutilcopyfileget_botrequestretrieve)r    r@   r<   r=   r>   r?   
local_fileurlfile_to_decryptr4   r.   filenames               r"   download_to_driveFile.download_to_drivex   s    D ~~Z[["4>>2
 dd&;&;&= $++"4>>2O''(B(B(DEC&K(C 6 67-G/J^J^^_S!K"zOODNNC,<=$$K(H''D0556HLLN**33%'+% 4 
 
 '',CS!
s   F+G)-G'.:G)outc                  #    U R                   (       d  [        S5      e[        U R                   5      nU(       a  SOU R                  5       nU(       a  [	        U R                   5      OSnU(       a  UR                  5       n	O3U R                  5       R                  R                  UUUUUS9I Sh  vN n	U R                  (       a  U R                  U	5      n	UR                  U	5        g N87f)a  
Download this file into memory. :paramref:`out` needs to be supplied with a
:obj:`io.BufferedIOBase`, the file contents will be saved to that object using the
:obj:`out.write<io.BufferedIOBase.write>` method.

.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`

Hint:
    If you want to immediately read the data from ``out`` after calling this method, you
    should call ``out.seek(0)`` first. See also :meth:`io.IOBase.seek`.

.. versionadded:: 20.0

.. versionchanged:: 21.7
    Raises :exc:`RuntimeError` if :attr:`file_path` is not set. Note that files without
    a :attr:`file_path` could never be downloaded, as this attribute is mandatory for that
    operation.

Args:
    out (:obj:`io.BufferedIOBase`): A file-like object. Must be opened for writing in
        binary mode.

Keyword Args:
    read_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.read_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
    write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.write_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
    connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.connect_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
    pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.pool_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.

Raises:
    RuntimeError: If :attr:`file_path` is not set.
rB   Nr;   )r   rC   r   r2   r   rD   rJ   rK   rL   r   r9   write)
r    rS   r<   r=   r>   r?   rM   rN   r.   r4   s
             r"   download_to_memoryFile.download_to_memory   s     ` ~~Z[["4>>2
 dd&;&;&='1tDNN#t//#C..77)+ /) 8  C '',C		#s   B/C,1C*29C,c                  #    U R                   (       d  [        S5      eUc
  [        5       n[        U R                   5      (       a$  [	        U R                   5      R                  5       nOAU R                  5       R                  R                  U R                  5       UUUUS9I Sh  vN nU R                  (       a"  UR                  U R                  U5      5        U$ UR                  U5        U$  NJ7f)a%  Download this file and return it as a bytearray.

.. versionchanged:: 21.7
    Raises :exc:`RuntimeError` if :attr:`file_path` is not set. Note that files without
    a :attr:`file_path` could never be downloaded, as this attribute is mandatory for that
    operation.

Args:
    buf (:obj:`bytearray`, optional): Extend the given bytearray with the downloaded data.

Keyword Args:
    read_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.read_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.

        .. versionadded:: 20.0
    write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.write_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.

        .. versionadded:: 20.0
    connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.connect_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.

        .. versionadded:: 20.0
    pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
        :paramref:`telegram.request.BaseRequest.post.pool_timeout`. Defaults to
        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.

        .. versionadded:: 20.0

Returns:
    :obj:`bytearray`: The same object as :paramref:`buf` if it was specified. Otherwise a
    newly allocated :obj:`bytearray`.

Raises:
    RuntimeError: If :attr:`file_path` is not set.

rB   Nr;   )r   rC   	bytearrayr   r   rD   rJ   rK   rL   r2   r   extendr9   )r    r4   r<   r=   r>   r?   
bytes_datas          r"   download_as_bytearrayFile.download_as_bytearray&  s     b ~~Z[[;+C((dnn-88:J#||~55>>%%')+ /)  ?   J JJt,,Z89 
 JJz"
s   B$C3&C1'AC3c                     Xl         g)zySets the passport credentials for the file.

Args:
    credentials (:class:`telegram.FileCredentials`): The credentials.
N)r   )r    credentialss     r"   set_credentialsFile.set_credentialsm  s
     (r$   )r   r   r   r   r   r   )NNr6   )r_   r   r%   N)__name__
__module____qualname____firstlineno____doc__	__slots__r   r   intr   r   r2   bytesr9   r
   r   r   floatr   rQ   r   rV   rY   r\   r`   __static_attributes____classcell__)r!   s   @r"   r   r   $   s   %NI $(#' *.  C=	
 C= X& 0
# 
dE de d
 04h )5)5+7(4hm,h uo	h
  h "%h uoh 
h\ )5)5+7(4BB uo	B
  B "%B uoB 
BL $(E )5)5+7(4Ei E uo	E
  E "%E uoE 
EN( (r$   r   )rf   rH   urllib.parseparser'   base64r   pathlibr   typingr   r   r   telegram._passport.credentialsr   telegram._telegramobjectr	   telegram._utils.defaultvaluer
   telegram._utils.filesr   telegram._utils.typesr   r   r   telegramr   r    r$   r"   <module>ry      sC   & F  #   4 4 2 3 5 / C C(O(> O(r$   