B
    ©0H[)9  ã               @   s  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	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 dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZ dZ G dd„ dƒZ!eG dd„ de!ƒƒZ"ddd„Z#G dd„ deƒZ$e$ƒ Z%dS )é    N)Údatetime)Úurljoin)Úsettings)ÚSuspiciousFileOperation)ÚFileÚlocks)Úfile_move_safe)Úsetting_changed)Útimezone)Ú	safe_join)Úget_random_string)Údeconstructible)Úfilepath_to_uri)Ú
LazyObjectÚcached_property)Úimport_string)Úget_valid_filename)ÚStorageÚFileSystemStorageÚDefaultStorageÚdefault_storagec               @   s†   e Zd ZdZd dd„Zd!dd„Zdd	„ Zd"d
d„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )#r   zŠ
    A base storage class, providing some default behaviors that all other
    storage systems can inherit or override, as necessary.
    Úrbc             C   s   |   ||¡S )z)Retrieve the specified file from storage.)Ú_open)ÚselfÚnameÚmode© r   ú]C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\core\files\storage.pyÚopen   s    zStorage.openNc             C   s<   |dkr|j }t|dƒs"t||ƒ}| j||d�}|  ||¡S )zÁ
        Save new content to the file specified by name. The content should be
        a proper File object or any python file-like object, ready to be read
        from the beginning.
        NÚchunks)Ú
max_length)r   Úhasattrr   Úget_available_nameÚ_save)r   r   Úcontentr    r   r   r   Úsave#   s    

zStorage.savec             C   s   t |ƒS )z‚
        Return a filename, based on the provided filename, that's suitable for
        use in the target storage system.
        )r   )r   r   r   r   r   Úget_valid_name5   s    zStorage.get_valid_namec             C   s¸   t j |¡\}}t j |¡\}}x’|  |¡s<|r²t|ƒ|kr²t j |d|tdƒ|f ¡}|dkrbq"t|ƒ| }|dkr"|d| … }|s”td| ƒ‚t j |d|tdƒ|f ¡}q"W |S )z„
        Return a filename that's free on the target storage system and
        available for new content to be written to.
        z%s_%s%sé   Nr   z‡Storage can not find an available filename for "%s". Please make sure that the corresponding file field allows sufficient "max_length".)	ÚosÚpathÚsplitÚsplitextÚexistsÚlenÚjoinr   r   )r   r   r    Zdir_nameÚ	file_nameZ	file_rootZfile_extZ
truncationr   r   r   r"   <   s     zStorage.get_available_namec             C   s,   t j |¡\}}t j t j ||  |¡¡¡S )z„
        Validate the filename by calling get_valid_name() and return a filename
        to be passed to the save() method.
        )r(   r)   r*   Únormpathr.   r&   )r   ÚfilenameÚdirnamer   r   r   Úgenerate_filename[   s    zStorage.generate_filenamec             C   s   t dƒ‚dS )zá
        Return a local filesystem path where the file can be retrieved using
        Python's built-in open() function. Storage systems that can't be
        accessed using open() should *not* implement this method.
        z,This backend doesn't support absolute paths.N)ÚNotImplementedError)r   r   r   r   r   r)   d   s    zStorage.pathc             C   s   t dƒ‚dS )zD
        Delete the specified file from the storage system.
        z4subclasses of Storage must provide a delete() methodN)r4   )r   r   r   r   r   Údeleteo   s    zStorage.deletec             C   s   t dƒ‚dS )z¤
        Return True if a file referenced by the given name already exists in the
        storage system, or False if the name is available for a new file.
        z5subclasses of Storage must provide an exists() methodN)r4   )r   r   r   r   r   r,   u   s    zStorage.existsc             C   s   t dƒ‚dS )zœ
        List the contents of the specified path. Return a 2-tuple of lists:
        the first item being directories, the second item being files.
        z5subclasses of Storage must provide a listdir() methodN)r4   )r   r)   r   r   r   Úlistdir|   s    zStorage.listdirc             C   s   t dƒ‚dS )zQ
        Return the total size, in bytes, of the file specified by name.
        z2subclasses of Storage must provide a size() methodN)r4   )r   r   r   r   r   Úsizeƒ   s    zStorage.sizec             C   s   t dƒ‚dS )zu
        Return an absolute URL where the file's contents can be accessed
        directly by a Web browser.
        z1subclasses of Storage must provide a url() methodN)r4   )r   r   r   r   r   Úurl‰   s    zStorage.urlc             C   s   t dƒ‚dS )zš
        Return the last accessed time (as a datetime) of the file specified by
        name. The datetime will be timezone-aware if USE_TZ=True.
        z?subclasses of Storage must provide a get_accessed_time() methodN)r4   )r   r   r   r   r   Úget_accessed_time�   s    zStorage.get_accessed_timec             C   s   t dƒ‚dS )z•
        Return the creation time (as a datetime) of the file specified by name.
        The datetime will be timezone-aware if USE_TZ=True.
        z>subclasses of Storage must provide a get_created_time() methodN)r4   )r   r   r   r   r   Úget_created_time—   s    zStorage.get_created_timec             C   s   t dƒ‚dS )zš
        Return the last modified time (as a datetime) of the file specified by
        name. The datetime will be timezone-aware if USE_TZ=True.
        z?subclasses of Storage must provide a get_modified_time() methodN)r4   )r   r   r   r   r   Úget_modified_timež   s    zStorage.get_modified_time)r   )N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r&   r"   r3   r)   r5   r,   r6   r7   r8   r9   r:   r;   r   r   r   r   r      s   


	r   c               @   sÈ   e Zd ZdZd,dd„Zdd„ Zdd„ Zed	d
