o
    -˜Žj&!  ã                   @   s¤   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZmZmZ d dlZd dlmZ d dlmZ d dlmZ e e¡ZG dd	„ d	ƒZdS )
é    N)Úmd5)ÚPurePath)ÚUnionÚBinaryIOÚCallable)ÚStopTransmission)Úraw)ÚSessionc                   @   s@   e Zd Z				ddddeeef deded	ed
efdd„Z	dS )ÚSaveFileNr   © Úselfzpyrogram.ClientÚpathÚfile_idÚ	file_partÚprogressÚprogress_argsc                 ƒ   s$  �ˆj 4 I dH �š{ |du r	 W d  ƒI dH  dS ‡ fdd„‰d}t|ttfƒr/t|dƒ}nt|tjƒr8|}ntdƒ‚t|ddƒ}| 	d	tj
¡ | ¡ }	| 	d	¡ |	d	krZtd
ƒ‚ˆjjr`dnd}
|	|
d d krrtd|
› d�ƒ‚tt |	| ¡ƒ}|	dk}|rƒdnd}|du}|pŽˆ ¡ }|s–|s–tƒ nd}tˆˆj ¡ I dH ˆj ¡ I dH ˆj ¡ I dH dd�‰‡‡‡fdd„t|ƒD ƒ}t d¡‰ �z‡z½ˆ ¡ I dH  | 	|| ¡ 	 | |¡}|sï|sî|sîd dd„ | ¡ D ƒ¡}n”|rýtj j!j"||||d�}n
tj j!j#|||d�}ˆ  $|¡I dH  |�rHW W |D ]}ˆ  $d¡I dH  �qtj%|Ž I dH  ˆ &¡ I dH  t|ttfƒ�r=| '¡  W d  ƒI dH  dS |�sS|�sS| (|¡ |d7 }|�r‚t)j*|t+|| |	ƒ|	g|¢R Ž }t, -|¡�rw|ƒ I dH  nˆj. /ˆj0|¡I dH  q×W n  t1�y�   ‚  t2�y¤ } zt3 4|¡ W Y d}~nƒd}~ww |�råtj5j6|||d�W |D ]}ˆ  $d¡I dH  �q³tj%|Ž I dH  ˆ &¡ I dH  t|ttfƒ�rÚ| '¡  W  d  ƒI dH  S tj5j7||||d�W |D ]}ˆ  $d¡I dH  �qñtj%|Ž I dH  ˆ &¡ I dH  t|ttfƒ�r| '¡  W  d  ƒI dH  S W |D ]}ˆ  $d¡I dH  �q&tj%|Ž I dH  ˆ &¡ I dH  t|ttfƒ�rM| '¡  n+|D ]}ˆ  $d¡I dH  �qPtj%|Ž I dH  ˆ &¡ I dH  t|ttfƒ�rx| '¡  w w W d  ƒI dH  dS 1 I dH �s‹w   Y  dS )aŠ	  Upload a file onto Telegram servers, without actually sending the message to anyone.
        Useful whenever an InputFile type is required.

        .. note::

            This is a utility method intended to be used **only** when working with raw
            :obj:`functions <pyrogram.api.functions>` (i.e: a Telegram API method you wish to use which is not
            available yet in the Client class as an easy-to-use method).

        .. include:: /_includes/usable-by/users-bots.rst

        Parameters:
            path (``str`` | ``BinaryIO``):
                The path of the file you want to upload that exists on your local machine or a binary file-like object
                with its attribute ".name" set for in-memory uploads.

            file_id (``int``, *optional*):
                In case a file part expired, pass the file_id and the file_part to retry uploading that specific chunk.

            file_part (``int``, *optional*):
                In case a file part expired, pass the file_id and the file_part to retry uploading that specific chunk.

            progress (``Callable``, *optional*):
                Pass a callback function to view the file transmission progress.
                The function must take *(current, total)* as positional arguments (look at Other Parameters below for a
                detailed description) and will be called back each time a new file chunk has been successfully
                transmitted.

            progress_args (``tuple``, *optional*):
                Extra custom arguments for the progress callback function.
                You can pass anything you need to be available in the progress callback scope; for example, a Message
                object or a Client instance in order to edit the message with the updated progress status.

        Other Parameters:
            current (``int``):
                The amount of bytes transmitted so far.

            total (``int``):
                The total size of the file.

            *args (``tuple``, *optional*):
                Extra custom arguments as defined in the ``progress_args`` parameter.
                You can either keep ``*args`` or add every single extra argument in your function signature.

        Returns:
            ``InputFile``: On success, the uploaded file is returned in form of an InputFile object.

        Raises:
            RPCError: In case of a Telegram RPC error.
        Nc              
   “   sd   �	 ˆ   ¡ I d H }|d u rd S z
|  |¡I d H  W n ty0 } zt |¡ W Y d }~nd }~ww q)N)ÚgetÚinvokeÚ	ExceptionÚlogÚ	exception)ÚsessionÚdataÚe)Úqueuer   ú`/var/www/kodo/Anonymous/send/lib/python3.10/site-packages/pyrogram/methods/advanced/save_file.pyÚworkere   s   €€ÿøz"SaveFile.save_file.<locals>.workeri   ÚrbzPInvalid file. Expected a file path as string or a binary (not text) file pointerÚnamezfile.jpgr   zFile size equals to 0 Bi   iÐ  i   zCan't upload files bigger than z MiBi    é   é   T)Úis_mediac                    s   g | ]
}ˆ j  ˆˆƒ¡‘qS r   )ÚloopÚcreate_task)Ú.0Ú_)r   r   r   r   r   Ú
<listcomp>’   s    z&SaveFile.save_file.<locals>.<listcomp>Ú c                 S   s"   g | ]}t |ƒd d…  d ¡‘qS )é   N)ÚhexÚzfill)r$   Úir   r   r   r&   Ÿ   s   " )r   r   Úfile_total_partsÚbytes)r   r   r-   )ÚidÚpartsr   )r.   r/   r   Úmd5_checksum)8Úsave_file_semaphoreÚ
isinstanceÚstrr   ÚopenÚioÚIOBaseÚ
ValueErrorÚgetattrÚseekÚSEEK_ENDÚtellÚmeÚ
is_premiumÚintÚmathÚceilÚrnd_idr   r	   ÚstorageÚdc_idÚauth_keyÚ	test_modeÚrangeÚasyncioÚQueueÚstartÚreadÚjoinÚdigestr   Ú	functionsÚuploadÚSaveBigFilePartÚSaveFilePartÚputÚgatherÚstopÚcloseÚupdateÚ	functoolsÚpartialÚminÚinspectÚiscoroutinefunctionr"   Úrun_in_executorÚexecutorr   r   r   r   ÚtypesÚInputFileBigÚ	InputFile)r   r   r   r   r   r   Ú	part_sizeÚfpÚ	file_nameÚ	file_sizeÚfile_size_limit_mibr,   Úis_bigÚworkers_countÚis_missing_partÚmd5_sumÚworkersÚchunkÚrpcr%   Úfuncr   r   )r   r   r   r   r   Ú	save_file'   s
  €:þ
þ

üý' �þT
ýüÕ&€ÿý �þrü �þh€ø
ÿ€ �0ÿzSaveFile.save_file)Nr   Nr   )
Ú__name__Ú
__module__Ú__qualname__r   r3   r   r>   r   Útuplerm   r   r   r   r   r
   &   s$    úÿ
þýüûúr
   )rG   rV   rY   r5   Úloggingr?   Úhashlibr   Úpathlibr   Útypingr   r   r   Úpyrogramr   r   Úpyrogram.sessionr	   Ú	getLoggerrn   r   r
   r   r   r   r   Ú<module>   s   
