
    R}bg0                         d dl Z d dlZd dlZd dlZd dlZd dlm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 e G d	 d
ee                      Z G d de	          ZdS )    N)urljoin)File)deconstructible)ClosingContextManager)BaseStorage)is_seekablesettingc                        e Zd Z fdZd Zd Zd Zed             Zd Z	ddZ
d	 ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )SFTPStoragec                 F    t                      j        di | | j        | _        | j        | _        | j        | _        | j        | _	        | j
        | _        | j        | _        | j        | _        | j        | _        | j        | _        | j        | _        d | _        d | _        d S )N )super__init__host_hostparams_paramsinteractive_interactive	file_mode
_file_modedir_mode	_dir_modeuid_uidgid_gidknown_host_file_known_host_file	root_path
_root_pathbase_url	_base_url_ssh_sftp)selfsettings	__class__s     p/home/e360mart.nyusoft.in/public_html/e360mart_env/lib/python3.11/site-packages/storages/backends/sftpstorage.pyr   zSFTPStorage.__init__   s    $$8$$$Y
{ ,.H	H	 $ 4.	


    c                 D   t          d          t          di           t          dd          t          d          t          d          t          d          t          d          t          d	          t          d
d          t          d          pt          d          d
S )NSFTP_STORAGE_HOSTSFTP_STORAGE_PARAMSSFTP_STORAGE_INTERACTIVEFSFTP_STORAGE_FILE_MODESFTP_STORAGE_DIR_MODESFTP_STORAGE_UIDSFTP_STORAGE_GIDSFTP_KNOWN_HOST_FILESFTP_STORAGE_ROOT SFTP_BASE_URL	MEDIA_URL)
r   r   r   r   r   r   r   r   r!   r#   r	   r'   s    r*   get_default_settingsz SFTPStorage.get_default_settings)   s    /003R88"#=uEE !9:: 788-..-..&'=>> !4b9900HGK4H4H
 
 	
r+   c                 `   t          j                    | _        | j        p=t          j                            t          j                            ddd                    }t          j                            |          r| j        	                    |           | j        
                    t          j                               	  | j        j        | j        fi | j         n# t           j        $ r}| j        r]d| j        vrTd| j        vrt#          j                    | j        d<   t#          j                    | j        d<   |                                  nt          j        |          Y d }~nd }~ww xY w| j                                        r | j                                        | _        d S d S )N~z.sshknown_hostspasswordusername)paramiko	SSHClientr%   r    ospath
expanduserjoinexistsload_host_keysset_missing_host_key_policyAutoAddPolicyconnectr   r   AuthenticationExceptionr   getpassgetuser_connectget_transport	open_sftpr&   )r'   r   es      r*   rN   zSFTPStorage._connect7   s   &((	/ 
273E3EGLLfm444
 4
 7>>/** 	6I$$_555 		--h.D.F.FGGG	:DIdj99DL9999/ 
	: 
	: 
	:  	:Zt|%C%C T\11/6/@/@DL,+2?+<+<Z(6q999  
	: 9""$$ 	/,,..DJJJ	/ 	/s   C! !E20A8E--E2c                 J    | j         d S | j                                          d S N)r%   closer9   s    r*   rT   zSFTPStorage.closeU   s'    9F	r+   c                     | j         r+| j                                                                        s|                                  | j         S )zLazy SFTP connection)r&   r%   rO   	is_activerN   r9   s    r*   sftpzSFTPStorage.sftpZ   sD     z 	!8!8!:!:!D!D!F!F 	MMOOOzr+   c                 6    t          j        | j        |          S rS   )	posixpathrE   r"   r'   names     r*   _remote_pathzSFTPStorage._remote_patha   s    ~dot444r+   rbc                 $    t          || |          S rS   )SFTPStorageFile)r'   r[   modes      r*   _openzSFTPStorage._opend   s    tT4000r+   c                 b    |                      |          }| j                            |d          S )Nr]   )r\   rW   openr'   r[   remote_paths      r*   _readzSFTPStorage._readg   s+    ''--y~~k4000r+   Nc                     ||,| j                             |          }|p|j        }|p|j        }| j                             |||           dS )z$Set uid and/or gid for file at path.N)rW   statst_uidst_gidchown)r'   rC   r   r   attrs        r*   _chownzSFTPStorage._chownk   sV     ;#+9>>$''D$C$C	c3'''''r+   c                 h   t          j        |          }|                     |          s|                     |           | j                            |           | j         | j                            || j                   | j        s| j	        r$| 
                    || j        | j	                   dS dS )zJCreate directory, recursing up to create parent dirs if
        necessary.Nr   r   )rY   dirname_path_exists_mkdirrW   mkdirr   chmodr   r   rm   )r'   rC   parents      r*   rr   zSFTPStorage._mkdiru   s     "4((  (( 	 KK	>%IOOD$.1119 	<	 	<KK$)K;;;;;	< 	<r+   c                    t          |          r |                    dt          j                   |                     |          }t          j        |          }|                     |          s|                     |           | j	        
                    ||           | j         | j	                            || j                   | j        s| j        r"|                     || j        | j                   |S )zSave file via SFTP.r   Nro   )r   seekrB   SEEK_SETr\   rY   rp   rq   rr   rW   putfor   rt   r   r   rm   )r'   r[   contentrC   rp   s        r*   _savezSFTPStorage._save   s    w 	)LLBK(((  &&#D))  )) 	!KK   	&&& ?&IOOD$/2229 	<	 	<KK$)K;;;r+   c                     	 | j                             |                     |                     d S # t          $ r Y d S w xY wrS   )rW   remover\   OSErrorrZ   s     r*   deletezSFTPStorage.delete   sS    	IT..t4455555 	 	 	DD	s   -1 
??c                 ^    	 | j                             |           dS # t          $ r Y dS w xY w)zVDetermines whether a file existis in the sftp storage given its
        absolute path.TF)rW   rh   FileNotFoundError)r'   rC   s     r*   rq   zSFTPStorage._path_exists   sB    	INN4   4  	 	 	55	s    
,,c                 R    |                      |                     |                    S )a  Determines whether a file exists within the root folder of the SFTP storage
        (as set by `SFTP_STORAGE_ROOT`). This method differs from `._path_exists()`
        in that the provided `name` is assumed to be the relative path of the file
        within the root folder.
        )rq   r\   rZ   s     r*   rF   zSFTPStorage.exists   s&       !2!24!8!8999r+   c                 b    |j         't          j        |j                   t          j        k    S dS NF)st_moderh   S_IFMTS_IFDIR)r'   items     r*   _isdir_attrzSFTPStorage._isdir_attr   s(    <#;t|,,<<5r+   c                 
   |                      |          }g g }}| j                            |          D ]L}|                     |          r|                    |j                   2|                    |j                   M||fS rS   )r\   rW   listdir_attrr   appendfilename)r'   rC   re   dirsfilesr   s         r*   listdirzSFTPStorage.listdir   s    ''--"eI**;77 	, 	,D%% ,DM****T]++++U{r+   c                 j    |                      |          }| j                            |          j        S rS   )r\   rW   rh   st_sizerd   s      r*   sizezSFTPStorage.size   s,    ''--y~~k**22r+   c                     | j         t          d          t          | j         |                              dd          S )Nz&This file is not accessible via a URL.\/)r$   
ValueErrorr   replacerZ   s     r*   urlzSFTPStorage.url   s;    >!EFFFt~t,,44T3???r+   )r]   )NN)__name__
__module____qualname__r   r:   rN   rT   propertyrW   r\   ra   rf   rm   rr   r{   r   rq   rF   r   r   r   r   __classcell__)r)   s   @r*   r   r      sT           
 
 
/ / /<  
   X5 5 51 1 1 11 1 1( ( ( (< < <  $    : : :    3 3 3@ @ @ @ @ @ @r+   r   c                   F    e Zd Zd Zed             ZddZd ZddZd Z	dS )	r_   c                 |    || _         || _        t          j                    | _        || _        d| _        d| _        d S r   )r[   r`   ioBytesIOfile_storage_is_read	_is_dirty)r'   r[   storager`   s       r*   r   zSFTPStorageFile.__init__   s6    		JLL	r+   c                 x    t          | d          s$| j                            | j                  | _        | j        S )N_size)hasattrr   r   r[   r   r9   s    r*   r   zSFTPStorageFile.size   s5    tW%% 	7++DI66DJzr+   Nc                     | j         s+| j                            | j                  | _        d| _         | j                            |          S )NT)r   r   rf   r[   r   read)r'   	num_bytess     r*   r   zSFTPStorageFile.read   s@    } 	!++DI66DI DMy~~i(((r+   c                     d| j         vrt          d          t          j        |          | _        d| _        d| _        d S )Nwz%File was opened for read-only access.T)r`   AttributeErrorr   r   r   r   r   )r'   rz   s     r*   writezSFTPStorageFile.write   sA    di !HIIIJw''	r+   c                    | j         s|                     d           d S | j        rM| j                            | j                  r.| j                            | j        |p| j                  | _        d S t          d          )Nr   zThe file cannot be reopened.)	closedrw   r[   r   rF   ra   r`   r   r   )r'   r`   s     r*   rc   zSFTPStorageFile.open   sw    { 	=IIaLLLLLY 	=4=//	:: 	=++DIt7HtyIIDIII;<<<r+   c                     | j         r | j                            | j        |            | j                                         d S rS   )r   r   r{   r[   r   rT   r9   s    r*   rT   zSFTPStorageFile.close   s>    > 	1M	4000	r+   rS   )
r   r   r   r   r   r   r   r   rc   rT   r   r+   r*   r_   r_      s             X
) ) ) )  = = = =    r+   r_   )rL   r   rB   rY   rh   urllib.parser   r@   django.core.files.baser   django.utils.deconstructr   paramiko.utilr   storages.baser   storages.utilsr   r
   r   r_   r   r+   r*   <module>r      s<    				 				                   ' ' ' ' ' ' 4 4 4 4 4 4 / / / / / / % % % % % % & & & & & & " " " " " " k@ k@ k@ k@ k@' k@ k@ k@\( ( ( ( (d ( ( ( ( (r+   