o
    ߥi                     @   s   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 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)Optional)staticfiles)URL)HTTPException)ASGIAppReceiveScopeSend   )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ddfddZdS )Filesz
    Provide the ASGI 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   sF   |  t||s| |j|rtd}|S 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/asgi/staticfiles.pyfile_response   s   

zFiles.file_responsescopereceivesendNc                    s   d}d}|d D ]\}}|dkr| d}q	|dkr | d}q	| |d }| |\}	}
|
rH|	rH|d us9J | ||	|||||I d H S | jd u rQtd| |||I d H S )N headers   if-none-matchlatin-1   if-modified-sincepath  )decodeensure_absolute_pathcheck_path_is_filer   
handle_404r   )r   r   r   r   r   r   kvr   r   is_filer   r   r   __call__%   s*   


zFiles.__call__)__name__
__module____qualname____doc__strosr   r   r   r   r   r	   r-   r   r   r   r   r      s    
r   c                       sH   e Zd ZdZdedededdfddZd	ede	e f fd
dZ
  ZS )PagesaJ  
    Provide the ASGI 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.

    Support request range and cache (304 status code).
    r   r   r   r   Nc                    s*  d}d}|d D ]\}}|dkr| d}q	|dkr | d}q	| |d }| |\}	}
|	d u rG|d urG|dsG|d7 }| |\}	}
|	d ur|d usQJ |
rb| ||	|||||I d H S t|	jrt|d}|j	d|j
d	 d
}t||||I d H S | jd u rtd| |||I d H S )Nr   r    r!   r"   r#   r$   z.html)r   /)schemer$   r%   )r&   r'   r(   endswithr   statS_ISDIRst_moder   replacer$   r   r)   r   )r   r   r   r   r   r   r*   r+   r   r   r,   urlr   r   r   r-   E   s@   



zPages.__call__r$   c                    s*   t  |}|d ur|dr|d7 }|S )Nr5   z
index.html)superr'   r7   )r   r$   abspath	__class__r   r   r'   g   s
   
zPages.ensure_absolute_path)r.   r/   r0   r1   r   r   r	   r-   r2   r   r'   __classcell__r   r   r?   r   r4   ;   s    	""r4   )r3   r8   typingr   baizer   baize.datastructuresr   baize.exceptionsr   baize.typingr   r   r   r	   	responsesr   r   r   	BaseFilesr   r4   r   r   r   r   <module>   s    .