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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)Úurljoin)Úurlparse)Úsettings)ÚImproperlyConfigured)ÚFile)ÚStorage)Údeconstructible)Úsettingc                   @   s   e Zd ZdS )ÚFTPStorageExceptionN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú\/var/www/newdalilibackend/backend/venv/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S )$Ú
FTPStoragez6FTP Storage class for Django pluggable storage system.Nc                 C   sb   |pt dƒ}|d u rtdƒ‚|| _|pt dƒpd| _|p"t dƒp"tj}|  |¡| _|| _d | _	d S )NÚFTP_STORAGE_LOCATIONzOYou must set a location at instanciation or at  settings.FTP_STORAGE_LOCATION'.ÚFTP_STORAGE_ENCODINGzlatin-1ÚBASE_URL)
r	   r   ÚlocationÚencodingr   Ú	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   Úsplitted_urlÚconfigr   r   r   r   7   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   ÚloginÚset_pasvÚcwdr
   Úrepr)r   r   r   r   r   Ú_start_connectionM   s0   

ÿ


ÿÿôzFTPStorage._start_connectionc                 C   s   | j  ¡  d | _ d S ©N)r   Úquit©r   r   r   r   Ú
disconnectg   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"   Úsepr5   r/   r0   Úmkdr
   )r   r"   r.   Úpath_splittedÚ	path_partr   r   r   Ú
_mkremdirsk   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)rB   r=   r"   Údirnamer   r.   r5   Ú
storbinaryÚbasenameÚfileÚDEFAULT_CHUNK_SIZEr/   r0   r
   )r   ÚnameÚcontentr.   r   r   r   Ú	_put_filey   s   
ýÿzFTPStorage._put_fileÚrbc                 C   s   t || |d}|S )N)Úmode)ÚFTPStorageFile)r   rH   rL   Ú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.   r5   r=   r"   rC   Ú
retrbinaryrE   ÚwriteÚseekr/   r0   r
   )r   rH   Úmemory_filer.   r   r   r   Ú_readŒ   s   
ÿ
ÿzFTPStorage._readc                 C   s(   |  ¡  |  ¡  |  ||¡ | ¡  |S r8   )Úopenr7   rJ   Úclose)r   rH   rI   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   Ú	retrlinesÚappendr<   Úlenr+   r/   r0   r
   )r   r"   ÚlinesÚdirsÚfilesÚlineÚwordsr   r   r   Ú_get_dir_details¡   s(   €
ÿzFTPStorage._get_dir_detailsc                 C   sB   |   ¡  z|  |¡\}}t| ¡ ƒt| ¡ ƒfW S  ty    ‚ w r8   )r7   rh   ÚlistÚkeysr
   )r   r"   rd   re   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)Úexistsr7   r   Údeleter/   r0   r
   ©r   rH   r   r   r   rm   ¾   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)r7   r   Únlstr=   r"   rC   rE   r/   Ú
error_tempÚ
error_permr0   r
   )r   rH   rp   r   r   r   rl   Ç   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   )r7   rh   r=   r"   rC   rE   r
   )r   rH   rd   re   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.ú\ro   )r   Ú
ValueErrorr   Úreplacern   r   r   r   Úurlâ   s   
zFTPStorage.url)NNN)rK   )r   r   r   Ú__doc__r   r   r7   r;   rB   rJ   rO   rV   rY   rh   rk   rm   rl   rs   rw   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 )rM   c                 C   s,   || _ || _|| _d| _t ¡ | _d| _d S )NF)rH   Ú_storageÚ_modeÚ	_is_dirtyrP   rQ   rF   Ú_is_read)r   rH   ÚstoragerL   r   r   r   r   é   s   

zFTPStorageFile.__init__c                 C   s    t | dƒs| j | j¡| _| jS )NÚ_size)Úhasattrry   rs   rH   r~   r:   r   r   r   rs   ñ   s   
zFTPStorageFile.sizec                 C   s0   | j s| j ¡  | j | j¡| _d| _ | j ¡ S ©NT)r|   ry   r7   rV   rH   rF   Ú	readlinesr:   r   r   r   r   ÷   s
   

zFTPStorageFile.readlinesNc                 C   s2   | j s| j ¡  | j | j¡| _d| _ | j |¡S r€   )r|   ry   r7   rV   rH   rF   Ú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)rz   ÚAttributeErrorrP   rQ   rF   r{   r|   )r   rI   r   r   r   rS     s
   

zFTPStorageFile.writec                 C   s8   | j r| j ¡  | j | j| ¡ | j ¡  | j ¡  d S r8   )r{   ry   r7   rJ   rH   r;   rF   rX   r:   r   r   r   rX     s
   

zFTPStorageFile.closer8   )
r   r   r   r   Úpropertyrs   r   r‚   rS   rX   r   r   r   r   rM   è   s    

rM   )r/   rP   r=   Úurllib.parser   r   Údjango.confr   Údjango.core.exceptionsr   Údjango.core.files.baser   Údjango.core.files.storager   Údjango.utils.deconstructr   Ústorages.utilsr	   Ú	Exceptionr
   r   rM   r   r   r   r   Ú<module>   s     D