a
    Rf5                     @   s  U 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 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 ddlmZ dd	lmZ dd
l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)m*Z* ddl+m,Z, dZ-erddl.m/Z/ eee0ged f  Z1e2ej34dZ5ee2 e6d< e Z7ee e6d< ej8dk rbde7j9d< edd dD Z:dZ;eddddddZ<e7j9=  e<> D ]\Z?Z@e7Ae?e@ qG dd  d e,ZBdS )!    N)suppress)	MimeTypes)S_ISREG)MappingProxyType)IOTYPE_CHECKINGAny	AwaitableCallableFinalIteratorListOptionalTupleUnioncast   )hdrs)AbstractStreamWriter)ETAG_ANYETagmust_be_empty_body)LooseHeadersPathLike)HTTPForbiddenHTTPNotFoundHTTPNotModifiedHTTPPartialContentHTTPPreconditionFailedHTTPRequestRangeNotSatisfiable)StreamResponse)FileResponse)BaseRequestZAIOHTTP_NOSENDFILE
NOSENDFILECONTENT_TYPES)   	   br.brc                 C   s   i | ]}|t j| qS  )r$   encodings_map).0extr)   r)   Q/var/www/ai-form-bot/venv/lib/python3.9/site-packages/aiohttp/web_fileresponse.py
<dictcomp>8       r.   )r(   .gzzapplication/octet-streamr0   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deeeee ee dd fddZ	e
ee eee
dd	d
Zdee eee
d fddZeeeedf edddZdeeee
 d fddZdee
 d fddZeeejejee f dddZdee
 d 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)r5   r6   r7   )super__init__pathlibPath_path_chunk_size)selfr3   r4   r5   r6   r7   	__class__r)   r-   r:   R   s    zFileResponse.__init__)writerfobjoffsetcountr8   c                    s   | j }t }|d |j|I d H  |d |j|I d H }|r~||I d H  || }|dkr`q~|d |jt||I d H }q:| I d H  |S Nr   )	r>   asyncioZget_event_looprun_in_executorseekreadwriteminZdrain)r?   rB   rC   rD   rE   r4   loopchunkr)   r)   r-   _sendfile_fallback_   s    zFileResponse._sendfile_fallbackr"   )requestrC   rD   rE   r8   c              	      s   t  |I d H }|d usJ ts(| jr>| ||||I d H S |j}|j}|d usVJ z|||||I d H  W n( ty   | ||||I d H  Y S 0 t  	 I d H  |S N)
r9   preparer#   compressionrO   _loop	transportsendfileNotImplementedError	write_eof)r?   rP   rC   rD   rE   rB   rM   rU   r@   r)   r-   	_sendfileu   s    
zFileResponse._sendfile.)
etag_valueetagsr8   c                    s4   t |dkr|d jtkrdS t fdd|D S )Nr   r   Tc                 3   s   | ]}|j s|j kV  qd S rQ   )Zis_weakvalue)r+   etagrZ   r)   r-   	<genexpr>   r/   z2FileResponse._strong_etag_match.<locals>.<genexpr>)lenr\   r   any)rZ   r[   r)   r^   r-   _strong_etag_match   s    zFileResponse._strong_etag_match)rP   rZ   last_modifiedr8   c                    s0   |  tj d| _|| _|| _t |I d H S )NF)
set_statusr   status_codeZ_length_checkr]   rc   r9   rR   )r?   rP   rZ   rc   r@   r)   r-   _not_modified   s
    zFileResponse._not_modified)rP   r8   c                    s$   |  tj d| _t |I d H S rF   )rd   r   re   content_lengthr9   rR   )r?   rP   r@   r)   r-   _precondition_failed   s    z!FileResponse._precondition_failed)accept_encodingr8   c              	   C   s   | j }t D ]x\}}||vr q||j| }tt> | }t|j	rh|||fW  d     S W d   q1 s|0    Y  q||
 dfS )a  Return the file path, 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)r=   ENCODING_EXTENSIONSitemswith_suffixsuffixr   OSErrorlstatr   st_modestat)r?   ri   	file_pathZfile_extensionfile_encodingZcompressed_pathstr)   r)   r-   _get_file_path_stat_encoding   s    

<z)FileResponse._get_file_path_stat_encodingc                    sB  t  }|jtjd }z |d | j|I d H \}}}W n0 t	yl   | 
tj t |I d H  Y S 0 t|js| 
tj t |I d H S |jdd|jd}|j}|j}	|	d ur| ||	s| |I d H S |j}
|
d ur|	d u r|j|
 kr| |I d H S |j}|d urH| ||rH| |||I d H S |j}|d ur|d u r|j| kr| |||I d H S | j}|j}|}d }|j}|d u s|j| krz|j}|j }|j!}W nD t"y   d| | jtj#< | 
t$j t |I d H  Y S 0 |d us*|d ur|dk r^|d u r^||7 }|dk rTd}|| }nt%|d urn|n||| }||krd| | jtj#< | 
t$j t |I d H S t&j}| 
| tj'| jvrt()| j*d pt+| _,|r|| jtj-< tj| jtj.< d| _/|| _0|j| _1|| _2d| jtj3< t4t5|}|t&jkrdd6||| d	 || jtj#< |dks~t7|j8| j9rt |I d H S z|d |j:d
I d H }W n2 t;y   | 
tj t |I d H  Y S 0 |r|}nd}z2| <||||I d H W t =|d |j>I d H  S t =|d |j>I d H  0 d S )N x-zbytes */r   Fbyteszbytes {}-{}/{}r   rb)?rG   Zget_running_loopr7   getr   ACCEPT_ENCODINGlowerrH   ru   rn   rd   r   re   r9   rR   r   rp   r   st_mtime_nsst_sizest_mtimeZif_matchrb   rh   Zif_unmodified_since	timestampZif_none_matchrf   Zif_modified_sinceZ_statusZif_rangeZ
http_rangestartstop
ValueErrorZCONTENT_RANGEr   rL   r   ZCONTENT_TYPEr$   
guess_typer=   FALLBACK_CONTENT_TYPEcontent_typeZCONTENT_ENCODINGZVARY_compressionr]   rc   rg   ZACCEPT_RANGESr   intformatr   methodr5   openPermissionErrorrY   shieldclose)r?   rP   rM   ri   rr   rt   rs   rZ   rc   ZifmatchZ
unmodsinceZifnonematchZmodsincer5   	file_sizerE   r   ZifrangerngendZ
real_startrC   rD   r@   r)   r-   rR      s    









zFileResponse.prepare)r1   r2   NN)__name__
__module____qualname____doc__r   r   r   strr   r:   r   r   r   rO   rY   staticmethodr   r   boolrb   floatrf   rh   r;   r<   osstat_resultru   rR   __classcell__r)   r)   r@   r-   r!   O   s>       r!   )CrG   r   r;   sys
contextlibr   	mimetypesr   rq   r   typesr   typingr   r   r   r	   r
   r   r   r   r   r   r   r   rv   r   abcr   Zhelpersr   r   r   Ztypedefsr   r   Zweb_exceptionsr   r   r   r   r   r   Zweb_responser    __all__Zweb_requestr"   ry   Z_T_OnChunkSentr   environr{   r#   __annotations__r$   version_infor*   rj   r   ZADDITIONAL_CONTENT_TYPESclearrk   r   	extensionadd_typer!   r)   r)   r)   r-   <module>   sL   
8 

