o
    ~jh:                     @  s  d dl mZ d dlZd dlmZmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZ d dlmZmZ d	d
lmZmZ d	dlmZmZmZmZmZm Z m!Z!m"Z" d	dl#m$Z$m%Z% dgZ&G dd dZ'eddG dd dee'Z(eG dd de'Z)dS )    )annotationsN)	dataclassfield)BufferedReaderFileIO)JSONDecodeError)Path)AnyLiteralOptionalUnioncast)	HTTPErrorResponse   )DEFAULT_FILE_OPTIONSDEFAULT_SEARCH_OPTIONS)
BaseBucketCreateSignedURLsOptionsDownloadOptionsFileOptionsListBucketFilesOptionsRequestMethodSignedUploadURL
URLOptions)AsyncClientStorageExceptionAsyncBucketc                   @  s   e Zd ZU dZded< ded< 			dKdLddZdMddZ	dNdOdd Zi fdPd&d'Zi fdQd,d-Z	i fdRd.d/Z
dSd2d3ZdSd4d5ZdTd8d9Z		dUdVd<d=Zi fdWd@dAZ	dNdXdCdDZ	dNdYdEdFZ	dNdYdGdHZdZdIdJZdS )[AsyncBucketActionsMixinz(Functions needed to access the file API.stridr   _clientNmethodr   urlheadersOptional[dict[str, Any]]jsonOptional[dict[Any, Any]]filesOptional[Any]kwargsr	   returnr   c           	        s   z| j j||f|pi ||d|I d H }|  W |S  tyA   z| }ti |d|ji ty@   td|jiw w )N)r$   r&   r(   
statusCode)r!   requestraise_for_statusr   r&   r   status_coder   )	selfr"   r#   r$   r&   r(   r*   responseresp r3   N/var/www/html/bot/env/lib/python3.10/site-packages/storage3/_async/file_api.py_request"   s&   	

z AsyncBucketActionsMixin._requestpathr   c                   s   |  |}| dd| I dH }| }tjt| jj|d  }tj	|j
}|ds4td| |d d |dS )	z
        Creates a signed upload URL.

        Parameters
        ----------
        path
            The file path, including the file name. For example `folder/image.png`.
        POST/object/upload/sign/Nr#   tokenzNo token sent by the APIr   )
signed_urlr9   r6   )_get_final_pathr5   r&   urllibparseurlparser   r!   base_urlparse_qsquerygetr   geturl)r0   r6   _pathr1   datafull_urlquery_paramsr3   r3   r4   create_signed_upload_url9   s   
	

z0AsyncBucketActionsMixin.create_signed_upload_urlr9   file/Union[BufferedReader, bytes, FileIO, str, Path]file_optionsOptional[FileOptions]c                   s  |  |}tjd| }tjd|i}|  d| }|du r&i }|d}	i }
|	r:d|	 |d< d|	i}
i | jjt	|}|j
dd	d
d }t|ts\t|ts\t|trgd|||dfi}nd|t|d|dfi}| jd||||
dI dH S )a  
        Upload a file with a token generated from :meth:`.create_signed_url`

        Parameters
        ----------
        path
            The file path, including the file name
        token
            The token generated from :meth:`.create_signed_url`
        file
            The file contents or a file-like object to upload
        file_options
            Additional options for the uploaded file
        r8   r9   ?Ncache-controlmax-age=cacheControl/   maxsplitrI   content-typerbPUTr(   r$   rE   )r;   r<   r=   r>   	urlencoderC   rB   r!   r$   r   rsplit
isinstancer   bytesr   popopenr5   )r0   r6   r9   rI   rK   rD   _urlrG   	final_urlcache_control_datar$   filename_filer3   r3   r4   upload_to_signed_urlQ   sH   


z,AsyncBucketActionsMixin.upload_to_signed_url
expires_inintoptionsr   dict[str, str]c                   s   dt |i}|dr|d|d i |dr#|d|d i | |}| jdd| |dI dH }| }| jj tt |d 	d	 |d< |S )
a/  
        Parameters
        ----------
        path
            file path to be downloaded, including the current file name.
        expires_in
            number of seconds until the signed URL expires.
        options
            options to be passed for downloading or transforming the file.
        	expiresIndownload	transformr7   /object/sign/r&   N	signedURLrQ   )
r   rB   updater;   r5   r&   r!   r?   r   lstrip)r0   r6   rg   ri   r&   r1   rE   r3   r3   r4   create_signed_url   s    


z)AsyncBucketActionsMixin.create_signed_urlpaths	list[str]r   list[dict[str, str]]c                   s   |t |d}|dr|d|di | jdd| j |dI dH }| }|D ]}| jj tt |d 	d |d< q,|S )	a  
        Parameters
        ----------
        path
            file path to be downloaded, including the current file name.
        expires_in
            number of seconds until the signed URL expires.
        options
            options to be passed for downloading the file.
        )rt   rk   rl   r7   rn   ro   Nrp   rQ   )
r   rB   rq   r5   r    r&   r!   r?   r   rr   )r0   rt   rg   ri   r&   r1   rE   itemr3   r3   r4   create_signed_urls   s   

z*AsyncBucketActionsMixin.create_signed_urlsc           	        s   g }d}| dr| ddu rdnd| d }|r"|| | dr)dnd}| dr9tj| dnd}|rB|| d|}d	| }| |}| jj | d
| | S )z
        Parameters
        ----------
        path
            file path, including the path and file name. For example `folder/image.png`.
        Nrl   Tz	download=rm   zrender/imageobject&rM   z/public/)	rB   appendr<   r=   rZ   joinr;   r!   r?   )	r0   r6   ri   _query_stringdownload_queryrender_pathtransformation_queryquery_stringrD   r3   r3   r4   get_public_url   s*   





