
    ;iQ                         d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZmZ ddlmZmZ  G d d      Z G d	 d
e      Zy)a  
OpenDrop: an open source AirDrop implementation
Copyright (C) 2018  Milan Stute
Copyright (C) 2018  Alexander Heinrich

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
    N)ArchiveEntrynew_archive_entry)	ARCHIVE_EOFentry_clearentry_sourcepathread_disk_descendread_next_header2
write_datawrite_finish_entrywrite_get_bytes_per_blockwrite_header)ArchiveWritenew_archive_read_disk)ExifTagsImagec                   X    e Zd ZdZedefd       Zed        Zedd       Zed        Z	y)	AirDropUtilz
    This class contains a set of utility functions that support the opendrop implementation
    They have been moved, because the opendrop files tend to get too long
    returnc                 R   d}t        | j                        dk(  st        | j                        dk(  r|S | j                  d   }| j                  d   }d|v r.d}d|v rd}|S d|v rd}|S d	|v rd
}|S d|v rd}|S d|v sd|v rd}|S d|v rd}|S d|v rd}|S d|v rd}d|v rd}d|v rd}|S )z
        Get the Apple conform UTI Type from a flp instance which has been used on the data which should be sent

        :param flp: fleep object
        zpublic.contentr   imagezpublic.imagejpgzpublic.jpegjp2zpublic.jpeg-2000gifzcom.compuserve.gifpngz
public.pngrawzpublic.camera-raw-imageaudiozpublic.audiovideozpublic.videoarchivezpublic.datagzipzorg.gnu.gnu-zip-archivezipzpublic.zip-archive)lenmimetype)flputi_typer"   f_types       @/home/ubuntu/myenv/lib/python3.12/site-packages/opendrop/util.pyget_uti_typezAirDropUtil.get_uti_type/   s    $sxx=ASXX!!3Oxx{!d?%H}(* ) $-& % $/" ! $'  $%6/4  %H  %H  & $H~4}/    c                    t        j                  |       }	 t        d |j                         j	                         D              }dddd}|d   }||j                         v r|j                  ||   d      }|j                  d	t         j                         t        j                         }|j                  |d
       |j                         }|S # t        t        f$ r Y jw xY w)z
        Generates a small and a big thumbnail of an image
        This will make it possible to preview the sent file

        :param file_path: The path to the image
        c              3   p   K   | ].  \  }}|t         j                  v rt         j                  |   |f 0 y wN)r   TAGS).0kvs      r'   	<genexpr>z1AirDropUtil.generate_file_icon.<locals>.<genexpr>e   s6      Aq% q!1%s   46   i  Z   )         OrientationT)expand)  r9   JPEG2000)format)r   opendict_getexifitemskeysrotateAttributeErrorKeyError	thumbnail	ANTIALIASioBytesIOsavegetvalue)	file_pathimexifanglesorientation	img_bytes	file_icons          r'   generate_file_iconzAirDropUtil.generate_file_iconY   s     ZZ	"	 KKM//1 D
 ,F}-Kfkkm+YYvk24Y@
 	Z1JJL	
	*-&&(	  ) 		s   A!C C#"C#c                    d } ||       }|y|j                   D ]d  }|j                  r&|r$t        j                  |j                  d         c S |j
                  sB|rEt        j                  |j                        c S  y)aQ  
        Get the ip address in IPv4 or IPv6 for a specific network interface

        :param str interface_name: declares the network interface name for which the ip should be accessed
        :param bool ipv6: Boolean indicating if the ipv6 address should be retrieved
        :return: IPv4Address or IPv6Address object or None
        c                 \    t        j                         D ]  }|j                  | k(  s|c S  y r,   )ifaddrget_adaptersname)rV   	interfaces     r'   get_interface_by_namez?AirDropUtil.get_ip_for_interface.<locals>.get_interface_by_name   s1    #002 %	>>T)$$% r)   Nr   )ipsis_IPv6	ipaddressIPv6Addressipis_IPv4IPv4Address)interface_nameipv6rX   rW   r]   s        r'   get_ip_for_interfacez AirDropUtil.get_ip_for_interface   s{    	 *.9	-- 	4Bzzd ,,EE!H  zz$ ,,RUU33	4 r)   c                    | j                   sy t        j                  j                  | j                        st        j
                  | j                         t        j                  j                  | j                  |      }t        |d      5 }t        |d      r1|j                  |j                                |j                  d       n|j                  |       d d d        y # 1 sw Y   y xY w)Nwbreadr   )debugospathexists	debug_dirmakedirsjoinr<   hasattrwritere   seek)configdata	file_namedebug_file_pathfiles        r'   write_debugzAirDropUtil.write_debug   s    ||ww~~f../KK(()'',,v'7'7C/4( 	!DtV$

499;'		!

4 	! 	! 	!s   AC$$C-N)F)
__name__
__module____qualname____doc__staticmethodstrr(   rQ   rb   ru    r)   r'   r   r   )   s`    
 'S ' 'R # #J  : ! !r)   r   c                       e Zd Zd Zy)AbsArchiveWritec           
         | j                   }t        |      }|dk  rd}t               5 }t        d|      }t	        |      5 }	 t        ||      }|t        k(  rn||_        t        |       t        ||       	 t        t        |      d      5 }		 |	j                  |      }
|
snt        ||
t        |
             +	 ddd       t#        |       t%        |       t&        j(                  j+                  |      rnddd       ddd       y# 1 sw Y   QxY w# t        $ r}|j                   dk7  r Y d}~rd}~ww xY w# 1 sw Y   DxY w# 1 sw Y   yxY w)zM
        Read the given paths from disk and add them to the archive.
        r   i (  Nrb   )_pointerr   r   r   r   r	   r   pathnamer   r   r<   r   re   r
   r!   IOErrorerrnor   r   rg   rh   isdir)selfrh   
store_pathwrite_p
block_sizeentry_pentryread_prfrq   es               r'   add_abs_filezAbsArchiveWrite.add_abs_file   sR    --.w7
?J  	G w/E&t, )&':AK'%/EN%f- '2	"!"27";TB Ea"&'(vvj'9'+$) *7D#d) D	 #' %*	E 'w/(ww}}T*) 	 	E E # "77b=! )" 	 	se   E	6D=8D-D;D7D=:E	DD	D: D50D=5D::D==E	E		EN)rv   rw   rx   r   r|   r)   r'   r~   r~      s    !r)   r~   )ry   rF   r[   rg   rT   libarchive.entryr   r   libarchive.ffir   r   r   r   r	   r
   r   r   r   libarchive.writer   r   PILr   r   r   r~   r|   r)   r'   <module>r      sL   & 
  	  <
 
 
 A @! @!F"l "r)   