o
    {l~i                     @   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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	ZdS )    N)settings)ImproperlyConfigured)SuspiciousFileOperation)FileProxyMixin)force_bytesc                 C   s   t | tr| S t| S )z5Wrap Django's force_bytes to pass through bytearrays.)
isinstance	bytearrayr   )content r
   U/var/www/newdalilibackend/backend/venv/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   sR   t | tjr
t| } t| dd}| dr!|ds!|d7 }|dkr'd}|S )z
    Normalize the name.

    Includes cleaning up Windows style paths, ensuring an ending trailing slash,
    and coercing from pathlib.PurePath.
    \/. )r   pathlibPurePathstr	posixpathnormpathreplaceendswith)r   
clean_namer
   r
   r   r   !   s   r   c                 G   s   | }| d}t|}|d }|D ]}tt||}|ds'|d |kr+|d7 }|}q||kr6|d7 }t|}||rE|| dkrIt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   z=the joined path is located outside of the base path component)
rstriplistr   r   joinr   len
startswith
ValueErrorlstrip)basepaths	base_path
final_pathpath_final_pathbase_path_lenr
   r
   r   	safe_join;   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__)storagecorrectr
   r
   r   check_locationa   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    osr(   splitsplitextr   r   r-   )r   
max_lengthdir_name	file_name	file_rootfile_ext
truncationr
   r
   r   get_available_overwrite_namep   s   r<   c                 C   s   t | d p	|  S )Nseekable)hasattrr=   )file_objectr
   r
   r   is_seekable   s   r@   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )ReadBytesWrapperzS
    A wrapper for a file-like object, that makes read() always returns bytes.
    Nc                 C   s    || _ |pt|ddpd| _dS )a   
        :param file: The file-like object to wrap.
        :param encoding: Specify the encoding to use when file.read() returns strings.
            If not provided will default to file.encoding, of if that's not available,
            to utf-8.
        encodingNzutf-8)filer   	_encoding)selfrC   rB   r
   r
   r   __init__   s   zReadBytesWrapper.__init__c                 O   s,   | j j|i |}t|ts|| j}|S N)rC   readr   bytesencoderD   )rE   argskwargsr	   r
   r
   r   rH      s   
zReadBytesWrapper.readc                 C   s   | j   d S rG   )rC   closerE   r
   r
   r   rM      s   zReadBytesWrapper.closec                 C   s   dS )NTr
   rN   r
   r
   r   readable   s   zReadBytesWrapper.readablerG   )r/   
__module____qualname____doc__rF   rH   rM   rO   r
   r
   r
   r   rA      s    

rA   rG   )r3   r   r   django.confr   django.core.exceptionsr   r   django.core.files.utilsr   django.utils.encodingr   r   r   r   r+   r2   r<   r@   rA   r
   r
   r
   r   <module>   s     
&