
    gA                    D   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      Z+ ed       G d de             Z,e G d de+             Z-y)    )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                  l   e Zd ZU dZded<   ded<   	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZ	 d	 	 	 	 	 	 	 	 	 dd	Zi f	 	 	 	 	 	 	 dd
Zi f	 	 	 	 	 	 	 ddZ	i fddZ
d dZd dZd!dZ	 	 	 	 d"dZ	 	 	 	 d#dZ	 	 d$	 	 	 	 	 d%dZi fd&dZ	 d	 	 	 	 	 	 	 	 	 d'dZ	 d	 	 	 	 	 	 	 d(dZ	 d	 	 	 	 	 	 	 d(dZd)dZy)*SyncBucketActionsMixinz(Functions needed to access the file API.stridr   _clientNc                Z   	  | j                   j                  ||f|xs i ||d|}|j                          |r0d|v r,t        |d   d   t              r|d   d   j                          |S # t        $ r5}|j                  j                         }	t        |	d   |	d   |	d         d }~ww xY w)N)headersjsonfilesmessageerror
statusCodefile   )
r%   requestraise_for_statusr   responser(   r   
isinstancer   close)
selfmethodurlr'   r(   r)   kwargsr1   excresps
             L/var/www/openai/venv/lib/python3.12/site-packages/storage3/_sync/file_api.py_requestzSyncBucketActionsMixin._request&   s    	V+t||++%,]UNTH %%' Vu_E&M!4Dn)U&M!""$  	V<<$$&D!$y/4=$|BTUU	Vs   6A, ,	B*50B%%B*c                   | j                  |      }| j                  dd|       }|j                         }t        j                  j                  t        | j                  j                        |d   z         }t        j                  j                  |j                        }|j                  d      st        d      |j                         |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/r6   tokenzNo token sent by the APIr   )
signed_urlr?   path)_get_final_pathr;   r(   urllibparseurlparser#   r%   base_urlparse_qsquerygetr   geturl)r4   rA   _pathr1   datafull_urlquery_paramss          r:   create_signed_upload_urlz/SyncBucketActionsMixin.create_signed_upload_url>   s     $$T*==+?w)GH}}-3\\-B-B%%&e4.
 ||,,X^^<("#=>>"//+!'*1-
 	
    c                   | j                  |      }t        j                  j                  d|       }t        j                  j	                  d|i      }|j                          d| }|i }|j                  d      }	i }
|	rd|	 |d<   d|	i}
i | j                  j                  t        |}|j                  dd	      d
   }t        |t              s t        |t              st        |t              rd|||j                  d      fi}n d|t!        |d      |j                  d      fi}| j#                  d||||
      }|j%                         }t'        ||j                  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>   r?   ?cache-controlmax-age=cacheControl/r.   maxsplitr-   content-typerbPUTr)   r'   rL   KeyrA   r^   )rB   rC   rD   rE   	urlencoderJ   rI   r%   r'   r   rsplitr2   r   bytesr   popopenr;   r(   r   )r4   rA   r?   r-   file_optionsrK   _urlrN   	final_urlcache_control_datar'   filename_filer1   rL   s                   r:   upload_to_signed_urlz+SyncBucketActionsMixin.upload_to_signed_urlV   s   * $$T*||$$';E7%CD||--w.>?{{}oQ|n5	L$((9 .6}o,FL)#]3E
ll""
"
 

 ;;sQ;/3 t^,$&$' hgkk..IJKE t$KK/E ==9E7 ! 
 $==?4TXXe_==rP   c                   dt        |      i}d}|j                  d      r@ |j                  d|d   i       |j                  d      du rdnd|j                  d       }|j                  d      r |j                  d|d   i       | j                  |      }| j	                  dd| |	      }|j                         }t        j                  j                  |d
         }t        j                  j                  |j                        d|j                   z   }| j                  j                   t        t         |      j                  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(   	signedURLrR   rV   )r#   rI   updaterB   r;   r(   rC   rD   rE   quoterA   rH   r%   rF   r   lstrip)	r4   rA   
