o
    m=8j@                     @   s  U d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZ dd	lmZ dd
l m!Z! ddl"m#Z#m$Z$m%Z%m&Z& ddl'm(Z(m)Z) ddl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2 dZ3erddl4m5Z5 eee6ged f  Z7e8ej9:dZ;ee8 e<d< e Z=ee e<d< edd dD Z>dZ?eddddddZ@G dd deZAe=jBC  e@D D ]
\ZEZFe=GeEeF qeH ZIeHe jJd  e<d < G d!d" d"e2ZKdS )#    N)suppress)Enumauto)	MimeTypes)S_ISREG)MappingProxyType)IOTYPE_CHECKINGAny	AwaitableCallableFinalIteratorListOptionalSetTupleUnioncast   )hdrs)AbstractStreamWriter)DEFAULT_CHUNK_SIZEETAG_ANYETagmust_be_empty_body)LooseHeadersPathLike)HTTPForbiddenHTTPNotFoundHTTPNotModifiedHTTPPartialContentHTTPPreconditionFailedHTTPRequestRangeNotSatisfiable)StreamResponse)FileResponse)BaseRequestAIOHTTP_NOSENDFILE
NOSENDFILECONTENT_TYPESc                 C   s   i | ]}|t j| qS  )r)   encodings_map).0extr*   r*   _/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/aiohttp/web_fileresponse.py
<dictcomp>8   s    r/   ).br.gzzapplication/octet-streamr1   r0   z.bz2z.Zz.xz)zapplication/gzipzapplication/x-brotlizapplication/x-bzip2zapplication/x-compresszapplication/x-xzc                   @   s(   e Zd ZdZe Ze Ze Ze ZdS )_FileResponseResultz The result of the file response.N)	__name__
__module____qualname____doc__r   	SEND_FILENOT_ACCEPTABLEPRE_CONDITION_FAILEDNOT_MODIFIEDr*   r*   r*   r.   r2   J   s    
r2   _CLOSE_FUTURESc                       s  e Zd ZdZedddfdededededB dedB d	df fd
dZ	de
e deded	efddZdede
e deded	ef
ddZddde
e deded	ef
 fddZededeedf ded	efddZdddeded	edB f fdd Zddd	edB f fd!d"Zddd#ed	eeejdB ejedB f fd$d%Zd#ed	eej dB ejedB f fd&d'Z!ddd	edB f fd(d)Z"dddejd*ejd+edB d	edB f
 fd,d-Z#  Z$S ).r%   z,A response object can be used to send files.   Npath
chunk_sizestatusreasonheadersreturnc                    s(   t  j|||d t|| _|| _d S )N)r?   r@   rA   )super__init__pathlibPath_path_chunk_size)selfr=   r>   r?   r@   rA   	__class__r*   r.   rD   _   s   
zFileResponse.__init__fobjoffsetc                 C   s   | | ||S N)seekread)rI   rL   rM   r>   r*   r*   r.   _seek_and_readl   s   

zFileResponse._seek_and_readwritercountc              	      s   | j }t }|d | j||t||I d H }|r=||I d H  |t| }|dkr-n|d |jt||I d H }|s|	 I d H  |S Nr   )
rH   asyncioget_event_looprun_in_executorrQ   minwritelenrP   drain)rI   rR   rL   rM   rS   r>   loopchunkr*   r*   r.   _sendfile_fallbackp   s   
zFileResponse._sendfile_fallbackrequestr&   c              	      s   t  |I d H }|d usJ ts| jr | ||||I d H S |j}|j}|d u r.tdz|||||I d H  W n t	yO   | ||||I d H  Y S w t  
 I d H  |S )NzConnection lost)rC   preparer(   compressionr^   _loop	transportConnectionResetErrorsendfileNotImplementedError	write_eof)rI   r_   rL   rM   rS   rR   r\   rc   rJ   r*   r.   	_sendfile   s    
zFileResponse._sendfile
etag_valueetags.weakc                   s6   t |dkr|d jtkrdS t fdd|D S )Nr   r   Tc                 3   s$    | ]}s	|j s|j kV  qd S rN   )is_weakvalue)r,   etagri   rk   r*   r.   	<genexpr>   s    
z+FileResponse._etag_match.<locals>.<genexpr>)rZ   rm   r   any)ri   rj   rk   r*   ro   r.   _etag_match   s
   zFileResponse._etag_matchlast_modifiedc                    s2   |  tj d| _|| _|| _t |I d H S )NF)
