a
    ތxd&                     @   s   d dl Z 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l	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ G d
d deZeG dd deZG dd deZdS )    N)datetime)urljoinurlparse)settings)ImproperlyConfigured)File)Storage)deconstructible)settingc                   @   s   e Zd ZdS )FTPStorageExceptionN)__name__
__module____qualname__ r   r   M/var/www/html/Ranjet/env/lib/python3.9/site-packages/storages/backends/ftp.pyr       s   r   c                   @   s   e Zd ZdZd$ddZdd Zdd Zd	d
 Z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S )&
FTPStoragez6FTP Storage class for Django pluggable storage system.Nc                 C   sZ   |p
t d}|d u rtd|| _|p0t dp0d| _|p<tj}| || _|| _d | _	d S )NZFTP_STORAGE_LOCATIONzOYou must set a location at instanciation or at  settings.FTP_STORAGE_LOCATION'.ZFTP_STORAGE_ENCODINGzlatin-1)
r
   r   locationencodingr   Z	MEDIA_URL_decode_location_config	_base_url_connection)selfr   base_urlr   r   r   r   __init__(   s    
zFTPStorage.__init__c                 C   s   t |}i }|jdvrtd|jdkr0td|jdkrDd|d< nd|d< |j|d	< |j|d
< |j|d< |j|d< t|j|d< |S )z1Return splitted configuration data from location.)ftpaftpz(FTPStorage works only with FTP protocol! z#You must at least provide hostname!r   TactiveFpathhostuserpasswdport)	r   schemer   hostnamer   usernamepasswordintr#   )r   r   Zsplitted_urlconfigr   r   r   r   5   s"    







zFTPStorage._decode_locationc                 C   s   | j d ur4z| j   W n tjy2   d | _ Y n0 | j d u rt }| j|_zn|| jd | jd  || jd | jd  | jd r|	d | jd dkr|
| jd  || _ W d S  tjy   td	t| j Y n0 d S )
Nr    r#   r!   r"   r   Fr   r   z'Connection or login error using data %s)r   pwdftplib
all_errorsFTPr   connectr   loginZset_pasvcwdr   repr)r   r   r   r   r   _start_connectionM   s.    



