o
    Rh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rddl.m/Z/ eee0ged f  Z1e2ej34dZ5ee2 e6d< e Z7ee e6d< ej8dk rd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)BaseRequestAIOHTTP_NOSENDFILE
NOSENDFILECONTENT_TYPES)   	   br.brc                 C   s   i | ]}|t j| qS  )r%   encodings_map).0extr*   r*   Z/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/aiohttp/web_fileresponse.py
<dictcomp>8   s    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                       s0  e Zd ZdZ				d&dedededee d	ee d
df 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fddZdddeded
ee
 f fddZddd
ee
 f fdd Zd!ed
eejejee f fd"d#Zddd
ee
 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)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countc                    s   | j }t }|d |j|I d H  |d |j|I d H }|rA||I d H  || }|dkr1n|d |jt||I d H }|s | I d H  |S Nr   )	r>   asyncioget_event_looprun_in_executorseekreadwritemindrain)r?   rB   rC   rD   rE   r4   loopchunkr*   r*   r.   _sendfile_fallback_   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s,J z|||||I d H  W n tyM   | ||||I d H  Y S w t  	 I d H  |S N)
r9   preparer$   compressionrQ   _loop	transportsendfileNotImplementedError	write_eof)r?   rR   rC   rD   rE   rB   rO   rW   r@   r*   r.   	_sendfileu   s   
zFileResponse._sendfile
etag_valueetags.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 rS   )is_weakvalue)r,   etagr\   r*   r.   	<genexpr>   s    z2FileResponse._strong_etag_match.<locals>.<genexpr>)lenr_   r   any)r\   r]   r*   ra   r.   _strong_etag_match   s   zFileResponse._strong_etag_matchlast_modifiedc                    s2   |  tj d| _|| _|| _t |I d H S )NF)
set_statusr   status_code_length_checkr`   rf   r9   rT   )r?   rR   r\   rf   r@   r*   r.   _not_modified   s   zFileResponse._not_modifiedc                    s&   |  tj d| _t |I d H S rF   )rg   r   rh   content_lengthr9   rT   )r?   rR   r@   r*   r.   _precondition_failed   s   z!FileResponse._precondition_failedaccept_encodingc              	   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||
 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?   rm   	file_pathfile_extensionfile_encodingcompressed_pathstr*   r*   r.   _get_file_path_stat_encoding   s   

z)FileResponse._get_file_path_stat_encodingc                    s.  t  }|jtjd }z|d | j|I d H \}}}W n t	y7   | 
tj t |I d H  Y S w t|jsL| 
tj t |I d H S |jdd|jd}|j}|j}	|	d uro| ||	so| |I d H S |j}
|
d ur|	d u r|j|
 kr| |I d H S |j}|d ur| ||r| |||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rZz|j}|j }|j!}W n" t"y   d| | jtj#< | 
t$j t |I d H  Y S w |d us|d urZ|dk r'|d u r'||7 }|dk r"d}|| }nt%|d ur/|n||| }||krRd| | jtj#< | 
t$j t |I d H S t&j}| 
| tj'| jvrmt()| j*d pkt+| _,|r|| jtj-< tj| jtj.< d| _/|| _0|j| _1|| _2d| jtj3< t4t5|}|t&jkrd6||| d	 || jtj#< |dkst7|j8| j9rt |I d H S z|d |j:d
I d H }W n t;y   | 
tj t |I d H  Y S w |r|}nd}z| <||||I d H W t =|d |j>I d H  S t =|d |j>I d H  w )N x-zbytes */r   Fbyteszbytes {}-{}/{}r   rb)?rG   get_running_loopr7   getr   ACCEPT_ENCODINGlowerrI   r{   rr   rg   r   rh   r9   rT   r   rt   r   st_mtime_nsst_sizest_mtimeif_matchre   rl   if_unmodified_since	timestampif_none_matchrj   if_modified_since_statusif_range
http_rangestartstop
ValueErrorCONTENT_RANGEr   rM   r   CONTENT_TYPEr%   
guess_typer=   FALLBACK_CONTENT_TYPEcontent_typeCONTENT_ENCODINGVARY_compressionr`   rf   rk   ACCEPT_RANGESr   intformatr   methodr5   openPermissionErrorr[   shieldclose)r?   rR   rO   rm   rv   rz   rx   r\   rf   ifmatch
unmodsinceifnonematchmodsincer5   	file_sizerE   r   ifrangerngend
real_startrC   rD   r@   r*   r.   rT      s   









8zFileResponse.prepare)r1   r2   NN)__name__
__module____qualname____doc__r   r   r   strr   r:   r   r   r   rQ   r[   staticmethodr   r   boolre   floatrj   rl   r;   r<   osstat_resultr{   rT   __classcell__r*   r*   r@   r.   r!   O   s~    
 
"r!   )CrG   r   r;   sys
contextlibr   	mimetypesr   ru   r   typesr   typingr   r   r   r	   r
   r   r   r   r   r   r   r   r|   r   abcr   helpersr   r   r   typedefsr   r   web_exceptionsr   r   r   r   r   r   web_responser    __all__web_requestr"   r   _T_OnChunkSentr   environr   r$   __annotations__r%   version_infor+   rn   r   ADDITIONAL_CONTENT_TYPESclearro   r   	extensionadd_typer!   r*   r*   r*   r.   <module>   sN   
 8 


