a
    ތxd                     @   sv   d dl Z d dl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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	   F/var/www/html/Ranjet/env/lib/python3.9/site-packages/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>C       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_join4   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storageZcorrectr	   r	   r
   check_locationY   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_enve   s    r2   c                 C   sx   |d u 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_lengthdir_name	file_nameZ	file_rootZfile_extZ
truncationr	   r	   r
   get_available_overwrite_namep   s    r8   )N)r-   r   Zdjango.confr   Zdjango.core.exceptionsr   r   Zdjango.utils.encodingr   r   r   r   r'   r,   r2   r8   r	   r	   r	   r
   <module>   s   
%