expires_inoptionsr(   download_queryr1   rL   r6   s	            r:   create_signed_urlz(SyncBucketActionsMixin.create_signed_url   sU    S_-;;z"DKKWZ%89: ;;z*d2 !'++j"9!:; 
 ;;{#DKKgk&:;<##D)==D6" ! 

 }} ll##D$56ll  *q_< ||$$%d3n&;&;C&@%A.AQR 	[ rP   c                ~   |t        |      d}d}|j                  d      rL |j                  d|j                  d      i       |j                  d      du rdnd|j                  d       }| j                  dd| j                   |      }|j                         }|D ]  }t        j                  j                  |d	         }	t        j                  j                  |	j                        d
|	j                   z   }	| j                  j                   t        t         |	      j                  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 the file.
        )pathsrn   ro   rp   Trq   r=   rs   rt   ru   rR   rV   )r#   rI   rv   r;   r$   r(   rC   rD   rE   rw   rA   rH   r%   rF   r   rx   )
r4   r~   ry   rz   r(   r{   r1   rL   itemr6   s
             r:   create_signed_urlsz)SyncBucketActionsMixin.create_signed_urls   s3    S_=;;z"DKKW[[%<=> ;;z*d2 !'++j"9!:;  ==DGG9% ! 

 }}D ,,''[(9:C,,$$SXX.1SYYK@C <<(()$sC.*?*?*D)EnEUV   rP   c                   g }d}|j                  d      r)|j                  d      du rdnd|j                  d       }|r|j                  |       |j                  d      rdnd}|j                  d      r.t        j                  j	                  |j                  d            nd}|r|j                  |       d	j                  |      }d
