U
    þ¯bA  ã                   @   s¤   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	m
Z
 d dlmZ dd„ Zddd	„Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ ZG dd„ de jƒZdS )é    N)ÚOptional)Úsettings)ÚImproperlyConfiguredÚSuspiciousFileOperation)Úforce_bytesc                 C   s   t | tƒr| S t| ƒS )z5Wrap Django's force_bytes to pass through bytearrays.)Ú
isinstanceÚ	bytearrayr   )Úcontent© r
   ú2/tmp/pip-unpacked-wheel-_urp_i0d/storages/utils.pyÚto_bytes   s    
r   c                 C   s   t t| |ƒS )zö
    Helper function to get a Django setting by name. If setting doesn't exists
    it will return a default.

    :param name: Name of setting
    :type name: str
    :param default: Value if setting is unfound
    :returns: Setting's value
    )Úgetattrr   )ÚnameÚdefaultr
   r
   r   Úsetting   s    
r   c                 C   s>   t  | ¡ dd¡}|  d¡r.| d¡s.|d }|dkr:d}|S )z:
    Cleans the name so that Windows style paths work
    ú\ú/Ú.Ú )Ú	posixpathÚnormpathÚreplaceÚendswith)r   Ú
clean_namer
   r
   r   r   #   s    r   c                 G   s¢   | }|  d¡}dd„ |D ƒ}|d }|D ]8}t t ||¡¡}| d¡sT|d |kr\|d7 }|}q(||krr|d7 }t|ƒ}| |¡r�|| dkr˜tdƒ‚| d¡S )a‰  
    A version of django.utils._os.safe_join for S3 paths.

    Joins one or more path components to the base path component
    intelligently. Returns a normalized version of the final path.

    The final path must be located inside of the base path component
    (otherwise a ValueError is raised).

    Paths outside the base path indicate a possible security
    sensitive operation.
    r   c                 S   s   g | ]}|‘qS r
   r
   )Ú.0Úpr
   r
   r   Ú
<listcomp>F   s     zsafe_join.<locals>.<listcomp>z=the joined path is located outside of the base path component)	Úrstripr   r   Újoinr   ÚlenÚ
startswithÚ
ValueErrorÚlstrip)ÚbaseÚpathsÚ	base_pathZ
final_pathÚpathZ_final_pathZbase_path_lenr
   r
   r   Ú	safe_join7   s    
r'   c                 C   s4   | j  d¡r0| j  d¡}td | jj| j |¡ƒ‚d S )Nr   zL{}.location cannot begin with a leading slash. Found '{}'. Use '{}' instead.)Úlocationr    r"   r   ÚformatÚ	__class__Ú__name__)ZstorageÚcorrectr
   r
   r   Úcheck_location\   s    ýÿr-   c                 C   s&   | D ]}t j |¡}|r|  S qdS )zP
    Look up for names in environment. Returns the first element
    found.
    N)ÚosÚenvironÚget)Únamesr   Úvaluer
   r
   r   Ú
lookup_envh   s    r3   c                 C   sx   |d kst | ƒ|kr| S tj | ¡\}}tj |¡\}}t | ƒ| }|d | … }|sbtd|  ƒ‚tj |d ||¡¡S )Nz‡Storage tried to truncate away entire filename "%s". Please make sure that the corresponding file field allows sufficient "max_length".z{}{})r   r.   r&   ÚsplitÚsplitextr   r   r)   )r   Ú
max_lengthZdir_nameÚ	file_nameZ	file_rootZfile_extZ
truncationr
   r
   r   Úget_available_overwrite_names   s    þÿr8   c                       sF   e Zd ZdZejf‡ fdd„	Zedd„ ƒZe	e
e dœdd„Z‡  ZS )	ÚGzipCompressionWrapperz1Wrapper for compressing file contents on the fly.c                    s,   t ƒ  ¡  || _tj|dd�| _tƒ | _d S )Né   )ÚlevelÚwbits)ÚsuperÚ__init__ÚrawÚzlibÚcompressobjÚcompressr   Úleftover)Úselfr?   r;   ©r*   r
   r   r>   ‰   s    
zGzipCompressionWrapper.__init__c                   C   s   dS )NTr
   r
   r
   r
   r   Úreadable�   s    zGzipCompressionWrapper.readable)ÚbufÚreturnc                 C   s°   t |ƒ}t | jƒ|k rht| j |¡ƒ}|sP| jrh|  j| j tj¡7  _d | _qh|  j| j |¡7  _qt | jƒdkrzdS | jd |… }t |ƒ}||d |…< | j|d … | _|S )Nr   )	r   rC   r   r?   ÚreadrB   Úflushr@   ÚZ_FINISH)rD   rG   ÚsizeÚchunkÚoutputr
   r
   r   Úreadinto“   s     zGzipCompressionWrapper.readinto)r+   Ú
__module__Ú__qualname__Ú__doc__r@   ÚZ_BEST_COMPRESSIONr>   ÚstaticmethodrF   r   r   ÚintrO   Ú__classcell__r
   r
   rE   r   r9   †   s
   
r9   )N)Úior.   r   r@   Útypingr   Zdjango.confr   Zdjango.core.exceptionsr   r   Zdjango.utils.encodingr   r   r   r   r'   r-   r3   r8   Ú	RawIOBaser9   r
   r
   r
   r   Ú<module>   s   
%