o
    h&                     @   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   ]/var/www/html/magazine_api/magazine_env/lib/python3.10/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|| _|pt dpd| _|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rtd|jdkr"d|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rz| j   W n tjy   d | _ Y nw | j d u rpt }| j|_z7|| jd | jd  || jd | jd  | jd rJ|	d | jd dkrY|
| jd  || _ W d S  tjyo   td	t| j w 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   s4   




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 ]1}z| j | W q tjy?   z| j 	| | j | W n tjy<   t
d| w Y qw | 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   z1|  tj| | j }| jtj| | jdtj| |j	|j
 | j| W d S  tjy?   td| w )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  }z+| j }| jtj| | jdtj	| |j
 | j| |d |W S  tjy=   td| w )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   zMg }| j d| |j i }i }|D ]5}| }t|dk r q|d dkr'q|d d dkr6d||d < q|d d dkrHt|d	 ||d < q||fW S  tjy[   td
| w )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rB|dd   }t|dkr<|d d dkr<tdt|dd  |dd   }t|d	S t	d
| )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   sB   |    z| |\}}t| t| fW S  ty     w r3   )r2   rZ   listkeysr   )r   r   rV   rW   r   r   r   listdir   s   zFTPStorage.listdirc                 C   sF   |  |sd S |   z	| j| W d S  tjy"   td| w )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  tjy-   Y dS  tj	y7   Y dS  tj
yD   td| w )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   s\   |    z| tj|\}}tj||v r!|tj| W S W dS  ty-   Y dS w )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   Z	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.closer3   )
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