o
    ߥie                     @   s   d dl Z d dl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 ddlmZmZmZ G d	d
 d
eje ZG dd deZdS )    N)IterableOptional)staticfiles)URL)HTTPException)EnvironStartResponseWSGIApp   )FileResponseRedirectResponseResponsec                
   @   sJ   e Zd ZdZdedejdededef
ddZd	e	d
e
dee fddZdS )Filesz
    Provide the WSGI application to download files in the specified path or
    the specified directory under the specified package.

    Support request range and cache (304 status code).
    filepathstat_resultif_none_matchif_modified_sincereturnc                 C   sD   |  t||s| |j|rtd}nt||d}| | |S )Ni0  )r   )r   r   generate_etagr   st_mtimer   set_response_headers)selfr   r   r   r   response r   J/home/ubuntu/.local/lib/python3.10/site-packages/baize/wsgi/staticfiles.pyfile_response   s   


zFiles.file_responseenvironstart_responsec                 C   s   | dd}| dd}| | dd}| |\}}|r1|r1|d us&J | ||||||S | jd u r:td| ||S )NHTTP_IF_NONE_MATCH HTTP_IF_MODIFIED_SINCE	PATH_INFO  )getensure_absolute_pathcheck_path_is_filer   
handle_404r   )r   r   r   r   r   r   r   is_filer   r   r   __call__%   s   
zFiles.__call__N)__name__
__module____qualname____doc__strosr   r   r   r   r   r   bytesr(   r   r   r   r   r      s(    
r   c                       sH   e Zd ZdZdededee fddZde	de
e	 f fdd	Z  ZS )
Pagesa  
    Provide the WSGI application to download files in the specified path or
    the specified directory under the specified package.
    Unlike `Files`, when you visit a directory, it will try to return the content
    of the file named `index.html` in that directory. Or if the `pathname.html` is
    exist, it will return the content of that file.

    Support request range and cache (304 status code).
    r   r   r   c           	      C   s   | dd}| dd}| | dd}| |\}}|d u r4|d ur4|ds4|d7 }| |\}}|d urg|d us>J |rK| ||||||S t|jrgt|d}|j	d|j
d d}t|||S | jd u rptd	| ||S )
Nr   r   r    r!   z.html)r   /)schemepathr"   )r#   r$   r%   endswithr   statS_ISDIRst_moder   replacer3   r   r&   r   )	r   r   r   r   r   r   r   r'   urlr   r   r   r(   C   s2   

zPages.__call__r3   c                    s*   t  |}|d ur|dr|d7 }|S )Nr1   z
index.html)superr$   r4   )r   r3   abspath	__class__r   r   r$   b   s
   
zPages.ensure_absolute_path)r)   r*   r+   r,   r   r   r   r/   r(   r-   r   r$   __classcell__r   r   r<   r   r0   8   s    

"r0   )r.   r5   typingr   r   baizer   baize.datastructuresr   baize.exceptionsr   baize.typingr   r   r	   	responsesr   r   r   	BaseFilesr   r0   r   r   r   r   <module>   s    +