o
    ˳iL                     @  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mZmZ d dlmZmZmZmZ d dl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,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 d	dl7m8Z8 dgZ9dddZ:G dd dZ;G dd de#Z<eG dd de;Z=dS )    )annotationsN)	dataclassfield)BufferedReaderFileIO)Path)AnyDictListLiteralOptionalUnioncast)AsyncClientHeadersHTTPStatusErrorResponse)URL   )DEFAULT_FILE_OPTIONSDEFAULT_SEARCH_OPTIONS)StorageApiError)
BaseBucketCreateSignedUploadUrlOptionsCreateSignedUrlResponseCreateSignedURLsOptionsDownloadOptionsFileOptionsListBucketFilesOptionsRequestMethodSearchV2OptionsSearchV2ResultSignedUploadURLSignedUrlJsonResponseSignedUrlResponseSignedUrlsJsonResponseTransformOptions
UploadDataUploadResponseUploadSignedUrlFileOptions
URLOptionstransform_to_dict)StorageExceptionAsyncBucketpathstrreturntuple[str, ...]c                 C  s0   t | }|js|jd dkr|jdd  S |jS )Nr   /   )r   absoluteparts)r.   url r7   L/home/ubuntu/.local/lib/python3.10/site-packages/storage3/_async/file_api.pyrelative_path_to_parts+   s   r9   c                   @  s   e Zd ZU dZded< ded< ded< ded	< 	
	
	
	
dbdcddZ	
ddded d!Z	
dddfd(d)Zdgd.d/Z	
dddhd3d4Z		
dddid9d:Z
	
dddjd;d<Zdkd?d@ZdkdAdBZdldDdEZdmdGdHZdndJdKZ	
	
dodpdNdOZ	
dddqdRdSZ	
	
