o
    zsgB                     @  s  d dl mZ d dlZd dl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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 ddlmZmZ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Z,eG dd de+Z-dS )    )annotationsN)	dataclassfield)BufferedReaderFileIO)Path)AnyLiteralOptionalUnioncast)HTTPStatusErrorResponse   )DEFAULT_FILE_OPTIONSDEFAULT_SEARCH_OPTIONS)StorageApiError)

BaseBucketCreateSignedURLsOptionsDownloadOptionsFileOptionsListBucketFilesOptionsRequestMethodSignedUploadURL
UploadDataUploadResponse
URLOptions)AsyncClientStorageExceptionAsyncBucketc                   @  s   e Zd ZU dZded< ded< 			dQdRddZdSddZ	dTdUd d!Zi fdVd'd(Zi fdWd-d.Z	i fdXd/d0Z
dYd3d4ZdYd5d6ZdZd9d:Zd[d;d<Zd\d>d?Z		d]d^dBdCZi fd_dFdGZ	dTd`dIdJZ	dTdadKdLZ	dTdadMdNZdbdOdPZdS )c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 n ty8 } z|j }	t|	d |	d |	d d }~ww |rPd|v rPt|d d trP|d d 	  |S )N)r&   r(   r*   messageerror
statusCodefile   )
r#   requestraise_for_statusr   responser(   r   
isinstancer   close)
selfr$   r%   r&   r(   r*   r,   r5   excresp r;   T/var/www/html/minecraft/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_urlrA   r>   )_get_final_pathr=   r(   urllibparseurlparser!   r#   base_urlparse_qsquerygetr   geturl)r8   r>   _pathr5   datafull_urlquery_paramsr;   r;   r<   create_signed_upload_url>   s   
	

z0AsyncBucketActionsMixin.create_signed_upload_urlrA   r1   /Union[BufferedReader, bytes, FileIO, str, Path]file_optionsOptional[FileOptions]r   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 }| }t||dd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
        r@   rA   ?Ncache-controlmax-age=cacheControl/r2   maxsplitr1   content-typerbPUTr*   r&   rM   Keyr>   r`   )rC   rD   rE   rF   	urlencoderK   rJ   r#   r&   r   rsplitr6   r   bytesr   popopenr=   r(   r   )r8   r>   rA   r1   rR   rL   _urlrO   	final_urlcache_control_datar&   filename_filer5   rM   r;   r;   r<   upload_to_signed_urlV   sL   


z,AsyncBucketActionsMixin.upload_to_signed_url
expires_inintoptionsr   dict[str, str]c           	        s   dt |i}d}|dr(|d|d i |ddu r dnd|d }|dr6|d|d i | |}| jdd| |d	I d
H }| }tj|d }tj	|j
d|j  }| jj tt |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 downloadT
&download=	transformr?   /object/sign/r(   N	signedURLrT   rX   )r!   rJ   updaterC   r=   r(   rD   rE   rF   quoter>   rI   r#   rG   r   lstrip)	r8   r>   rn   rp   r(   download_queryr5   rM   r%   r;   r;   r<   create_signed_url   s.   


z)AsyncBucketActionsMixin.create_signed_urlpaths	list[str]r   list[dict[str, str]]c           
        s   |t |d}d}|dr*|d|di |ddu r"dnd|d }| jdd| j |dI d	H }| }|D ])}tj|d
 }	tj	|	j
d|	j  }	| jj tt |	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.
        )r   rr   rs   rt   Tru   r?   rw   rx   Nry   rT   rX   )r!   rJ   rz   r=   r"   r(   rD   rE   rF   r{   r>   rI   r#   rG   r   r|   )
r8   r   rn   rp   r(   r}   r5   rM   itemr%   r;   r;   r<   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`.
        rs   rt   Tru   rv   zrender/imageobjectN&rT   z/public/)	rJ   appendrD   rE   rb   joinrC   r#   rG   )	r8   r>   rp   _query_stringr}   render_pathtransformation_queryquery_stringrL   r;   r;   r<   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`.
        r?   z/object/movebucketId	sourceKeydestinationKeyrx   Nr=   r"   r(   r8   r   r   resr;   r;   r<   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`.
        r?   z/object/copyr   rx   Nr   r   r;   r;   r<   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/prefixesrx   Nr   )r8   r   r5   r;   r;   r<   remove:  s   	
