o
    hA                     @   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
   V/var/www/html/magazine_api/magazine_env/lib/python3.10/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 ]}tt||}|ds*|d |kr.|d7 }|}q||kr9|d7 }t|}||rH|| dkrLt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r| 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_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   r2   c                 C   sx   |d u s
t | |kr| S tj| \}}tj|\}}t | | }|d |  }|s1t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_names   s   r8   c                       sH   e Zd ZdZejf fdd	Zedd Zde	de
e f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 r:t| j|}|s(| jr'|  j| jtj7  _d | _n|  j| j|7  _t | j|k st | jdkrCd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,   r2   r8   	RawIOBaser9   r
   r
   r
   r   <module>   s     
%