U
    mf                     @   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
 ddd	d
ddddg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                   @   s   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   C/tmp/pip-unpacked-wheel-siwebuq3/django/core/files/uploadhandler.pyr      s   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 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   
c                   @   s   e Zd ZdZdS )r
   zX
    This exception is raised by an upload handler that wants to skip a given file.
    Nr   r   r   r   r   r
   3   s   c                   @   s   e Zd ZdZdS )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   c                   @   sR   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   s   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   Z
input_dataZ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   s   t ddS )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() methodNNotImplementedErrorr   raw_datastartr   r   r   receive_data_chunk}   s    z$FileUploadHandler.receive_data_chunkc                 C   s   t ddS )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() methodNr*   r   	file_sizer   r   r   file_complete   s    zFileUploadHandler.file_completec                 C   s   dS )z
        Signal that the upload is complete. Subclasses should perform cleanup
        that is necessary for this handler.
        Nr   r   r   r   r   upload_complete   s    z!FileUploadHandler.upload_completec                 C   s   dS )z
        Signal that the upload was interrupted. Subclasses should perform
        cleanup that is necessary for this handler.
        Nr   r   r   r   r   upload_interrupted   s    z$FileUploadHandler.upload_interrupted)N)N)NN)r   r   r   r   
chunk_sizer   r'   r)   r/   r2   r3   r4   r   r   r   r   r   D   s   
	 
  
	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                    s,   t  j|| 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   )r7   writer,   r   r   r   r/      s    z-TemporaryFileUploadHandler.receive_data_chunkc                 C   s   | j d || j _| j S )Nr   )r7   seeksizer0   r   r   r   r2      s    z(TemporaryFileUploadHandler.file_completec                 C   sF   t | drB| j }z| j  t| W n tk
r@   Y nX d S )Nr7   )hasattrr7   Ztemporary_file_pathcloseosremoveFileNotFoundError)r   Ztemp_locationr   r   r   r4      s    


z-TemporaryFileUploadHandler.upload_interrupted)	r   r   r   r   r)   r/   r2   r4   __classcell__r   r   r;   r   r      s
   	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   ZFILE_UPLOAD_MAX_MEMORY_SIZE	activatedr$   r   r   r   r'      s    	z(MemoryFileUploadHandler.handle_raw_inputc                    s&   t  j|| | jr"t | _t d S r   )r6   r)   rF   r   r7   r   r8   r;   r   r   r)      s    z MemoryFileUploadHandler.new_filec                 C   s   | j r| j| n|S dS )z!Add the data to the BytesIO file.N)rF   r7   r=   r,   r   r   r   r/      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   )r7   r(   namer   r?   r!   r"   )	rF   r7   r>   r   r(   r   r   r!   r"   r0   r   r   r   r2      s    z%MemoryFileUploadHandler.file_complete)N)	r   r   r   r   r'   r)   r/   r2   rE   r   r   r;   r   r      s    
c                 O   s   t | ||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   )pathr9   r:   r   r   r   r      s    )r   rB   ior   Zdjango.confr   Zdjango.core.files.uploadedfiler   r   Zdjango.utils.module_loadingr   __all__	Exceptionr   r	   r
   r   r   r   r   r   r   r   r   r   <module>   s,   	\ .