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rt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 )NzStorage 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   
%