set_statusr    status_code_length_checkrn   rs   rC   r`   )rI   r_   ri   rs   rJ   r*   r.   _not_modified   s   zFileResponse._not_modifiedc                    s&   |  tj d| _t |I d H S rT   )rt   r"   ru   content_lengthrC   r`   )rI   r_   rJ   r*   r.   _precondition_failed   s   z!FileResponse._precondition_failedaccept_encodingc                 C   sB  |  |\}}}|stjd|dfS |jdd|jd}|j }dur2| j||dds2tjd||fS |j }durK|du rK|j	|
 krKtjd||fS |j }	dura| j||	ddratjd||fS |j }
durz|	du rz|j	|

 krztjd||fS |d}tt t| }W d   n1 sw   Y  tj|||fS )a  Return the response result, io object, stat result, and encoding.

        If an uncompressed file is returned, the encoding is set to
        :py:data:`None`.

        This method should be called from a thread executor
        since it calls os.stat which may block.
        Nx-F)rk   Trb)_get_file_path_stat_encodingr2   r8   st_mtime_nsst_sizeif_matchrr   r9   if_unmodified_sincest_mtime	timestampif_none_matchr:   if_modified_sinceopenr   OSErrorosstatfilenor7   )rI   r_   rz   	file_pathstfile_encodingri   ifmatch
unmodsinceifnonematchmodsincerL   r*   r*   r.   _make_response   s6   


zFileResponse._make_responsec              	   C   s   | j }t D ]<\}}||vrq||j| }tt | }t|j	r4|||fW  d      S W d    n1 s>w   Y  q|
 }t|j	rR||d fS d |d fS rN   )rG   ENCODING_EXTENSIONSitemswith_suffixsuffixr   r   lstatr   st_moder   )rI   rz   r   file_extensionr   compressed_pathr   r*   r*   r.   r~      s   

z)FileResponse._get_file_path_stat_encodingc              
      s  t  }|jtjd }z|d | j||I d H \}}}}W n/ t	y9   | 
tj t |I d H  Y S  tyP   | 
tj t |I d H  Y S w |tju re| 
tj t |I d H S |tju rr| |I d H S |tju r|jdd|jd}|j}	| |||	I d H S |d usJ z| ||||I d H W |d |j}
t|
 |
tj S |d |j}
t|
 |
tj w )N r{   r|   ) rU   get_running_looprA   getr   ACCEPT_ENCODINGlowerrW   r   PermissionErrorrt   r   ru   rC   r`   r   r   r2   r8   r9   ry   r:   r   r   r   rw   _prepare_open_filecloser;   addadd_done_callbackremove)rI   r_   r\   rz   response_resultrL   r   r   ri   rs   close_futurerJ   r*   r.   r`      s@   





zFileResponse.preparer   r   c                    s>  | j }|j}|j}|}d }	|j }
d u s||
 krz|j}|j}	|j}W n! tyG   d| | j	t
j< | tj t |I d H  Y S w |	d ur|	dk rc|d u rc|	|7 }	|	dk r^d}	||	 }nt|d urj|n|||	 }|	|krd| | j	t
j< | tj t |I d H S tj}| | t
j| j	vrtjdkrtj}ntj}|| jd pt| _|r|| j	t
j< t
j| j	t
j< d| _|j dd|jd| _!|| _"|| _#d| j	t
j$< |tjkr|	}|d usJ d| d|| d	  d
| | j	t
j< |dkst%|j&|rt |I d H S |	pd}| '||||I d H S )Nzbytes */r   )      Fr{   r|   byteszbytes r   /)(_statusr   r   if_ranger   
http_rangestartstop
ValueError_headersr   CONTENT_RANGErt   r#   ru   rC   r`   rX   r!   CONTENT_TYPEsysversion_infor)   guess_file_type
guess_typerG   FALLBACK_CONTENT_TYPEcontent_typeCONTENT_ENCODINGr   VARY_compressionr   rn   rs   rx   ACCEPT_RANGESr   methodrh   )rI   r_   rL   r   r   r?   	file_size
file_mtimerS   r   ifrangerngendguesser
real_startrM   rJ   r*   r.   r   -  sh   









zFileResponse._prepare_open_file)%r3   r4   r5   r6   r   r   intstrr   rD   r   r
   r   rQ   r   r^   rh   staticmethodtupler   boolrr   floatrw   ry   r2   ioBufferedReaderr   stat_resultr   rE   rF   r~   r`   r   __classcell__r*   r*   rJ   r.   r%   \   s    
$
8
-r%   )LrU   r   r   rE   r   
contextlibr   enumr   r   	mimetypesr   r   r   typesr   typingr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   abcr   helpersr   r   r   r   typedefsr   r   web_exceptionsr   r   r    r!   r"   r#   web_responser$   __all__web_requestr&   r   _T_OnChunkSentr   environr   r(   __annotations__r)   r   r   ADDITIONAL_CONTENT_TYPESr2   r+   clearr   r   	extensionadd_typesetr;   Futurer%   r*   r*   r*   r.   <module>   sR   
 < 