| }| j                  |      }| j                  j                   | d| | S )z
        Parameters
        ----------
        path
            file path, including the path and file name. For example `folder/image.png`.
        ro   rp   Trq   rr   zrender/imageobjectN&rR   z/public/)	rI   appendrC   rD   r`   joinrB   r%   rF   )	r4   rA   rz   _query_stringr{   render_pathtransformation_queryquery_stringrK   s	            r:   get_public_urlz%SyncBucketActionsMixin.get_public_url   s    ;;z" ;;z*d2 !'++j"9!:;    0(/K(@nh {{;' LL""7;;{#;< 	    !56xx.<.)$$T*,,''(XeW\NSSrP   c                f    | j                  dd| j                  ||d      }|j                         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destinationKeyrt   r;   r$   r(   r4   	from_pathto_pathress       r:   movezSyncBucketActionsMixin.move  =     mm GG&")  
 xxzrP   c                f    | j                  dd| j                  ||d      }|j                         S )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   rt   r   r   s       r:   copyzSyncBucketActionsMixin.copy$  r   rP   c                h    | j                  dd| j                   d|i      }|j                         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/prefixesrt   r   )r4   r~   r1   s      r:   removezSyncBucketActionsMixin.remove:  s>     ==twwi e$ ! 

 }}rP   c                f    | j                  dd| j                   d|       }|j                         S )z
        Lists info for a particular file.

        Parameters
        ----------
        path
            The path to the file.
        GET/object/info/rV   r   r4   rA   r1   s      r:   infozSyncBucketActionsMixin.infoJ  s7     ==DGG9AdV,
 }}rP   c                d    | j                  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   rV      )r;   r$   status_coder   s      r:   existszSyncBucketActionsMixin.exists\  s<     ==DGG9AdV,
 ##s**rP   c                    |xs i }ddi}i t         |d|xs di}| j                  dd| j                   ||      }|j                         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prefixro   r=   z/object/list/)r(   r'   )r   r;   r$   r(   )r4   rA   rz   extra_optionsextra_headersbodyr1   s          r:   listzSyncBucketActionsMixin.listn  sy      2');<
$

 djb

 ==DGG9%!	 ! 
 }}rP   c                   |j                  d      rdnd}t        j                  j                  |j                  d      xs i       }|rd| nd}| j	                  |      }| j                  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`.
        rr   zrender/image/authenticatedr   rR   ro   r   rV   )rI   rC   rD   r`   rB   r;   content)r4   rA   rz   r   r   r   rK   r1   s           r:   rp   zSyncBucketActionsMixin.download  s     -4KK,D(( 	  &||55gkk+6N6TRTU5I/01r$$T*==m1UGL>2
 rP   c                   |i }|j                  dd      }i }|j                  dd      }|r|j                  d|i       |j                  dd      }|j                  dd      }	i | j                  j                  t        |}
|rNt        j                  |      }t        j                  |j                               |
d<   |j                  d|i       |	r|
j                  i |	       |dk7  r|
d= |j                  d	d
      d   }|rd| |
d<   |j                  d|i       t        |t              s t        |t              st        |t              rd|||
j                  d      fi}n d|t        |d      |
j                  d      fi}| j!                  |      }| j#                  |d| ||
|      }|j                         }t%        ||j'                  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.
        NrS   upsertzx-upsertmetadatar'   z
x-metadatar=   rV   r.   rW   rY   rT   rU   r-   rZ   r[   r   r]   r^   r_   )rc   rv   r%   r'   r   r(   dumpsbase64	b64encodeencodera   r2   r   rb   r   rd   rB   r;   r   rI   )r4   r5   rA   r-   re   rh   ri   r   r   file_opts_headersr'   metadata_strrj   r)   rK   r1   rL   s                    r:   _upload_or_updatez(SyncBucketActionsMixin._upload_or_update  s   ( L$(($?!!(D1V 45##J5(,,Y=
ll""
"
 
 ::h/L$*$4$4\5H5H5J$KGL!LL*l34NN0/01 V
#;;sQ;/3)1-'AGO$LL.-89 t^,$&$' hgkk..IJKE t$KK/E $$T*==hug&eW5 ! 
 $==?4TXXe_==rP   c                *    | j                  d|||      S )r   r=   r   r4   rA   r-   re   s       r:   uploadzSyncBucketActionsMixin.upload  s    & %%fdD,GGrP   c                *    | j                  d|||      S )Nr\   r   r   s       r:   rv   zSyncBucketActionsMixin.update
  s     %%eT4FFrP   c                $    | j                    d| S )NrV   )r$   )r4   rA   s     r:   rB   z&SyncBucketActionsMixin._get_final_path  s    ''!D6""rP   )NNN)r5   r   r6   r#   r'   zOptional[dict[str, Any]]r(   zOptional[dict[Any, Any]]r)   zOptional[Any]r7   r	   returnr   )rA   r#   r   r   )N)
rA   r#   r?   r#   r-   /Union[BufferedReader, bytes, FileIO, str, Path]re   Optional[FileOptions]r   r   )rA   r#   ry   intrz   r   r   dict[str, str])r~   z	list[str]ry   r   rz   r   r   list[dict[str, str]])rA   r#   rz   r   r   r#   )r   r#   r   r#   r   r   )r~   r   r   zlist[dict[str, Any]])rA   r#   r   r   )rA   r#   r   bool)NN)rA   zOptional[str]rz   z Optional[ListBucketFilesOptions]r   r   )rA   r#   rz   r   r   rb   )
r5   zLiteral['POST', 'PUT']rA   r#   r-   r   re   r   r   r   )rA   r#   r-   r   re   r   r   r   )rA   r#   r   r#   )__name__
__module____qualname____doc____annotations__r;   rO   rl   r|   r   r   r   r   r   r   r   r   rp   r   r   rv   rB    rP   r:   r"   r"       s2   2G -1)-#  *	
 '   
0
: /3@>@> @> >	@>
 ,@> 
@>F AC))%()3=)	)X UW'',/':Q'	'R ?A  TD,,  
$++ 
+( #48 2 
	< >@  6 /3Q>&Q> Q> >	Q>
 ,Q> 
Q>n /3	HH >H ,	H
 
H2 /3	GG >G ,	G
 
G#rP   r"   Freprc                      e Zd ZdZy)r    zRepresents a storage bucket.N)r   r   r   r   r   rP   r:   r    r      s    &rP   c                  8    e Zd ZU dZded<    ed      Zded<   y)	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   rP   r:   r   r     s    ZGU+GZ+rP   r   ).
__future__r   r   r(   urllib.parserC   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   rP   r:   <module>r      s    "    ( %  6 6 + D (   1.s# s#l ' ' ' ,, , ,rP   