
    g                        d dl mZ d dlmZmZmZ d dl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 erd dlmZ  G d	 d
ej(                  j*                        Zy)    )annotations)TYPE_CHECKINGAnyCallableN)config)UploadedFileRec)routesserver_util)is_xsrf_enabled)MemoryUploadedFileManagerc                  8    e Zd ZdZ	 	 	 	 ddZd Zd Zd Zd Zy)	UploadFileRequestHandlerz*Implements the POST /upload_file endpoint.c                     || _         || _        y)a2  
        Parameters
        ----------
        file_mgr : UploadedFileManager
            The server's singleton UploadedFileManager. All file uploads
            go here.
        is_active_session:
            A function that returns true if a session_id belongs to an active
            session.
        N)	_file_mgr_is_active_session)selffile_mgris_active_sessions      e/var/www/openai/venv/lib/python3.12/site-packages/streamlit/web/server/upload_file_request_handler.py
initializez#UploadFileRequestHandler.initialize"   s     ""3    c                   | j                  dd       | j                  dd       t               ro| j                  dt        j                  t	        j
                  d                   | j                  dd       | j                  dd	       | j                  d
d       y t        j                         r| j                  dd       y y )NzAccess-Control-Allow-MethodszPUT, OPTIONS, DELETEzAccess-Control-Allow-HeadersContent-TypezAccess-Control-Allow-Originzbrowser.serverAddresszX-Xsrftoken, Content-TypeVaryOriginz Access-Control-Allow-Credentialstrue*)
set_headerr   r
   get_urlr   
get_optionr	   allow_cross_origin_requests)r   s    r   set_default_headersz,UploadFileRequestHandler.set_default_headers4   s    68NO6GOO-##F$5$56M$NO OO:<WXOOFH-OO>G//1OO93? 2r   c                F    | j                  d       | j                          y)a  /OPTIONS handler for preflight CORS checks.

        When a browser is making a CORS request, it may sometimes first
        send an OPTIONS request, to check whether the server understands the
        CORS protocol. This is optional, and doesn't happen for every request
        or in every browser. If an OPTIONS request does get sent, and is not
        then handled by the server, the browser will fail the underlying
        request.

        The proper way to handle this is to send a 204 response ("no content")
        with the CORS headers attached. (These headers are automatically added
        to every outgoing response, including OPTIONS responses,
        via set_default_headers().)

        See https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
           N)
set_statusfinish)r   kwargss     r   optionsz UploadFileRequestHandler.optionsB   s    " 	r   c                   i }i }| j                   d   }| j                   d   }t        j                  j                  | j                  j
                  d   | j                  j                  ||       	 | j                  |      st        d      	 g }|j                         D ]3  \  }}	|	D ])  }
|j                  t        ||
d	   |
d
   |
d                + 5 t        |      dk7  r | j                  ddt        |              y| j                  j!                  ||d          | j#                  d       y# t        $ r&}| j                  dt        |             Y d}~yd}~ww xY w)z?Receive an uploaded file and add it to our UploadedFileManager.
session_idfile_idr   )content_typebody	argumentsfileszInvalid session_idi  )reasonNfilenamer,   r-   )r+   nametypedata   zExpected 1 file, but got r   )r*   filer$   )path_kwargstornadohttputilparse_body_argumentsrequestheadersr-   r   	Exception
send_errorstritemsappendr   lenr   add_filer%   )r   r'   argsr/   r*   r+   euploaded_files_flistr6   s              r   putzUploadFileRequestHandler.putV   sn    (*&(%%l3
""9-----n=""	 	. 	
	**:6 455 7 13HAu%%# '!*-!.1!&\	  & ~!#OO7N8K7LM   :N1<MN1  	OOCAO/	s   2D7 7	E& E!!E&c                    | j                   d   }| j                   d   }| j                  j                  ||       | j                  d       y)zDelete file request handler.r*   r+   )r*   r+   r$   N)r7   r   remove_filer%   )r   r'   r*   r+   s       r   deletezUploadFileRequestHandler.delete   sE    %%l3
""9-""j'"Jr   N)r   r   r   zCallable[[str], bool])	__name__
__module____qualname____doc__r   r"   r(   rI   rL    r   r   r   r      s2    44+4 14$@(+Zr   r   )
__future__r   typingr   r   r   tornado.httputilr8   tornado.web	streamlitr   'streamlit.runtime.uploaded_file_managerr   streamlit.web.serverr	   r
    streamlit.web.server.server_utilr   .streamlit.runtime.memory_uploaded_file_managerr   webRequestHandlerr   rQ   r   r   <module>r]      s@    # / /    C 4 <Xjw{{99 jr   