a
    nJh1-                     @   s  U d dl Z d dlZd dlZd dl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  dd	l!m"Z" d
Z#erddl$m%Z% ee	e&ged f  Z'e(ej)*dZ+e
e( e,d< G dd de"Z-dS )    N)IOTYPE_CHECKINGAny	AwaitableCallableFinalIteratorListOptionalTupleUnioncast   )hdrs)AbstractStreamWriter)ETAG_ANYETagmust_be_empty_body)LooseHeadersPathLike)HTTPNotModifiedHTTPPartialContentHTTPPreconditionFailedHTTPRequestRangeNotSatisfiable)StreamResponse)FileResponse)BaseRequestZAIOHTTP_NOSENDFILE
NOSENDFILEc                       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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)r"   r#   r$   )super__init__pathlibPath_path_chunk_size)selfr    r!   r"   r#   r$   	__class__ J/var/www/shaz/venv/lib/python3.9/site-packages/aiohttp/web_fileresponse.pyr'   /   s    zFileResponse.__init__)writerfobjoffsetcountr%   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+   asyncioget_event_looprun_in_executorseekreadwriteminZdrain)r,   r1   r2   r3   r4   r!   loopchunkr/   r/   r0   _sendfile_fallback<   s    zFileResponse._sendfile_fallbackr   )requestr2   r3   r4   r%   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)
r&   preparer   compressionr?   _loop	transportsendfileNotImplementedError	write_eof)r,   r@   r2   r3   r4   r1   r=   rE   r-   r/   r0   	_sendfileR   s    
zFileResponse._sendfile.)
etag_valueetagsr%   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 rA   )Zis_weakvalue).0etagrJ   r/   r0   	<genexpr>k       z2FileResponse._strong_etag_match.<locals>.<genexpr>)lenrL   r   any)rJ   rK   r/   rO   r0   _strong_etag_matchg   s    zFileResponse._strong_etag_match)r@   rJ   last_modifiedr%   c                    s0   |  tj d| _|| _|| _t |I d H S )NF)
set_statusr   status_codeZ_length_checkrN   rU   r&   rB   )r,   r@   rJ   rU   r-   r/   r0   _not_modifiedm   s
    zFileResponse._not_modified)r@   r%   c                    s$   |  tj d| _t |I d H S r5   )rV   r   rW   content_lengthr&   rB   )r,   r@   r-   r/   r0   _precondition_failedx   s    z!FileResponse._precondition_failed)check_for_gzipped_filer%   c                 C   sL   | j }|r>||jd }z|| dfW S  ty<   Y n0 || dfS )zReturn the file path, stat result, and gzip status.

        This method should be called from a thread executor
        since it calls os.stat which may block.
        z.gzTF)r*   	with_namenamestatOSError)r,   r[   filepathZ	gzip_pathr/   r/   r0   _get_file_path_stat_and_gzip   s    z)FileResponse._get_file_path_stat_and_gzipc                    s  t  }d|jtjd v }|d | j|I d H \}}}|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r| ||r| |||I d H S |j}|d ur"|d u r"|j| kr"| |||I d H S tj| jvrRtt|\}}|sLd}d}n|r\dnd }d}| 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 r2d	}|| }nt$|d urL|n||| }||krd| | jtj< | t j! t" #|I d H S t%j!}| | |r|| _&|r|| jtj'< |rtj| jtj(< d| _)|| _*|j| _+|| _,d
| jtj-< t.t/|}|t%j!kr.d0||| d || jtj< |d	ksHt1|j2| j3rZt" #|I d H S |d |j4dI d H }|r||}nd	}z2| 5||||I d H W t 6|d |j7I d H  S t 6|d |j7I d H  0 d S )Ngzip x-zapplication/octet-streamTFzbytes */r   byteszbytes {}-{}/{}r   rb)8r6   r7   r$   getr   ACCEPT_ENCODINGlowerr8   ra   st_mtime_nsst_sizest_mtimeZif_matchrT   rZ   Zif_unmodified_since	timestampZif_none_matchrX   Zif_modified_sinceZCONTENT_TYPE	mimetypes
guess_typestrZ_statusif_rangeZ
http_rangestartstop
ValueErrorZCONTENT_RANGErV   r   rW   r&   rB   r<   r   content_typeZCONTENT_ENCODINGZVARY_compressionrN   rU   rY   ZACCEPT_RANGESr   intformatr   methodr"   openrI   shieldclose)r,   r@   r=   r[   r`   strb   rJ   rU   ZifmatchZ
unmodsinceZifnonematchZmodsincectencodingZshould_set_ctr"   	file_sizer4   rs   ZifrangerngendZ
real_startr2   r3   r-   r/   r0   rB      s    








zFileResponse.prepare)r   r   NN)__name__
__module____qualname____doc__r   rx   r
   rq   r   r'   r   r   r   r?   rI   staticmethodr   r   boolrT   floatrX   rZ   r(   r)   osstat_resultra   rB   __classcell__r/   r/   r-   r0   r   ,   s>       r   ).r6   ro   r   r(   typingr   r   r   r   r   r   r   r	   r
   r   r   r   rc   r   abcr   Zhelpersr   r   r   Ztypedefsr   r   Zweb_exceptionsr   r   r   r   Zweb_responser   __all__Zweb_requestr   rf   Z_T_OnChunkSentr   environrh   r   __annotations__r   r/   r/   r/   r0   <module>   s    
8