„ ƒZedd„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZd-dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ ZdS ).r   z%
    Standard filesystem storage
    Nc             C   s(   || _ || _|| _|| _t | j¡ d S )N)Ú	_locationÚ	_base_urlÚ_file_permissions_modeÚ_directory_permissions_moder	   ÚconnectÚ_clear_cached_properties)r   ÚlocationÚbase_urlÚfile_permissions_modeÚdirectory_permissions_moder   r   r   Ú__init__¬   s
    zFileSystemStorage.__init__c             K   sp   |dkr&| j  dd¡ | j  dd¡ nF|dkr>| j  dd¡ n.|dkrV| j  dd¡ n|d	krl| j  d
d¡ dS )z$Reset setting based property values.Ú
MEDIA_ROOTÚbase_locationNrF   Ú	MEDIA_URLrG   ÚFILE_UPLOAD_PERMISSIONSrH   Ú!FILE_UPLOAD_DIRECTORY_PERMISSIONSrI   )Ú__dict__Úpop)r   ÚsettingÚkwargsr   r   r   rE   ´   s    z*FileSystemStorage._clear_cached_propertiesc             C   s   |d kr|S |S )Nr   )r   ÚvaluerR   r   r   r   Ú_value_or_settingÀ   s    z#FileSystemStorage._value_or_settingc             C   s   |   | jtj¡S )N)rU   r@   r   rK   )r   r   r   r   rL   Ã   s    zFileSystemStorage.base_locationc             C   s   t j | j¡S )N)r(   r)   ÚabspathrL   )r   r   r   r   rF   Ç   s    zFileSystemStorage.locationc             C   s4   | j d k	r$| j  d¡s$|  j d7  _ |  | j tj¡S )Nú/)rA   ÚendswithrU   r   rM   )r   r   r   r   rG   Ë   s    zFileSystemStorage.base_urlc             C   s   |   | jtj¡S )N)rU   rB   r   rN   )r   r   r   r   rH   Ñ   s    z'FileSystemStorage.file_permissions_modec             C   s   |   | jtj¡S )N)rU   rC   r   rO   )r   r   r   r   rI   Õ   s    z,FileSystemStorage.directory_permissions_moder   c             C   s   t t|  |¡|ƒƒS )N)r   r   r)   )r   r   r   r   r   r   r   Ù   s    zFileSystemStorage._openc          	   C   sÂ  |   |¡}tj  |¡}tj  |¡s|yD| jd k	rZt d¡}zt || j¡ W d t |¡ X n
t |¡ W n tk
rz   Y nX tj  |¡s”t	d| ƒ‚�xyÒt
|dƒr´t| ¡ |ƒ n´tjtjB tjB ttddƒB }t ||d¡}d }zXt |tj¡ xD| ¡ D ]8}	|d k�r,t|	tƒ�rdnd}
t ||
¡}| |	¡ qþW W d t |¡ |d k	�r\| ¡  n
t |¡ X W n* tk
�r”   |  |¡}|   |¡}Y q˜X P q˜W | jd k	�r¶t || j¡ |  dd	¡S )
Nr   z!%s exists and is not a directory.Útemporary_file_pathÚO_BINARYi¶  ÚwbÚwtú\rW   )!r)   r(   r2   r,   rI   ÚumaskÚmakedirsÚFileNotFoundErrorÚisdirÚIOErrorr!   r   rY   ÚO_WRONLYÚO_CREATÚO_EXCLÚgetattrr   r   ÚlockZLOCK_EXr   Ú
isinstanceÚbytesÚfdopenÚwriteZunlockÚcloseÚFileExistsErrorr"   rH   ÚchmodÚreplace)r   r   r$   Ú	full_pathÚ	directoryZ	old_umaskÚflagsÚfdÚ_fileÚchunkr   r   r   r   r#   Ü   sP    








zFileSystemStorage._savec             C   sV   |st dƒ‚|  |¡}y&tj |¡r0t |¡ n
t |¡ W n tk
rP   Y nX d S )Nz-The name argument is not allowed to be empty.)ÚAssertionErrorr)   r(   ra   ÚrmdirÚremover`   )r   r   r   r   r   r5   %  s    
zFileSystemStorage.deletec             C   s   t j |  |¡¡S )N)r(   r)   r,   )r   r   r   r   r   r,   3  s    zFileSystemStorage.existsc             C   s\   |   |¡}g g  }}x>t |¡D ]0}tj  tj  ||¡¡rF| |¡ q | |¡ q W ||fS )N)r)   r(   r6   ra   r.   Úappend)r   r)   ÚdirectoriesÚfilesÚentryr   r   r   r6   6  s    

zFileSystemStorage.listdirc             C   s   t | j|ƒS )N)r   rF   )r   r   r   r   r   r)   @  s    zFileSystemStorage.pathc             C   s   t j |  |¡¡S )N)r(   r)   Úgetsize)r   r   r   r   r   r7   C  s    zFileSystemStorage.sizec             C   s8   | j d krtdƒ‚t|ƒ}|d k	r,| d¡}t| j |ƒS )Nz&This file is not accessible via a URL.rW   )rG   Ú
ValueErrorr   Úlstripr   )r   r   r8   r   r   r   r8   F  s    

zFileSystemStorage.urlc             C   s(   t jrt |¡jtjd�S t |¡S dS )z‘
        If timezone support is enabled, make an aware datetime object in UTC;
        otherwise make a naive one in the local timezone.
        )ÚtzinfoN)r   ZUSE_TZr   Úutcfromtimestampro   r
   ÚutcÚfromtimestamp)r   Útsr   r   r   Ú_datetime_from_timestampN  s    z*FileSystemStorage._datetime_from_timestampc             C   s   |   tj |  |¡¡¡S )N)r…   r(   r)   Úgetatime)r   r   r   r   r   r9   Y  s    z#FileSystemStorage.get_accessed_timec             C   s   |   tj |  |¡¡¡S )N)r…   r(   r)   Úgetctime)r   r   r   r   r   r:   \  s    z"FileSystemStorage.get_created_timec             C   s   |   tj |  |¡¡¡S )N)r…   r(   r)   Úgetmtime)r   r   r   r   r   r;   _  s    z#FileSystemStorage.get_modified_time)NNNN)r   )r<   r=   r>   r?   rJ   rE   rU   r   rL   rF   rG   rH   rI   r   r#   r5   r,   r6   r)   r7   r8   r…   r9   r:   r;   r   r   r   r   r   ¦   s,    

I
r   c             C   s   t | p
tjƒS )N)r   r   ZDEFAULT_FILE_STORAGE)Zimport_pathr   r   r   Úget_storage_classc  s    r‰   c               @   s   e Zd Zdd„ ZdS )r   c             C   s   t ƒ ƒ | _d S )N)r‰   Z_wrapped)r   r   r   r   Ú_setuph  s    zDefaultStorage._setupN)r<   r=   r>   rŠ   r   r   r   r   r   g  s   r   )N)&r(   r   Úurllib.parser   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.filesr   r   Zdjango.core.files.mover   Zdjango.core.signalsr	   Zdjango.utilsr
   Zdjango.utils._osr   Zdjango.utils.cryptor   Zdjango.utils.deconstructr   Zdjango.utils.encodingr   Zdjango.utils.functionalr   r   Zdjango.utils.module_loadingr   Zdjango.utils.textr   Ú__all__r   r   r‰   r   r   r   r   r   r   Ú<module>   s0     =
