
    ՁEjy%                       S SK Jr  S SKrS SKrS SKrS SKJr  S SKJr  \" S5      r\R                  R                  S5      r\(       a7  \R                  R                  \R                  R                  \SS5      5      rO\R                  R                  S	5      rSS
 jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrg)    )annotationsN)datetime)
get_loggerzactions.file_managerWOLFPANEL_DEV_HOMEvarwwwz/var/wwwc                   U (       d  [        S5      e[        R                  R                  U 5      (       dQ  [        R                  R	                  [        R                  R                  [        U R                  S5      5      5      nO[        R                  R	                  U 5      nU[        :w  a:  UR                  [        [        R                  -   5      (       d  [        SU S35      eU$ )zLNormalize and verify that the path is strictly within the allowed BASE_PATH.zpath is required/z2Access denied: Path is outside of base directory ())
ValueErrorospathisabsrealpathjoin	BASE_PATHlstrip
startswithsepPermissionError)	user_pathresolved_paths     >D:\Programs\laragon\www\wolf\agent\src\actions\file_manager.py_verify_safe_pathr      s    +,, 77==##((iAQAQRUAV)WX((3 	!-*B*B9rvvCU*V*V RS`Raabcdd    c                H    U S:  a  U  S3$ U S:  a	  U S-  S S3$ U S-  S S3$ )z.Format file sizes into human-readable strings.   z Bi   z.1fz KBz MB )
size_bytess    r   _format_sizer    $   sI    DR  	k	!t#C(,,,S155r   c                x    U R                  SS5      n[        U5      n[        R                  R	                  U5      (       d  SSU 3S.$ [        R                  R                  U5      (       d  SSU 3S.$ / n[        R                  " U5       H  nUS;   a  M  [        R                  R                  X$5      n[        R                  R                  U5      n [        R                  " U5      nU(       d  [        UR                  5      OSn[        R                  " UR                  5      R                  S	5      n	UR!                  UU(       a  SOSUU	S.5        M     SUS.$ ! [         a    U(       d  S
OSnSn	 NAf = f! [         a.  n
["        R%                  SU
5        S['        U
5      S.s Sn
A
$ Sn
A
ff = f)z0List directory contents under the verified path.r    FzDirectory not found: successerrorzPath is not a directory: ).z..Nz%d.%m.%Y %H:%Mz0 BUnknownfolderfile)nametypesizemodified_atT)r$   itemszFile list failed: %s)getr   r   r   existsisdirlistdirr   statr    st_sizer   fromtimestampst_mtimestrftime	Exceptionappendlogr%   str)payloadr   r   r.   r*   	full_pathis_dirr3   size_str	mtime_strexcs              r   handle_file_listrB   .   s   0
KK+	))4ww~~m,, 0<  ww}}]++ 4YK@ 
 JJ}-D{"]9IWW]]9-F&wwy)=C<5$224==AJJK[\	
 LL$* (	 ! .0 
 	
  &(.5D%	&  
		(#.X
 	

sP   AF +F 7A!F A#E$<'F $E>;F =E>>F 
F9#F4.F94F9c                H    U R                  SS5      n[        U5      n[        R                  R	                  U5      (       a$  [        R                  R                  U5      (       d  SSU 3S.$ [        R                  R                  U5      nUS:  a  SSS.$ [        US5       nUR                  S	5      nS
U;   a  SSS.sSSS5        $  SSS5        [        R                  " U5      u  pgU(       a)  UR                  S5      =(       d    US;   nU(       d  SSS.$ [        USSSS9 nUR                  5       n	SSS5        SW	SS.$ ! , (       d  f       N= f! , (       d  f       N%= f! [         a.  n
[        R                  SU
5        S[        U
5      S.s Sn
A
$ Sn
A
ff = f)z4Read textual files safely (max 2MB, rejects binary).r   r"   FzFile not found: r#       z(File exceeds maximum allowed size of 2MBrbr       zBinary files are not allowedNztext/)zapplication/jsonzapplication/javascriptzapplication/xmlzapplication/x-javascriptzapplication/x-shzapplication/x-bashzapplication/x-phpzapplication/xhtml+xmlrutf-8ignore)encodingerrorsT)r$   contentrJ   zFile read failed: %s)r/   r   r   r   r0   isfilegetsizeopenread	mimetypes
guess_typer   r8   r:   r%   r;   )r<   r   r   	file_sizefchunk	mime_type_is_textrL   rA   s              r   handle_file_readrY   c   s   ;
KK+	))4ww~~m,,BGGNN=4Q4Q +I;7  GGOOM2	& C  -&!FF4LE%$; '& ' !++M:	**73 	y 	E 	8G $; 
 -wxHAffhG I 
 	
= '&6 IH  
		(#.X
 	

sg   A,E) /)E) E) %E	E) AE) E) (E9E) 
EE) 
E&"E) )
F!3#FF!F!c                   Sn U R                  SS5      nU R                  SS5      n[        U5      n[        R                  " [        R                  R                  U5      SS9  UR                  S5      n[        U5      S:  a  S	S
S.$ US-   n[        US5       nUR                  U5        SSS5        [        R                  " X5        SSS.$ ! , (       d  f       N)= f! [         a  n[        R                  SU5        U(       aL  [        R                  R                  U5      (       a(   [        R                  " U5        O! [         a     Of = fS	[!        U5      S.s SnA$ SnAff = f)z&Write text files atomically (max 2MB).Nr   r"   rL   Texist_okrH   rD   Fz+Content exceeds maximum allowed size of 2MBr#   z.tmpwb)r$   writtenzFile write failed: %s)r/   r   r   makedirsr   dirnameencodelenrO   writereplacer8   r:   r%   r0   remover;   )r<   tmp_pathr   rL   r   content_bytesrT   rA   s           r   handle_file_writerh      s>   H%
KK+	++i,))4 	BGGOOM2TB  w/}/ F  !6)(D!QGGM" " 	

8+
 	
	 "!  

		)3/x00		(#  X
 	


sa   BC! 
C! C-"C! 
CC! !
E,+AE'-EE'
EE'EE'!E,'E,c                    U R                  SS5      n[        U5      n[        R                  R	                  U5      (       d  SSU 3S.$ [        R                  R                  U5      (       a  [        R                  " U5        O[        R                  " U5        SSS.$ ! [         a.  n[        R                  SU5        S[        U5      S.s S	nA$ S	nAff = f)
zDelete a file or directory.r   r"   FPath not found: r#   T)r$   deletedzFile delete failed: %sN)r/   r   r   r   r0   r1   shutilrmtreere   r8   r:   r%   r;   )r<   r   r   rA   s       r   handle_file_deletern      s    
KK+	))4ww~~m,, +I;7 
 77==''MM-(IIm$ 
 	
  
		*C0X
 	

s%   AB! AB! !
C+#CCCc                    U R                  SS5      nU R                  SS5      nU(       a  U(       d  SSS.$ SU;   d  SU;   a  SS	S.$ [        U5      n[        R                  R	                  U5      n[        [        R                  R                  XB5      5      n[        R                  R                  U5      (       d  SS
U 3S.$ [        R                  R                  U5      (       a  SSS.$ [        R                  " X55        SSS.$ ! [         a.  n[        R                  SU5        S[        U5      S.s SnA$ SnAff = f)z-Rename a file or directory inside its folder.old_pathr"   new_nameFz'Both old_path and new_name are requiredr#   r
   \zInvalid file name formatrj   z1A file or folder with the new name already existsT)r$   renamedzFile rename failed: %sN)r/   r   r   r   r`   r   r0   renamer8   r:   r%   r;   )r<   rp   rq   old_resolved
parent_dirnew_resolvedrA   s          r   handle_file_renamerx      s3   ,
;;z2.;;z2.x B  (?dh. 3 
 )2WW__\2
(j)KLww~~l++ +H:6  77>>,'' L 
 			,-
 	
  
		*C0X
 	

s5   6D D 
A=D (D 1D 
E#D?9E?Ec                    U R                  SS5      nU R                  SS5      n[        U5      n[        R                  " [        R                  R                  U5      SS9  [        R                  R                  U5      (       a  SSS	.$ US
:X  a  [        R                  " USS9  O[        USSS9 n SSS5        SSS.$ ! , (       d  f       N= f! [         a.  n[        R                  SU5        S[        U5      S	.s SnA$ SnAff = f)z!Create a blank file or directory.r   r"   r+   r)   Tr[   FzPath already existsr#   r(   wrH   )rJ   N)r$   createdzFile create failed: %s)r/   r   r   r_   r   r`   r0   rO   r8   r:   r%   r;   )r<   r   	item_typer   rT   rA   s         r   handle_file_creater}     s    
KK+	KK/	))4 	BGGOOM2TB77>>-(( . 
  KK5mS7;q < 
 	
 <;  
		*C0X
 	

s<   B	C 'C 3C5C 
CC 
D#D DD)r   r;   returnr;   )r   intr~   r;   )r<   dictr~   r   )
__future__r   rQ   r   rl   r   loggerr   r:   environr/   DEV_HOMEr   r   r   r   r   r    rB   rY   rh   rn   rx   r}   r   r   r   <module>r      s    "  	   '(::>>./  hu!EFI  ,I$62
j=
@(
V
:.
b 
r   