o
    ˜¨Êh  ã                   @   sÂ   d 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
 g d¢ZG dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZG dd„ deƒZG dd„ deƒZdd„ ZdS )zH
Base file upload handler classes, and the built-in concrete subclasses
é    N)ÚBytesIO)Úsettings)ÚInMemoryUploadedFileÚTemporaryUploadedFile©Úimport_string)ÚUploadFileExceptionÚ
StopUploadÚSkipFileÚFileUploadHandlerÚTemporaryFileUploadHandlerÚMemoryFileUploadHandlerÚload_handlerÚStopFutureHandlersc                   @   ó   e Zd ZdZdS )r   z6
    Any error having to do with uploading files.
    N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úQ/var/www/html/env/lib/python3.10/site-packages/django/core/files/uploadhandler.pyr      ó    r   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )	r	   z=
    This exception is raised when an upload must abort.
    Fc                 C   s
   || _ dS )zÒ
        If ``connection_reset`` is ``True``, Django knows will halt the upload
        without consuming the rest of the upload. This will cause the browser to
        show a "connection reset" error.
        N©Úconnection_reset)Úselfr   r   r   r   Ú__init__$   s   
zStopUpload.__init__c                 C   s   | j rdS dS )Nz StopUpload: Halt current upload.z,StopUpload: Consume request data, then halt.r   ©r   r   r   r   Ú__str__,   s   zStopUpload.__str__N)F)r   r   r   r   r   r   r   r   r   r   r	      s    
r	   c                   @   r   )r
   zX
    This exception is raised by an upload handler that wants to skip a given file.
    Nr   r   r   r   r   r
   3   r   r
   c                   @   r   )r   z”
    Upload handlers that have handled a file and do not want future handlers to
    run should raise this exception instead of returning None.
    Nr   r   r   r   r   r   ;   s    r   c                   @   sX   e Zd ZdZdZddd„Z	ddd„Z		ddd	„Zd
d„ Zdd„ Z	dd„ Z
dd„ ZdS )r   z3
    Base class for streaming upload handlers.
    i   Nc                 C   s(   d | _ d | _d | _d | _d | _|| _d S ©N)Ú	file_nameÚcontent_typeÚcontent_lengthÚcharsetÚcontent_type_extraÚrequest)r   r%   r   r   r   r   K   s   
zFileUploadHandler.__init__c                 C   ó   dS )aÆ  
        Handle the raw input from the client.

        Parameters:

            :input_data:
                An object that supports reading via .read().
            :META:
                ``request.META``.
            :content_length:
                The (integer) value of the Content-Length header from the
                client.
            :boundary: The boundary from the Content-Type header. Be sure to
                prepend two '--'.
        Nr   ©r   Ú
input_dataÚMETAr"   ÚboundaryÚencodingr   r   r   Úhandle_raw_inputS   s   z"FileUploadHandler.handle_raw_inputc                 C   s(   || _ || _|| _|| _|| _|| _dS )z½
        Signal that a new file has been started.

        Warning: As with any data from the client, you should not trust
        content_length (and sometimes won't even get it).
        N)Ú
field_namer    r!   r"   r#   r$   )r   r-   r    r!   r"   r#   r$   r   r   r   Únew_fileg   s   
zFileUploadHandler.new_filec                 C   ó   t dƒ‚)z{
        Receive data from the streamed upload parser. ``start`` is the position
        in the file of the chunk.
        zJsubclasses of FileUploadHandler must provide a receive_data_chunk() method©ÚNotImplementedError©r   Úraw_dataÚstartr   r   r   Úreceive_data_chunk}   s   ÿz$FileUploadHandler.receive_data_chunkc                 C   r/   )zÆ
        Signal that a file has completed. File size corresponds to the actual
        size accumulated by all the chunks.

        Subclasses should return a valid ``UploadedFile`` object.
        zEsubclasses of FileUploadHandler must provide a file_complete() methodr0   ©r   Ú	file_sizer   r   r   Úfile_complete†   s   ÿzFileUploadHandler.file_completec                 C   r&   )zƒ
        Signal that the upload is complete. Subclasses should perform cleanup
        that is necessary for this handler.
        Nr   r   r   r   r   Úupload_complete‘   ó   z!FileUploadHandler.upload_completec                 C   r&   )z‡
        Signal that the upload was interrupted. Subclasses should perform
        cleanup that is necessary for this handler.
        Nr   r   r   r   r   Úupload_interrupted˜   r:   z$FileUploadHandler.upload_interruptedr   )NN)r   r   r   r   Ú