dodrdWdXZ	
dddsd\d]Z	
dddtd^d_Z	
dddtd`daZd
S )uAsyncBucketActionsMixinz(Functions needed to access the file API.r/   idr   	_base_urlr   _clientr   _headersNmethodr   r.   	list[str]headersOptional[dict[str, Any]]jsonOptional[dict[Any, Any]]filesOptional[Any]query_paramsOptional[dict[str, str]]kwargsr   r0   r   c                   s  z.| j j| |}|pt }|| j | jj|t|f|||d|I d H }	|		  W n8 t
yg }
 z,z|
j }t|d |d |d |
 tyb } zd|j }t|dd|d }~ww d }
~
ww |rd|v rt|d d	 tr|d d	   |	S )
N)rA   rC   rE   messageerror
statusCodezUnable to parse error message: InternalErrori  filer3   )r<   joinpath
with_querydictupdater>   r=   requestr/   raise_for_statusr   responserC   r   KeyErrortext
isinstancer   close)selfr?   r.   rA   rC   rE   rG   rI   url_pathrU   excresperrrJ   r7   r7   r8   _request:   sD   


z AsyncBucketActionsMixin._requestoptions&Optional[CreateSignedUploadUrlOptions]r"   c           	        s   t  }|dur|jr|d|ji t|}| jdddd| jg||dI dH }| }tj	t
| jtt
|d d	 }tj|j}|d
sPtd| | |d
 d |dS )a  
        Creates a signed upload URL.

        Parameters
        ----------
        path
            The file path, including the file name. For example `folder/image.png`.
        options
            Additional options for the upload url creation.
        Nx-upsertPOSTobjectuploadsign)rA   r6   r2   tokenzNo token sent by the APIr   )
signed_url	signedUrlrg   r.   )rQ   upsertrR   r9   r_   r;   rC   urllibparseurlparser/   r<   r   lstripparse_qsquerygetr,   geturl)	rZ   r.   r`   rA   
path_partsrU   datafull_urlrG   r7   r7   r8   create_signed_upload_urla   s(   

z0AsyncBucketActionsMixin.create_signed_upload_urlrg   rN   /Union[BufferedReader, bytes, FileIO, str, Path]file_options$Optional[UploadSignedUrlFileOptions]r(   c                   s   t |}d|i}ddd| jg|}|pi }|d}	i }
|	r*d|	 |d< d|	i}
i | jjt|}|d }t|tsGt|tsGt|t	rRd	|||
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
        rg   rd   re   rf   cache-controlmax-age=cacheControlrN   content-typerbPUT)rE   rA   rt   rG   NKeyr.   r   )r9   r;   rq   r=   rA   r   rX   r   bytesr   popopenr_   rC   r(   )rZ   r.   rg   rN   rx   rs   rG   	final_urlr`   cache_control_datarA   filename_filerU   rt   r7   r7   r8   upload_to_signed_url   sR   
z,AsyncBucketActionsMixin.upload_to_signed_urlrh   download_querydict[str, str]r$   c                 C  s4   t |dd  }| j||}t|t|dS )Nr3   )	signedURLri   )r   r<   joinextend_queryr/   )rZ   rh   r   r6   r   r7   r7   r8   _make_signed_url   s   z(AsyncBucketActionsMixin._make_signed_url
expires_inintOptional[URLOptions]c                   s   dt |i}i }|pi }|d }r%|d|i d|du r"dn|i}|d }r3|d|i t|}	| jddd| jg|	|d	I d
H }
t|
j}| 	|j
|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 	transformrc   rd   rf   rC   N)r/   rq   rR   r9   r_   r;   r#   model_validate_jsoncontentr   r   )rZ   r.   r   r`   rC   r   url_optionsr   r   rs   rU   rt   r7   r7   r8   create_signed_url   s"   z)AsyncBucketActionsMixin.create_signed_urlpaths	List[str]!Optional[CreateSignedURLsOptions]List[CreateSignedUrlResponse]c                   s   |t |d}i }|pi }|d }r&|d|i d|du r#dn|i}| jddd| jg|dI d	H }t|j}	g }
|	D ]}| |j	|}|j
|j|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.
        )r   r   r   Tr   rc   rd   rf   r   Nr   )rK   r.   r   ri   )r/   rq   rR   r_   r;   r%   validate_jsonr   r   r   rK   r.   append)rZ   r   r   r`   rC   r   r   r   rU   rt   signed_urlsitemr6   signed_itemr7   r7   r8   create_signed_urls   s2   
z*AsyncBucketActionsMixin.create_signed_urlsc                   s   i }|pi }| d }rd|du rdn|i}| dr!ddgndg}| d }r/t|nt }t|}	| jjg |d| j|	R  ||}
t	|
S )	z
        Parameters
        ----------
        path
            file path, including the path and file name. For example `folder/image.png`.
        r   Tr   r   renderimagerd   public)
rq   r+   rQ   r9   r<   rO   r;   rP   r   r/   )rZ   r.   r`   r   r   r   render_pathttransformationrs   r6   r7   r7   r8   get_public_url  s   	z&AsyncBucketActionsMixin.get_public_url	from_pathto_pathc                   .   | j dddg| 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`.
        rc   rd   movebucketId	sourceKeydestinationKeyr   Nr_   r;   rC   rZ   r   r   resr7   r7   r8   r   <     	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`.
        rc   rd   copyr   r   Nr   r   r7   r7   r8   r   R  r   zAsyncBucketActionsMixin.copylist[dict[str, Any]]c                   s*   | j dd| jg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`].
        DELETErd   prefixesr   Nr   )rZ   r   rU   r7   r7   r8   removeh  s   	zAsyncBucketActionsMixin.removedict[str, Any]c                   s0   t |}| ddd| jg|I dH }| S )z
        Lists info for a particular file.

        Parameters
        ----------
        path
            The path to the file.
        GETrd   infoN)r9   r_   r;   rC   rZ   r.   rs   rU   r7   r7   r8   r   x  s   
zAsyncBucketActionsMixin.infoboolc                   sJ   zt |}| dd| jg|I dH }|jdkW S  tjy$   Y dS w )z
        Returns True if the file exists, False otherwise.

        Parameters
        ----------
        path
            The path to the file.
        HEADrd   N   F)r9   r_   r;   status_coderC   JSONDecodeErrorr   r7   r7   r8   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d| jg||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prefixr   rc   rd   list)rC   rA   N)r   r_   r;   rC   )rZ   r.   r`   extra_optionsextra_headersbodyrU   r7   r7   r8   r     s"   
