o
    ʷ6i                     @   sP  d dl Z 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	m
Z
mZmZ d dlmZmZ d dlmZmZ d dlm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"m#Z#m$Z$m%Z% d dl&m'Z' e(dZ)edd Z*dd Z+dd Z,G dd dZ-G dd de-Z.G dd de-Z/G dd deZ0dd Z1dS )     N)	lru_cache)URLError)quote
quote_plusurlsplit
urlunsplit)Requesturlopen)ContentFileFile)Storage)	force_str)
LazyObjectempty)default)settingsstorage)ThumbnailErrordeserializeget_module_classtokey)parse_geometryz^(https?|ftp):\/\/c                 C   s
   t |  S N)r   r    r   Z/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/sorl/thumbnail/images.pyget_or_create_storage      
r   c                 C   s0   | j d u r	td| j|  | j d}t|S )Nz:Trying to serialize an ``ImageFile`` with a ``None`` size.)namer   size)r   r   r   serialize_storagejsondumps)
image_filedatar   r   r   serialize_image_file   s   

r%   c                 C   s0   t | }t|d t|d }||d  |S )Nr   r   r   )r   	ImageFiler   set_size)sr$   r#   r   r   r   deserialize_image_file)   s   r)   c                   @   s\   e Zd Zg Zdd Zedd ZeZedd ZeZ	dd Z
ed	d
 Zedd ZeZdS )BaseImageFilec                 C      t  r   NotImplementedErrorselfr   r   r   exists3      zBaseImageFile.existsc                 C   
   | j d S )Nr   r   r.   r   r   r   width6   r   zBaseImageFile.widthc                 C   r2   )N   r3   r.   r   r   r   height<   r   zBaseImageFile.heightc                 C   s   | j | jkS r   )yxr.   r   r   r   is_portraitB   s   zBaseImageFile.is_portraitc                 C   s   t | jt | j S r   )floatr8   r7   r.   r   r   r   ratioE   s   zBaseImageFile.ratioc                 C   r+   r   r,   r.   r   r   r   urlI      zBaseImageFile.urlN)__name__
__module____qualname__r   r0   propertyr4   r8   r6   r7   r9   r;   r<   srcr   r   r   r   r*   0   s    



r*   c                   @   s   e Zd ZdZdddZdd Zdd Zddd	Zd
d Ze	dd Z
e	dd Zdd Zdd Zdd Zdd Ze	dd Zdd ZdS )r&   Nc                 C   s   |st dt|dr|j| _nt|| _tjr!| jdd | _| jdr-d| j | _|d ur5|| _nt|dr?|j| _nt	
| jrJt | _nt| _t| jdrt| jj}| jjd	sb|d	7 }| j|rv| jt|d  | _d S d S d S )
NzFile is empty.r   ?r   z//zhttp:r   location/)r   hasattrr   r   r   THUMBNAIL_REMOVE_URL_ARGSsplit
startswithr   url_patmatch
UrlStoragedefault_storagerD   endswithlen)r/   file_r   rD   r   r   r   __init__S   s0   





zImageFile.__init__c                 C      | j S r   )r   r.   r   r   r   __str__y   r1   zImageFile.__str__c                 C      | j | jS r   )r   r0   r   r.   r   r   r   r0   |      zImageFile.existsc                 C   st   |d urn.| j d urd S t| jdr| j| j}ntj| }tj|}| 	|r3t
|}|  t
|| _ d S )N
image_size)_sizerF   r   rV   r   r   engine	get_imageget_image_sizeflip_dimensionslistreverse)r/   r   imager   r   r   r'      s   

