
    Ji                     |    d dl mZ d dlmZ d dlZd dl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  G d	 d
      Zy)    )BytesIO)BinaryION)Path)AsyncLlamaCloud)File
FileCreate)Optional)
SourceText	FileInputc            	           e Zd ZdZ	 	 	 ddedee   dee   defdZded	e	fd
Z
ded	efdZ	 ddedee   d	e	fdZdeded	e	fdZdededed	e	fdZ	 ddedee   d	e	fdZy)
FileClienta  
    Higher-level client for interacting with the LlamaCloud Files API.
    Optionally uses presigned URLs for uploads.

    Args:
        client: The LlamaCloud client to use.
        project_id: The project ID to use.
        organization_id: The organization ID to use.
        use_presigned_url: Whether to use presigned URLs for uploads (set to False when uploading to BYOC deployments).
    Nclient
project_idorganization_iduse_presigned_urlc                 <    || _         || _        || _        || _        y N)r   r   r   r   )selfr   r   r   r   s        s/var/www/html/BankruptcyAI-uat/bankruptcy-ai/venv/lib/python3.12/site-packages/llama_cloud_services/files/client.py__init__zFileClient.__init__   s"     $.!2    file_idreturnc                    K   | j                   j                  j                  || j                  | j                         d {   S 7 wNr   r   )r   filesget_filer   r   )r   r   s     r   r   zFileClient.get_file#   sC     [[&&//AUAU 0 
 
 	
 
s   A A	AA	c                 X  K   | j                   j                  j                  || j                  | j                         d {   }| j                   j
                  j                  }|j                  |j                         d {   }|j                          |j                  S 7 c7 "wr   )r   r   read_file_contentr   r   _client_wrapperhttpx_clientgeturlraise_for_statuscontent)r   r   presigned_urlr"   responses        r   r    zFileClient.read_file_content(   s     "kk//AA 00 B 
 

 {{22??%))-*;*;<<!!#
 =s%   A B*B&AB*B(!B*(B*	file_pathexternal_file_idc                    K   |xs |}t         j                  j                  |      }t        |d      5 }| j	                  |||       d {   cd d d        S 7 # 1 sw Y   y xY ww)Nrb)ospathgetsizeopenupload_buffer)r   r)   r*   	file_sizefiles        r   upload_filezFileClient.upload_file3   si      ,8yGGOOI.	)T" 	Od++D2BINN	O 	ON	O 	Os.   2A'AAA
A'AA$ A'bytesc                 h   K   | j                  t        |      |t        |             d {   S 7 wr   )r1   r   len)r   r5   r*   s      r   upload_byteszFileClient.upload_bytes;   s*     ''8H#e*UUUUs   )202bufferr2   c           	      |  K   | j                   rDt        |dd       r4t        j                  j	                  t        t        |d|                  }n|}| j                  j                  j                  | j                  | j                  t        |||             d {   }| j                  j                  j                  }|j                  |j                  |j!                                d {   }|j#                          | j                  j                  j%                  |j&                  | j                  | j                         d {   S t        |dd       st)        |d|       | j                  j                  j+                  ||| j                  | j                         d {   S 7 7 7 h7 w)Nname)r;   r*   r2   )r   r   request)datar   )r4   r*   r   r   )r   getattrr-   r.   basenamestrr   r   generate_presigned_urlr   r   r   r!   r"   putr$   readr%   r   r   setattrr4   )r   r9   r*   r2   r;   r'   r"   upload_responses           r   r1   zFileClient.upload_buffer>   s     !!vvt,ww''GFFDT,U(VW'"&++"3"3"J"J?? $ 4 4"%5' #K # M  ;;66CCL$0$4$4!![[] %5 % O ,,.**33%%?? $ 4 4 4    6640(89**66"!1?? $ 4 4	 7   3
sK   BF<F3AF<1F62AF<F8AF<.F:/F<6F<8F<:F<
file_inputc                   K   t        |t              r|S t        |t              r|j                  #| j	                  |j                         d{   S |j
                  G|j
                  j                  d      }| j                  ||xs |j                  xs d       d{   S t        |j                  t        t        f      r;| j                  t        |j                        |xs |j                         d{   S t        |j                  t              r6| j                  |j                  |xs |j                  xs d       d{   S t        |j                  d      rg|j                  j                         }t        |t              r|j                  d      }| j                  ||xs |j                  xs d       d{   S t!        dt#        |j                               t        |t        t        f      r#| j                  t        |      |       d{   S t        |d      rt        |d      r3t$        j&                  j)                  t        |j*                              }n|xs d}|j                         }t        |t              r|j                  d      }| j                  ||xs |       d{   S t!        dt#        |       d      7 7 B7 7 7 #7 7 -w)	a  
        Upload content from various input types or fetch an already-uploaded file.

        Args:
            file_input: The content to upload. Can be:
                - File: Already uploaded file (returned as-is)
                - str/Path: Path to a file on disk
                - SourceText: Text content, file, or file_id with explicit filename
                - BufferedIOBase: File-like binary object
            external_file_id: Optional external identifier for the file

        Returns:
            File: The uploaded (or fetched) file object

        Raises:
            ValueError: If the input type is not supported or required info is missing
        Nzutf-8r3   rC   zUnsupported file type: r;   zUnsupported file input type: zB. Supported types: str, Path, SourceText, BufferedIOBase, or File.)
isinstancer   r
   r   r   text_contentencoder8   filenamer3   r@   r   r4   r5   hasattrrC   
ValueErrortyper-   r.   r?   r;   )r   rF   r*   
text_bytesr&   rK   s         r   upload_contentzFileClient.upload_contenti   s    * j$' j*-!!-!]]:+=+=>>>((4'44;;GD
!.. 0 QJ4G4G Q6   JOOc4[9!--
(*:*Qj>Q>Q   JOOU3!..OO%5%V9L9L%VPV   &1$//..0gs+%nnW5G!..-N1D1DN   !#:4
;P:Q!RSS 
S$K0))#j/;KLLL Z(z6*77++C
,@A+5v !oo'G'3'!..1**74D4PQQQ /Z0@/A BS T _ ?

 M Rs   AK5K"AK5#K%$AK5>K(?AK5K+A<K5K.AK5%K1&BK5K3K5%K5(K5+K5.K51K53K5)NNFr   )__name__
__module____qualname____doc__r   r	   r@   boolr   r   r   r5   r    r4   r8   r   intr1   r   rP    r   r   r   r      s   	 %))-"'
3
3 SM
3 "#	
3
  
3
c 
d 

	 s 	 u 	  AEOO08O	OV V V V)) ) 	)
 
)X HLN#N7?}N	Nr   r   )ior   typingr   r-   pathlibr   llama_cloud.clientr   llama_cloud.typesr   r   r	   llama_cloud_services.utilsr
   r   r   rW   r   r   <module>r^      s)      	  . .  <l lr   