zAsyncBucketActionsMixin.listOptional[SearchV2Options]r!   c                   s<   |ri |ni }| j ddd| jg|dI d H }t|jS )Nrc   rd   zlist-v2r   )r_   r;   r!   r   r   )rZ   r`   r   rU   r7   r7   r8   list_v2  s   
zAsyncBucketActionsMixin.list_v2Optional[DownloadOptions]Optional[Dict[str, str]]r   c           	        sv   |pt  }|drg dndg}|dpt }t|}| jdg || j|i t||p1i 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`.
        r   )r   r   authenticatedrd   r   )rG   N)r   rq   r&   r9   r_   r;   r+   r   )	rZ   r.   r`   rG   r   r   transform_optionsrs   rU   r7   r7   r8   r     s$   

z AsyncBucketActionsMixin.downloadLiteral['POST', 'PUT']r1   Optional[FileOptions]c                   sn  |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= |d	 }|rod
| |
d< |d|i t
|ts~t
|ts~t
|trd|||
 dfi}nd|t|d|
 dfi}| j|d| jg|||
|dI dH }| }td||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.
        Nrz   rj   rb   metadatarA   z
x-metadatarc   r}   r{   r|   rN   r~   r   rd   )rE   rA   rt   r2   r   r   )r   rR   r=   rA   r   rC   dumpsbase64	b64encodeencoderX   r   r   r   r   r_   r;   r(   r   )rZ   r?   r.   rN   rx   r   r   rj   r   file_opts_headersrA   metadata_strr   rE   rU   rt   r7   r7   r8   _upload_or_update  s^   
z)AsyncBucketActionsMixin._upload_or_updatec                   s    t |}| d|||I dH S )r   rc   Nr9   r   rZ   r.   rN   rx   rs   r7   r7   r8   re   >  s   zAsyncBucketActionsMixin.uploadc                   s    t |}| d|||I d H S )Nr   r   r   r7   r7   r8   rR   T  s   zAsyncBucketActionsMixin.update)NNNN)r?   r   r.   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   r   r0   r   )N)r.   r/   r`   ra   r0   r"   )
r.   r/   rg   r/   rN   rw   rx   ry   r0   r(   )rh   r/   r   r   r0   r$   )r.   r/   r   r   r`   r   r0   r$   )r   r   r   r   r`   r   r0   r   )r.   r/   r`   r   r0   r/   )r   r/   r   r/   r0   r   )r   r@   r0   r   )r.   r/   r0   r   )r.   r/   r0   r   )NN)r.   r   r`   r   r0   r   )r`   r   r0   r!   )r.   r/   r`   r   rG   r   r0   r   )
r?   r   r.   r1   rN   rw   rx   r   r0   r(   )r.   r/   rN   rw   rx   r   r0   r(   )__name__
__module____qualname____doc____annotations__r_   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   re   rR   r7   r7   r7   r8   r:   2   sR   
 **
E$.




 'Ur:   c                   @  s   e Zd ZdZdS )r-   zRepresents a storage bucket.N)r   r   r   r   r7   r7   r7   r8   r-   ^  s    c                   @  s<   e Zd ZU dZded< ded< 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;   r   r<   r   r>   F)reprr   r=   N)r   r   r   r   r   r   r=   r7   r7   r7   r8   r   b  s   
 r   )r.   r/   r0   r1   )>
__future__r   r   rC   urllib.parserk   dataclassesr   r   ior   r   pathlibr   typingr   r	   r
   r   r   r   r   httpxr   r   r   r   yarlr   	constantsr   r   
exceptionsr   typesr   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   utilsr,   __all__r9   r:   r-   r   r7   r7   r7   r8   <module>   s0    $X
    0