zAsyncBucketActionsMixin.removec                   s*   |  dd| j d| I dH }| S )z
        Lists info for a particular file.

        Parameters
        ----------
        path
            The path to the file.
        GET/object/info/rX   Nr   r8   r>   r5   r;   r;   r<   infoJ  s   
zAsyncBucketActionsMixin.infoboolc                   s,   |  dd| j d| I dH }|jdkS )z
        Returns True if the file exists, False otherwise.

        Parameters
        ----------
        path
            The path to the file.
        HEADr   rX   N   )r=   r"   status_coder   r;   r;   r<   exists\  s   

zAsyncBucketActionsMixin.exists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`, `sortBy` and `search`.
        zContent-Typezapplication/jsonprefixrs   r?   z/object/list/)r(   r&   N)r   r=   r"   r(   )r8   r>   rp   extra_optionsextra_headersbodyr5   r;   r;   r<   r   n  s"   
zAsyncBucketActionsMixin.listr   rd   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`.
        rv   zrender/image/authenticatedr   rT   rs   r   rX   N)rJ   rD   rE   rb   rC   r=   content)r8   r>   rp   r   r   r   rL   r5   r;   r;   r<   rt     s   


z AsyncBucketActionsMixin.downloadLiteral['POST', 'PUT']c                   sz  |du ri }| dd}i }| dd}|r|d|i | dd}| dd}	i | jjt|}
|rKt|}t|	 |
d< |d|i |	rT|
i |	 |dkr[|
d= |j
d	d
dd }|rtd| |
d< |d|i t|tst|tst|trd|||
 dfi}nd|t|d|
 dfi}| |}| j|d| ||
|dI dH }| }t||dd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.
        NrU   upsertzx-upsertmetadatar&   z
x-metadatar?   rX   r2   rY   r[   rV   rW   r1   r\   r]   r   r_   r`   ra   )re   rz   r#   r&   r   r(   dumpsbase64	b64encodeencoderc   r6   r   rd   r   rf   rC   r=   r   rJ   )r8   r$   r>   r1   rR   ri   rj   r   r   file_opts_headersr&   metadata_strrk   r*   rL   r5   rM   r;   r;   r<   _upload_or_update  s`   

z)AsyncBucketActionsMixin._upload_or_updatec                   s   |  d|||I dH S )r   r?   Nr   r8   r>   r1   rR   r;   r;   r<   upload  s   zAsyncBucketActionsMixin.uploadc                   s   |  d|||I d H S )Nr^   r   r   r;   r;   r<   rz   
  s   zAsyncBucketActionsMixin.updatec                 C  s   | j  d| S )NrX   )r"   )r8   r>   r;   r;   r<   rC     s   z'AsyncBucketActionsMixin._get_final_path)NNN)r$   r   r%   r!   r&   r'   r(   r)   r*   r+   r,   r   r-   r   )r>   r!   r-   r   )N)
r>   r!   rA   r!   r1   rQ   rR   rS   r-   r   )r>   r!   rn   ro   rp   r   r-   rq   )r   r   rn   ro   rp   r   r-   r   )r>   r!   rp   r   r-   r!   )r   r!   r   r!   r-   rq   )r   r   r-   r   )r>   r!   r-   r   )r>   r!   r-   r   )NN)r>   r   rp   r   r-   r   )r>   r!   rp   r   r-   rd   )
r$   r   r>   r!   r1   rQ   rR   rS   r-   r   )r>   r!   r1   rQ   rR   rS   r-   r   )r>   r!   r-   r!   )__name__
__module____qualname____doc____annotations__r=   rP   rm   r~   r   r   r   r   r   r   r   r   rt   r   r   rz   rC   r;   r;   r;   r<   r        s@   
 
C,)
"



Wr    Freprc                   @  s   e Zd ZdZdS )r   zRepresents a storage bucket.N)r   r   r   r   r;   r;   r;   r<   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#   r;   r;   r;   r<   r     s   
 r   ).
__future__r   r   r(   urllib.parserD   dataclassesr   r   ior   r   pathlibr   typingr   r	   r
   r   r   httpxr   r   	constantsr   r   
exceptionsr   typesr   r   r   r   r   r   r   r   r   r   utilsr   r   __all__r    r   r   r;   r;   r;   r<   <module>   s,    0   y