zFTPStorage._start_connectionc                 C   s   | j   d | _ d S N)r   quitr   r   r   r   
disconnecth   s    
zFTPStorage.disconnectc                 C   s   | j  }|tjj}|D ]h}z| j | W q tjy   z| j 	| | j | W n  tjy|   t
d| Y n0 Y q0 q| j | d S )Nz Cannot create directory chain %s)r   r*   splitosr   sepr0   r+   r,   Zmkdr   )r   r   r*   Zpath_splittedZ	path_partr   r   r   
_mkremdirsl   s    
zFTPStorage._mkremdirsc                 C   s   z`|  tj| | j }| jtj| | jdtj| |j	|j
 | j| W n  tjy   td| Y n0 d S )NzSTOR zError writing file %s)r:   r8   r   dirnamer   r*   r0   Z
storbinarybasenamefileZDEFAULT_CHUNK_SIZEr+   r,   r   )r   namecontentr*   r   r   r   	_put_file}   s    
zFTPStorage._put_filerbc                 C   s   t || |d}|S )N)mode)FTPStorageFile)r   r>   rB   Zremote_filer   r   r   _open   s    zFTPStorage._openc                 C   s   t  }zV| j }| jtj| | jdtj	| |j
 | j| |d |W S  tjy~   td| Y n0 d S )NzRETR r   zError reading file %s)ioBytesIOr   r*   r0   r8   r   r;   Z
retrbinaryr<   writeseekr+   r,   r   )r   r>   Zmemory_filer*   r   r   r   _read   s    

zFTPStorage._readc                 C   s(   |   |   | || |  |S r3   )openr2   r@   close)r   r>   r?   r   r   r   _save   s
    zFTPStorage._savec                 C   s   zg }| j d| |j i }i }|D ]j}| }t|dk r@q&|d dkrNq&|d d dkrld||d < q&|d d dkr&t|d	 ||d < q&||fW S  tjy   td
| Y n0 d S )NzLIST    z->r   d-zError getting listing for %s)	r   Z	retrlinesappendr7   lenr(   r+   r,   r   )r   r   linesdirsfileslinewordsr   r   r   _get_dir_details   s$    
zFTPStorage._get_dir_detailsc                 C   s   |    | jd| }|d d dkr|dd   }t|dkrx|d d dkrxtdt|dd  |dd   }t|d	S t	d
| d S )NzMDTM    Z213      Z19il     z%Y%m%d%H%M%Sz*Error getting modification time of file %s)
r2   r   ZsendcmdstriprT   strr(   r   strptimer   )r   r>   respsr   r   r   modified_time   s    $zFTPStorage.modified_timec                 C   sJ   |    z(| |\}}t| t| fW S  tyD    Y n0 d S r3   )r2   rZ   listkeysr   )r   r   rV   rW   r   r   r   listdir   s    zFTPStorage.listdirc                 C   sL   |  |sd S |   z| j| W n  tjyF   td| Y n0 d S )NzError when removing %s)existsr2   r   deleter+   r,   r   r   r>   r   r   r   ri      s    
zFTPStorage.deletec                 C   s   |    z@| jtj|d }||v s:tj||v r@W dS W dS W nH tjy^   Y dS  tj	yr   Y dS  tj
y   td| Y n0 d S )N/TFz"Error when testing existence of %s)r2   r   nlstr8   r   r;   r<   r+   Z
error_temp
error_permr,   r   )r   r>   rl   r   r   r   rh      s     
zFTPStorage.existsc                 C   sd   |    zB| tj|\}}tj||v rB|tj| W S W dS W n ty^   Y dS 0 d S )Nr   )r2   rZ   r8   r   r;   r<   r   )r   r>   rV   rW   r   r   r   size   s    
zFTPStorage.sizec                 C   s&   | j d u rtdt| j |ddS )Nz&This file is not accessible via a URL.\rk   )r   
ValueErrorr   replacerj   r   r   r   url   s    
zFTPStorage.url)NNN)rA   )r   r   r   __doc__r   r   r2   r6   r:   r@   rD   rI   rL   rZ   rd   rg   ri   rh   rn   rr   r   r   r   r   r   $   s"   

	r   c                   @   sB   e Zd Zdd Zedd Zdd Zddd	Zd
d Zdd Z	dS )rC   c                 C   s,   || _ || _|| _d| _t | _d| _d S )NF)r>   _storage_mode	_is_dirtyrE   rF   r=   _is_read)r   r>   ZstoragerB   r   r   r   r      s    
zFTPStorageFile.__init__c                 C   s    t | ds| j| j| _| jS )N_size)hasattrrt   rn   r>   rx   r5   r   r   r   rn     s    
zFTPStorageFile.sizec                 C   s0   | j s&| j  | j| j| _d| _ | j S NT)rw   rt   r2   rI   r>   r=   	readlinesr5   r   r   r   r{   	  s
    
zFTPStorageFile.readlinesNc                 C   s2   | j s&| j  | j| j| _d| _ | j|S rz   )rw   rt   r2   rI   r>   r=   read)r   	num_bytesr   r   r   r|     s
    
zFTPStorageFile.readc                 C   s.   d| j vrtdt|| _d| _d| _d S )Nwz%File was opened for read-only access.T)ru   AttributeErrorrE   rF   r=   rv   rw   )r   r?   r   r   r   rG     s
    
zFTPStorageFile.writec                 C   s8   | j r*| j  | j| j|  | j  | j  d S r3   )rv   rt   r2   r@   r>   r6   r=   rK   r5   r   r   r   rK     s
    

zFTPStorageFile.close)N)
r   r   r   r   propertyrn   r{   r|   rG   rK   r   r   r   r   rC      s   

rC   )r+   rE   r8   r   urllib.parser   r   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.files.baser   Zdjango.core.files.storager   Zdjango.utils.deconstructr	   Zstorages.utilsr
   	Exceptionr   r   rC   r   r   r   r   <module>   s    V