o
    zsgA                     @  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)StorageException
SyncClient
SyncBucketc                   @  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SyncBucketActionsMixinz(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           
   
   K  s   z| j j||f|p
i ||d|}|  W n ty4 } z|j }	t|	d |	d |	d d }~ww |rLd|v rLt|d d trL|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;   S/var/www/html/minecraft/env/lib/python3.10/site-packages/storage3/_sync/file_api.py_request&   s"   	

zSyncBucketActionsMixin._requestpathr   c                 C  sx   |  |}| dd| }| }tjt| jj|d  }tj	|j
}|ds0t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/r%   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   
	

z/SyncBucketActionsMixin.create_signed_upload_urlrA   r1   /Union[BufferedReader, bytes, FileIO, str, Path]file_optionsOptional[FileOptions]r   c                 C  s  |  |}tjd| }tjd|i}|  d| }|du r%i }|d}	i }
|	r9d|	 |d< d|	i}
i | jjt	|}|j
dd	d
d }t|ts[t|ts[t|trfd|||dfi}nd|t|d|dfi}| jd||||
d}| }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   sJ   


z+SyncBucketActionsMixin.upload_to_signed_url
expires_inintoptionsr   dict[str, str]c           	      C  s   dt |i}d}|dr'|d|d i |ddu rdnd|d }|dr5|d|d i | |}| jdd| |d	}| }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(   	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(SyncBucketActionsMixin.create_signed_urlpaths	list[str]r   list[dict[str, str]]c           
      C  s   |t |d}d}|dr)|d|di |ddu r!dnd|d }| jdd| j |d}| }|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   ry   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)SyncBucketActionsMixin.create_signed_urlsc           	      C  s   g }d}| dr| ddu rdnd| d }|r!|| | dr(dnd}| dr8tj| dnd}|rA|| 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%SyncBucketActionsMixin.get_public_url	from_pathto_pathc                 C  "   | j dd| j||dd}| 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   r=   r"   r(   r8   r   r   resr;   r;   r<   move     	zSyncBucketActionsMixin.movec                 C  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   r   r   r;   r;   r<   copy$  r   zSyncBucketActionsMixin.copylistlist[dict[str, Any]]c                 C  s$   | j dd| j d|id}| 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   r   )r8   r   r5   r;   r;   r<   remove:  s   	
zSyncBucketActionsMixin.removec                 C  s"   |  dd| j d| }| S )z
        Lists info for a particular file.

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

        Parameters
        ----------
        path
            The path to the file.
        HEADr   rX      )r=   r"   status_coder   r;   r;   r<   exists\  s
   
zSyncBucketActionsMixin.existsOptional[str] Optional[ListBucketFilesOptions]c                 C  sJ   |pi }ddi}i t |d|pdi}| jdd| j ||d}| 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&   )r   r=   r"   r(   )r8   r>   rp   extra_optionsextra_headersbodyr5   r;   r;   r<   r   n  s    
zSyncBucketActionsMixin.listr   rd   c                 C  sd   | drdnd}tj| dpi }|rd| nd}| |}| d| d| | }|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   )rJ   rD   rE   rb   rC   r=   content)r8   r>   rp   r   r   r   rL   r5   r;   r;   r<   rt     s   

zSyncBucketActionsMixin.downloadLiteral['POST', 'PUT']c                 C  sr  |du ri }| dd}i }| dd}|r|d|i | dd}| dd}	i | jjt|}
|rJt|}t|	 |
d< |d|i |	rS|
i |	 |dkrZ|
d= |j
d	d
dd }|rsd| |
d< |d|i t|tst|tst|trd|||
 dfi}nd|t|d|
 dfi}| |}| j|d| ||
|d}| }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(SyncBucketActionsMixin._upload_or_updatec                 C     |  d|||S )r   r?   r   r8   r>   r1   rR   r;   r;   r<   upload  s   zSyncBucketActionsMixin.uploadc                 C  r   )Nr^   r   r   r;   r;   r<   rz   
  s   zSyncBucketActionsMixin.updatec                 C  s   | j  d| S )NrX   )r"   )r8   r>   r;   r;   r<   rC     s   z&SyncBucketActionsMixin._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 )	SyncBucketProxyzPA 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