o
    iK                     @  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)Client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
SyncBucket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   U/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/storage3/_sync/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SyncBucketActionsMixinz(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                 K  s   z+| j j| |}|pt }|| j | jj|t|f|||d|}	|		  W n8 t
yc }
 z,z|
j }t|d |d |d |
 ty^ } 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:   sB   


zSyncBucketActionsMixin._requestoptions&Optional[CreateSignedUploadUrlOptions]r"   c           	      C  s   t  }|dur|jr|d|ji t|}| jdddd| jg||d}| }tj	t
| jtt
|d d	 }tj|j}|d
sLt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&   

z/SyncBucketActionsMixin.create_signed_upload_urlrg   rN   /Union[BufferedReader, bytes, FileIO, str, Path]file_options$Optional[UploadSignedUrlFileOptions]r(   c                 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sFt|tsFt|t	rQd	|||
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
        rg   rd   re   rf   cache-controlmax-age=cacheControlrN   content-typerbPUT)rE   rA   rt   rG   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   sP   
z+SyncBucketActionsMixin.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'SyncBucketActionsMixin._make_signed_url
expires_inintOptional[URLOptions]c                 C  s   dt |i}i }|pi }|d }r$|d|i d|du r!dn|i}|d }r2|d|i t|}	| jddd| jg|	|d	}
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   )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(SyncBucketActionsMixin.create_signed_urlpaths	List[str]!Optional[CreateSignedURLsOptions]List[CreateSignedUrlResponse]c                 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}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   r   )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   s0   
z)SyncBucketActionsMixin.create_signed_urlsc                 C  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%SyncBucketActionsMixin.get_public_url	from_pathto_pathc                 C  &   | j dddg| 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`.
        rc   rd   movebucketId	sourceKeydestinationKeyr   r_   r;   rC   rZ   r   r   resr7   r7   r8   r   :     	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`.
        rc   rd   copyr   r   r   r   r7   r7   r8   r   P  r   zSyncBucketActionsMixin.copylist[dict[str, Any]]c                 C  s"   | j dd| jg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`].
        DELETErd   prefixesr   r   )rZ   r   rU   r7   r7   r8   removef  s   	zSyncBucketActionsMixin.removedict[str, Any]c                 C  s(   t |}| ddd| jg|}| S )z
        Lists info for a particular file.

        Parameters
        ----------
        path
            The path to the file.
        GETrd   info)r9   r_   r;   rC   rZ   r.   rs   rU   r7   r7   r8   r   v  s   zSyncBucketActionsMixin.infoboolc                 C  sB   zt |}| dd| jg|}|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      F)r9   r_   r;   status_coderC   JSONDecodeErrorr   r7   r7   r8   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d| jg||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prefixr   rc   rd   list)rC   rA   )r   r_   r;   rC   )rZ   r.   r`   extra_optionsextra_headersbodyrU   r7   r7   r8   r     s    
zSyncBucketActionsMixin.listOptional[SearchV2Options]r!   c                 C  s4   |ri |ni }| j ddd| jg|d}t|jS )Nrc   rd   zlist-v2r   )r_   r;   r!   r   r   )rZ   r`   r   rU   r7   r7   r8   list_v2  s   
zSyncBucketActionsMixin.list_v2Optional[DownloadOptions]Optional[Dict[str, str]]r   c           	      C  sn   |pt  }|drg dndg}|dpt }t|}| jdg || j|i t||p0i 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`.
        r   )r   r   authenticatedrd   r   )rG   )r   rq   r&   r9   r_   r;   r+   r   )	rZ   r.   r`   rG   r   r   transform_optionsrs   rU   r7   r7   r8   r     s"   

zSyncBucketActionsMixin.downloadLiteral['POST', 'PUT']r1   Optional[FileOptions]c                 C  sf  |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= |d	 }|rnd
| |
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}| }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(SyncBucketActionsMixin._upload_or_updatec                 C     t |}| d|||S )r   rc   r9   r   rZ   r.   rN   rx   rs   r7   r7   r8   re   <  s   zSyncBucketActionsMixin.uploadc                 C  r   )Nr   r   r   r7   r7   r8   rR   R  s   zSyncBucketActionsMixin.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   sP   
 **
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 )SyncBucketProxyzPA 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   `  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
    .