z&AsyncBucketActionsMixin.get_public_url	from_pathto_pathc                   *   | j dd| j||ddI dH }| S )aa  
        Moves an existing file, optionally renaming it at the same time.

        Parameters
        ----------
        from_path
            The original file path, including the current file name. For example `folder/image.png`.
        to_path
            The new file path, including the new file name. For example `folder/image-copy.png`.
        r7   z/object/movebucketId	sourceKeydestinationKeyro   Nr5   r    r&   r0   r   r   resr3   r3   r4   move      	zAsyncBucketActionsMixin.movec                   r   )aZ  
        Copies an existing file to a new path in the same bucket.

        Parameters
        ----------
        from_path
            The original file path, including the current file name. For example `folder/image.png`.
        to_path
            The new file path, including the new file name. For example `folder/image-copy.png`.
        r7   z/object/copyr   ro   Nr   r   r3   r3   r4   copy  r   zAsyncBucketActionsMixin.copylistlist[dict[str, Any]]c                   s,   | j dd| j d|idI dH }| S )z
        Deletes files within the same bucket

        Parameters
        ----------
        paths
            An array or list of files to be deletes, including the path and file name. For example [`folder/image.png`].
        DELETE/object/prefixesro   Nr   )r0   rt   r1   r3   r3   r4   remove  s   	
zAsyncBucketActionsMixin.removeOptional[str] Optional[ListBucketFilesOptions]c                   sR   |pi }ddi}i t |d|pdi}| jdd| j ||dI dH }| S )	z
        Lists all the files within a bucket.

        Parameters
        ----------
        path
            The folder path.
        options
            Search options, including `limit`, `offset`, and `sortBy`.
        zContent-Typezapplication/jsonprefix r7   z/object/list/)r&   r$   N)r   r5   r    r&   )r0   r6   ri   extra_optionsextra_headersbodyr1   r3   r3   r4   r   *  s"   
zAsyncBucketActionsMixin.listr   r]   c                   sl   | drdnd}tj| dpi }|rd| nd}| |}| d| d| | I dH }|jS )	z
        Downloads a file.

        Parameters
        ----------
        path
            The file path to be downloaded, including the path and file name. For example `folder/image.png`.
        rm   zrender/image/authenticatedry   rM   r   GETrQ   N)rB   r<   r=   rZ   r;   r5   content)r0   r6   ri   r   r   r   rD   r1   r3   r3   r4   rl   H  s   


z AsyncBucketActionsMixin.downloadLiteral['POST', 'PUT']c                   s  |du ri }| d}i }| dr |d| di |d= i | jjt|}|dkr1|d= |jdddd	 }|rGd
| |d< d|i}t|tsVt|tsVt|t	rad|||
dfi}	nd|t|d|
dfi}	| |}
| j|d|
 |	||dI dH S )  
        Uploads a file to an existing bucket.

        Parameters
        ----------
        path
            The relative file path including the bucket ID. Should be of the format `bucket/folder/subfolder/filename.png`.
            The bucket must already exist before attempting to upload.
        file
            The File object to be stored in the bucket. or a async generator of chunks
        file_options
            HTTP headers.
        NrN   upsertzx-upsertr7   rQ   rR   rS   rU   rO   rP   rI   rV   rW   r   rY   )rB   rq   r!   r$   r   r[   r\   r   r]   r   r^   r_   r;   r5   )r0   r"   r6   rI   rK   rb   rc   r$   rd   r(   rD   r3   r3   r4   _upload_or_update^  sL   


z)AsyncBucketActionsMixin._upload_or_updatec                   s   |  d|||I dH S )r   r7   Nr   r0   r6   rI   rK   r3   r3   r4   upload  s   zAsyncBucketActionsMixin.uploadc                   s   |  d|||I d H S )NrX   r   r   r3   r3   r4   rq     s   zAsyncBucketActionsMixin.updatec                 C  s   | j  d| S )NrQ   )r    )r0   r6   r3   r3   r4   r;     s   z'AsyncBucketActionsMixin._get_final_path)NNN)r"   r   r#   r   r$   r%   r&   r'   r(   r)   r*   r	   r+   r   )r6   r   r+   r   )N)
r6   r   r9   r   rI   rJ   rK   rL   r+   r   )r6   r   rg   rh   ri   r   r+   rj   )rt   ru   rg   rh   ri   r   r+   rv   )r6   r   ri   r   r+   r   )r   r   r   r   r+   rj   )rt   r   r+   r   )NN)r6   r   ri   r   r+   rv   )r6   r   ri   r   r+   r]   )
r"   r   r6   r   rI   rJ   rK   rL   r+   r   )r6   r   rI   rJ   rK   rL   r+   r   )r6   r   r+   r   )__name__
__module____qualname____doc____annotations__r5   rH   rf   rs   rx   r   r   r   r   r   rl   r   r   rq   r;   r3   r3   r3   r4   r      s<   
 
@ 
"

Gr   Freprc                   @  s$   e Zd ZU dZeddZded< dS )r   zRepresents a storage bucket.Fr   r   r!   N)r   r   r   r   r   r!   r   r3   r3   r3   r4   r     s   
 c                   @  s,   e Zd ZU dZded< eddZded< dS )	AsyncBucketProxyzPA bucket proxy, this contains the minimum required fields to query the File API.r   r    Fr   r   r!   N)r   r   r   r   r   r   r!   r3   r3   r3   r4   r     s   
 r   )*
__future__r   urllib.parser<   dataclassesr   r   ior   r   r&   r   pathlibr   typingr	   r
   r   r   r   httpxr   r   	constantsr   r   typesr   r   r   r   r   r   r   r   utilsr   r   __all__r   r   r   r3   r3   r3   r4   <module>   s(    (
   ,