
    g                     <    d dl mZ  G d de      Z G d de      Zy)    )FileResponsec                   :    e Zd ZdZdZd ed      dfdZd Zd Zy)	RangedFileReaderz
    Wraps a file like object with an iterator that runs over part (or all) of
    the file defined by start and stop. Blocks of block_size will be returned
    from the starting position, up to, but not including the stop point.
    i    r   infNc                     || _         t        | j                   j                               | _        |xs t        j
                  | _        || _        || _        y)a5  
        Args:
            file_like (File): A file-like object.
            start (int): Where to start reading the file.
            stop (Optional[int]:float): Where to end reading the file.
                Defaults to infinity.
            block_size (Optional[int]): The block_size to read with.
        N)flenreadsizer   
block_sizestartstop)self	file_liker   r   r   s        [/var/www/html/brdwt/brdwt/brdwtenv/lib/python3.12/site-packages/ranged_response/__init__.py__init__zRangedFileReader.__init__   sB     &	$C(8(C(C
	    c              #   R  K   | j                   j                  | j                         | j                  }|| j                  k  rc| j                   j	                  t        | j                  | j                  |z
              }|sy| || j                  z  }|| j                  k  rbyyw)z+
        Reads the data in chunks.
        N)r   seekr   r   r
   minr   )r   positiondatas      r   __iter__zRangedFileReader.__iter__   s~      	DJJ::"66;;s4??DII4HIJDJ'H "s   B"B'%B'c                    |rd|vryg }|j                  dd      \  }}|j                         j                         }|dk7  ry|j                  d      D ]  }|j                         }d|vr y|j                  d      r|t	        |      z   }|dk  rd}|}n9|j                  dd      \  }}t	        |      }|rt	        |      dz   n|}||k\  r y|j                  ||f        |S )a  
        Parses a range header into a list of two-tuples (start, stop) where
        `start` is the starting byte of the range (inclusive) and
        `stop` is the ending byte position of the range (exclusive).

        Args:
            header (str): The HTTP_RANGE request header.
            resource_size (int): The size of the file in bytes.

        Returns:
            None if the value of the header is not syntatically valid.
        =N   bytes,-r   )splitstriplower
startswithintappend)	r   headerresource_sizerangesunitsrange_valr   r   s	            r   parse_range_headerz#RangedFileReader.parse_range_header)   s     F*S!,v##%G<<$ 	)C))+C#~~~c" &C019E$ "iiQ/tE
 )-s4y1}-D=MM5$-(/	)2 r   )	__name__
__module____qualname____doc__r   floatr   r   r,    r   r   r   r      s(    
 J()e (0r   r   c                   (     e Zd ZdZ fdZd Z xZS )RangedFileResponsez
    This is a modified FileResponse that returns `Content-Range` headers with
    the response, so browsers that request the file, can stream the response
    properly.
    c                     t        |      | _        t        t        |   | j                  g|i | d|j
                  v r| j                  |j
                  d          yy)a  
        RangedFileResponse constructor also requires a request, which
        checks whether range headers should be added to the response.

        Args:
            request(WGSIRequest): The Django request object.
            file (File): A file-like object.
        
HTTP_RANGEN)r   ranged_filesuperr4   r   METAadd_range_headers)r   requestfileargskwargs	__class__s        r   r   zRangedFileResponse.__init__c   se     ,D1 $0	
#	
'-	
 7<<'""7<<#=> (r   c                 p   d| d<   | j                   j                  }	 | j                   j                  ||      }|kt	        |      dk(  r\|d   \  }}||k\  rd| _        y||k\  r|}|| j                   _        || j                   _        d||dz
  |fz  | d<   ||z
  | d	<   d
| _        yyy# t        $ r d}Y {w xY w)a  
        Adds several headers that are necessary for a streaming file
        response, in order for Safari to play audio files. Also
        sets the HTTP status_code to 206 (partial content).

        Args:
            range_header (str): Browser HTTP_RANGE request header.
        r   zAccept-RangesNr   r   i  zbytes %d-%d/%dzContent-RangezContent-Length   )r7   r   r,   
ValueErrorr	   status_coder   r   )r   range_headerr   r(   r   r   s         r   r:   z$RangedFileResponse.add_range_headerst   s     !(_$$	%%88tLF
 #f+"2 )KE4}#& t|%*D"$(D!$4tax7N$ND!%)E\D!""D #3	  	F	s   B' 'B54B5)r-   r.   r/   r0   r   r:   __classcell__)r?   s   @r   r4   r4   \   s    ?"#r   r4   N)django.http.responser   objectr   r4   r2   r   r   <module>rH      s#    -Uv Up5# 5#r   