U
    a<                     @   s   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
 d dlmZ d dlmZ d	d
lmZ d	dlmZmZ dgZG dd deZG dd deZdS )    N)basename)urlparse)settings)staticfiles_storage)finders)FileResponse)get_script_prefix   )
WhiteNoise)decode_if_byte_stringensure_leading_trailing_slashWhiteNoiseMiddlewarec                   @   s   e Zd ZdZdd ZdS )WhiteNoiseFileResponsea  
    Wrap Django's FileResponse to prevent setting any default headers. For the
    most part these just duplicate work already done by WhiteNoise but in some
    cases (e.g. the content-disposition header introduced in Django 3.0) they
    are actively harmful.
    c                 O   s   d S N )selfargskwargsr   r   /home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/whitenoise/middleware.pyset_headers   s    z"WhiteNoiseFileResponse.set_headersN)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   c                       s   e Zd ZdZejd ZdZdZdZde	f fdd	Z
dd Zd	d
 Zedd Zdd Zdd Z fddZdd Zdd Zdd Z  ZS )r   z
    Wrap WhiteNoise to allow it to function as Django middleware, rather
    than WSGI middleware

    This functions as both old- and new-style middleware, so can be included in
    either MIDDLEWARE or MIDDLEWARE_CLASSES.
    )rootuse_findersstatic_prefixNFc                    sb   || _ | | tt| d  | jr8| j| j| jd | jrJ| | j | j	r^| j
s^|   d S )N)prefix)get_responseconfigure_from_settingssuperr   __init__static_rootZ	add_filesr   r   r   autorefreshadd_files_from_finders)r   r   r   	__class__r   r   r!   ,   s    
zWhiteNoiseMiddleware.__init__c                 C   s    |  |}|d kr| |}|S r   )process_requestr   )r   requestresponser   r   r   __call__8   s    

zWhiteNoiseMiddleware.__call__c                 C   s:   | j r| |j}n| j|j}|d k	r6| ||S d S r   )r#   Z	find_fileZ	path_infofilesgetserve)r   r(   static_filer   r   r   r'   >   s
    z$WhiteNoiseMiddleware.process_requestc                 C   sN   |  |j|j}t|j}t|jp$d|d}|d= |jD ]\}}|||< q8|S )Nr   )statuszcontent-type)r   methodZMETAintr/   r   fileheaders)r.   r(   r)   r/   http_responsekeyvaluer   r   r   r-   F   s    

zWhiteNoiseMiddleware.servec              	   C   s   |j | _|j | _t|jpdj| _t d}|rR| j	|rR| jt
|d  | _|j r^d| _| jD ]J}d| }zt||}W n tk
r   Y qdX t|}t| || qdt| j| _t|j| _d S )N /r   zWHITENOISE_{0})DEBUGr#   r   r   Z
STATIC_URLpathr   r   rstrip
startswithlenZmax_ageconfig_attrsformatuppergetattrAttributeErrorr   setattrr   ZSTATIC_ROOTr"   )r   r   Zscript_prefixattrZsettings_keyr6   r   r   r   r   Q   s&    
z,WhiteNoiseMiddleware.configure_from_settingsc                 C   s   i }t  D ]d}|d D ]T\}}t|dd p0dd}d| j||rJdnd|ddf}|||	| qqdd |
 D }| D ]\}}| j|||d qd S )Nr   r7   r8   \c                 S   s   i | ]}|t |qS r   )osstat).0r:   r   r   r   
<dictcomp>x   s      z?WhiteNoiseMiddleware.add_files_from_finders.<locals>.<dictcomp>)
stat_cache)r   Zget_finderslistrA   stripjoinr   replace
setdefaultr:   valuesitemsZadd_file_to_dictionary)r   r+   finderr:   Zstorager   urlrJ   r   r   r   r$   i   s    

	z+WhiteNoiseMiddleware.add_files_from_findersc                 #   sX   | j r4|| jr4t|t| jd  }|r4|V  tt| |}|D ]
}|V  qHd S r   )	r   r<   r   r   findr=   r    r   candidate_paths_for_url)r   rS   r:   pathsr%   r   r   rU   |   s    z,WhiteNoiseMiddleware.candidate_paths_for_urlc                 C   s^   | | jsdS |t| jd }| |}||kr8dS | |}|rZt|t|krZdS dS )z
        Determine whether given URL represents an immutable file (i.e. a
        file with a hash of its contents as part of its name) which can
        therefore be cached forever
        FNT)r<   r   r=   get_name_without_hashget_static_urlr   )r   r:   rS   nameZname_without_hashZ
static_urlr   r   r   immutable_file_test   s    

z(WhiteNoiseMiddleware.immutable_file_testc                 C   s(   t j|\}}t j|d }|| S )aW  
        Removes the version hash from a filename e.g, transforms
        'css/application.f3ea4bcc2.css' into 'css/application.css'

        Note: this is specific to the naming scheme used by Django's
        CachedStaticFilesStorage. You may have to override this if
        you are using a different static files versioning system
        r   )rF   r:   splitext)r   filenameZname_with_hashextrY   r   r   r   rW      s    	z*WhiteNoiseMiddleware.get_name_without_hashc                 C   s,   zt t|W S  tk
r&   Y d S X d S r   )r   r   rS   
ValueError)r   rY   r   r   r   rX      s    z#WhiteNoiseMiddleware.get_static_url)r   r   r   r   r
   r>   r   r   r   r   r!   r*   r'   staticmethodr-   r   r$   rU   rZ   rW   rX   __classcell__r   r   r%   r   r      s    


	)rF   	posixpathr   urllib.parser   Zdjango.confr   Z"django.contrib.staticfiles.storager   Zdjango.contrib.staticfilesr   Zdjango.httpr   Zdjango.urlsr   baser
   Zstring_utilsr   r   __all__r   r   r   r   r   r   <module>   s   