
    Asi                         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lmZ d Zdd	Zd
 Zd Zd Zd Zd Zd Z G d de	      Zy)    N)settings)ImproperlyConfigured)SuspiciousFileOperation)FileProxyMixin)validate_file_name)force_bytesc                 <    t        | t              r| S t        |       S )z5Wrap Django's force_bytes to pass through bytearrays.)
isinstance	bytearrayr   )contents    Z/var/www/python-projects/worksol/worksolenv/lib/python3.12/site-packages/storages/utils.pyto_bytesr      s    '9%w    c                 $    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defaults     r   settingr      s     8T7++r   c                     t        | t        j                        rt        |       } t	        j
                  |       j                  dd      }| j                  d      r|j                  d      s|dz  }|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_names     r   r!   r!   "   sv     $(()4y ##D)11$<J }}S*"5"5c":c
 S
r   c                 |   | }|j                  d      }t        |      }|dz   }|D ]K  }t        j                  t        j                  ||            }|j                  d      s|dz   |k(  r|dz  }|}M ||k(  r|dz  }t        |      }|j                  |      r||   dk7  rt        d      |j                  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_lens          r   	safe_joinr1   <   s     I  %IKESJ !((
D)IJ==s!2j!@3K 
! Yc
 	NM  +z-/HC/OK
 	
 S!!r   c                     | j                   j                  d      rU| j                   j                  d      }t        dj	                  | j
                  j                  | j                   |            y )Nr   zL{}.location cannot begin with a leading slash. Found '{}'. Use '{}' instead.)locationr'   r)   r   format	__class____name__)storagecorrects     r   check_locationr9   b   sh    ""3'""))#."f!!**  		
 		
 (r   c                 \    | D ]'  }t         j                  j                  |      }|s%|c S  y)zP
    Look up for names in environment. Returns the first element
    found.
    N)osenvironget)namesr   values      r   
lookup_envr@   q   s-    
  

t$Lr   c                    t         j                  j                  |       \  }}dt        j                  |      j
                  v rt        d|z        t        |d       |t        |       |k  r| S t         j                  j                  |      \  }}t        |       |z
  }|d |  }|st        d| z        t         j                  j                  |dj                  ||            } t        | d       | S )Nz..z'Detected path traversal attempt in '%s'T)allow_relative_pathzStorage tried to truncate away entire filename "%s". Please make sure that the corresponding file field allows sufficient "max_length".z{}{})r;   r.   splitr   r   partsr   r   r&   splitextr%   r4   )r   
max_lengthdir_name	file_name	file_rootfile_ext
truncations          r   get_available_overwrite_namerL   |   s     ''---Hiw)///%5@
 	
 yd;SY*4''**95IxTZ'J,J;'I%.045
 	

 77<<&--	8"DEDt6Kr   c                 @    t        | d       xs | j                         S )Nseekable)hasattrrN   )file_objects    r   is_seekablerQ      s     {J//I;3G3G3IIr   c                   *    e Zd ZdZddZd Zd Zd Zy)ReadBytesWrapperzS
    A wrapper for a file-like object, that makes read() always returns bytes.
    Nc                 F    || _         |xs t        |dd      xs d| _        y)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)selfrV   rU   s      r   __init__zReadBytesWrapper.__init__   s%     	!OWT:t%DOr   c                      | j                   j                  |i |}t        |t              s|j	                  | j
                        }|S N)rV   readr
   bytesencoderW   )rX   argskwargsr   s       r   r\   zReadBytesWrapper.read   s<     $))..$1&1'5)nnT^^4Gr   c                 8    | j                   j                          y r[   )rV   closerX   s    r   rb   zReadBytesWrapper.close   s    		r   c                      y)NT rc   s    r   readablezReadBytesWrapper.readable   s    r   r[   )r6   
__module____qualname____doc__rY   r\   rb   rf   re   r   r   rS   rS      s    Pr   rS   r[   )r;   r   r   django.confr   django.core.exceptionsr   r   django.core.files.utilsr   r   django.utils.encodingr   r   r   r!   r1   r9   r@   rL   rQ   rS   re   r   r   <module>rn      sV    	     7 : 2 6 - 
,4#"L
8J~ r   