U
    ŽHšd5  ã                   @   sŽ   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 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dd„ ZdS )é    N)Ú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-1m3lyvgh/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   sR   t | tjƒrt| ƒ} t | ¡ dd¡}|  d¡rB| d¡sB|d7 }|dkrNd}|S )z“
    Normalize the name.

    Includes cleaning up Windows style paths, ensuring an ending trailing slash,
    and coercing from pathlib.Path.
    ú\ú/Ú.Ú )r   ÚpathlibÚPathÚstrÚ	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>I   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_join:   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_envk   s    r5   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!   r0   r(   ÚsplitÚsplitextr   r    r+   )r   Ú
max_lengthZdir_nameÚ	file_nameZ	file_rootZfile_extZ
truncationr	   r	   r
   Úget_available_overwrite_namev   s    þÿr:   c                 C   s   t | dƒ p|  ¡ S )NÚseekable)Úhasattrr;   )Zfile_objectr	   r	   r
   Úis_seekable‰   s    r=   )N)r0   r   r   Zdjango.confr   Zdjango.core.exceptionsr   r   Zdjango.utils.encodingr   r   r   r   r)   r/   r5   r:   r=   r	   r	   r	   r
   Ú<module>   s   
%