zImageFile.set_sizec                 C   s   t j|S )zw
        Do not manipulate image, but ask engine whether we'd be doing a 90deg
        rotation at some point.
        )r   rX   r[   )r/   r^   r   r   r   r[      s   zImageFile.flip_dimensionsc                 C   rR   r   )rW   r.   r   r   r   r      r=   zImageFile.sizec                 C   rT   r   )r   r<   r   r.   r   r   r   r<      s   zImageFile.urlc                 C   s,   | j | j}z	| W |  S |  w r   )r   openr   readclose)r/   fr   r   r   r`      s   zImageFile.readc                 C   s0   t |ts	t|}d | _| j| j|| _| jS r   )
isinstancer   r
   rW   r   saver   )r/   contentr   r   r   write   s
   
zImageFile.writec                 C   rT   r   )r   deleter   r.   r   r   r   rg      rU   zImageFile.deletec                 C   sF   t | jtr| jjtu r| j  | jjj}n| jj}d|j|jf S )Nz%s.%s)	rc   r   r   _wrappedr   _setup	__class__r?   r>   )r/   clsr   r   r   r       s   
zImageFile.serialize_storagec                 C   s   t | j|  S r   )r   r   r    r.   r   r   r   key   s   zImageFile.keyc                 C   s   t | S r   )r%   r.   r   r   r   	serialize   s   zImageFile.serializer   )r>   r?   r@   rW   rQ   rS   r0   r'   r[   rA   r   r<   r`   rf   rg   r    rl   rm   r   r   r   r   r&   P   s$    
&


	
r&   c                   @   s(   e Zd Zdd Zdd Zedd ZdS )DummyImageFilec                 C   s   t |tj| _d S r   )r   r   THUMBNAIL_DUMMY_RATIOr   )r/   geometry_stringr   r   r   rQ      s   
zDummyImageFile.__init__c                 C   s   dS )NTr   r.   r   r   r   r0         zDummyImageFile.existsc                 C   s   t j| j| jd S )N)r4   r6   )r   THUMBNAIL_DUMMY_SOURCEr8   r7   r.   r   r   r   r<      s   zDummyImageFile.urlN)r>   r?   r@   rQ   r0   rA   r<   r   r   r   r   rn      s
    rn   c                   @   s8   e Zd ZdddZdddZdd Zd	d
 Zdd ZdS )rL   utf-8c                 C   sT   |j |dd}|jddd}t|\}}}}}t|d}t|d}t|||||fS )Nignore)errorsasciis   /%s   :&%=)encodedecoder   r   r   r   )r/   r<   charsetschemenetlocpathqsanchorr   r   r   normalize_url   s   

zUrlStorage.normalize_urlrbc                 C   s@   |  |}t d }dj|d}t|d|id}t|tjdS )Nr   z!python-urllib{python_version}/0.6)python_versionz
User-Agent)headers)timeout)r   platformpython_version_tupleformatr   r	   r   THUMBNAIL_URL_TIMEOUT)r/   r   moder<   r   
user_agentreqr   r   r   r_      s
   
zUrlStorage.openc                 C   s&   z|  | W dS  ty   Y dS w )NFT)r_   r   r/   r   r   r   r   r0      s   zUrlStorage.existsc                 C   s   |S r   r   r   r   r   r   r<      rq   zUrlStorage.urlc                 C   s   d S r   r   r   r   r   r   rg      rq   zUrlStorage.deleteN)rs   )r   )r>   r?   r@   r   r_   r0   r<   rg   r   r   r   r   rL      s    

rL   c                     s&   t j tj}  fdd|  d S )Nc              	      s|     | \}}|D ]} tj| | q	|D ]#}tj| |}| z |}W n	 ty5   Y qw t| qd S r   )listdirrg   osr|   join	Exceptionrmdir)r|   dirsfilesrb   d	directory	full_pathr   walkr   r   r      s   z#delete_all_thumbnails.<locals>.walk)r   r   r   THUMBNAIL_PREFIX)r|   r   r   r   delete_all_thumbnails   s   r   )2r!   r   r   re	functoolsr   urllib.errorr   urllib.parser   r   r   r   urllib.requestr   r	   django.core.files.baser
   r   django.core.files.storager   django.utils.encodingr   django.utils.functionalr   r   sorl.thumbnailr   sorl.thumbnail.confr   sorl.thumbnail.defaultr   rM   sorl.thumbnail.helpersr   r   r   r   sorl.thumbnail.parsersr   compilerJ   r   r%   r)   r*   r&   rn   rL   r   r   r   r   r   <module>   s6    

 y!