chunk_sizer   r,   r.   r5   r8   r9   r;   r   r   r   r   r   D   s    
	
ÿ
ù	r   c                       s8   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  ZS )
r   zA
    Upload handler that streams data into a temporary file.
    c                    s0   t ƒ j|i |¤Ž t| j| jd| j| jƒ| _dS )zK
        Create the file object to append to as data is coming in.
        r   N)Úsuperr.   r   r    r!   r#   r$   Úfile©r   ÚargsÚkwargs©Ú	__class__r   r   r.   ¥   s   
ÿz#TemporaryFileUploadHandler.new_filec                 C   s   | j  |¡ d S r   )r>   Úwriter2   r   r   r   r5   ®   s   z-TemporaryFileUploadHandler.receive_data_chunkc                 C   s   | j  d¡ || j _| j S )Nr   )r>   ÚseekÚsizer6   r   r   r   r8   ±   s   z(TemporaryFileUploadHandler.file_completec                 C   sH   t | dƒr"| j ¡ }z| j ¡  t |¡ W d S  ty!   Y d S w d S )Nr>   )Úhasattrr>   Útemporary_file_pathÚcloseÚosÚremoveÚFileNotFoundError)r   Útemp_locationr   r   r   r;   ¶   s   


ÿûz-TemporaryFileUploadHandler.upload_interrupted)	r   r   r   r   r.   r5   r8   r;   Ú__classcell__r   r   rB   r   r       s    	r   c                       s<   e Zd ZdZ	ddd„Z‡ fdd„Zdd„ Zd	d
„ Z‡  ZS )r   zS
    File upload handler to stream uploads into memory (used for small files).
    Nc                 C   s   |t jk| _dS )zf
        Use the content_length to signal whether or not this handler should be
        used.
        N)r   ÚFILE_UPLOAD_MAX_MEMORY_SIZEÚ	activatedr'   r   r   r   r,   Å   s   	z(MemoryFileUploadHandler.handle_raw_inputc                    s*   t ƒ j|i |¤Ž | jrtƒ | _tƒ ‚d S r   )r=   r.   rP   r   r>   r   r?   rB   r   r   r.   Ð   s
   þz MemoryFileUploadHandler.new_filec                 C   s   | j r| j |¡ dS |S )z!Add the data to the BytesIO file.N)rP   r>   rD   r2   r   r   r   r5   Ö   s   z*MemoryFileUploadHandler.receive_data_chunkc              	   C   s8   | j sdS | j d¡ t| j| j| j| j|| j| jd�S )z2Return a file object if this handler is activated.Nr   )r>   r-   Únamer!   rF   r#   r$   )	rP   r>   rE   r   r-   r    r!   r#   r$   r6   r   r   r   r8   Ý   s   ùz%MemoryFileUploadHandler.file_completer   )	r   r   r   r   r,   r.   r5   r8   rN   r   r   rB   r   r   À   s    
ÿr   c                 O   s   t | ƒ|i |¤ŽS )al  
    Given a path to a handler, return an instance of that handler.

    E.g.::
        >>> from django.http import HttpRequest
        >>> request = HttpRequest()
        >>> load_handler(
        ...     'django.core.files.uploadhandler.TemporaryFileUploadHandler',
        ...     request,
        ... )
        <TemporaryFileUploadHandler object at 0x...>
    r   )Úpathr@   rA   r   r   r   r   î   s   r   )r   rJ   Úior   Údjango.confr   Údjango.core.files.uploadedfiler   r   Údjango.utils.module_loadingr   Ú__all__Ú	Exceptionr   r	   r
   r   r   r   r   r   r   r   r   r   Ú<module>   s    	\ .