o
    ʷ6i                     @   s   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	 d dl
mZ d dlmZ d dlmZ e eZedZG d	d
 d
eZG dd deZdS )    N)OrderedDict)NamedTemporaryFile)	smart_str)
EXTENSIONS)settings)
EngineBasez+^(?:.+) (?:[A-Z0-9]+) (?P<x>\d+)x(?P<y>\d+)c                   @   sx   e Zd Z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d Zdd Zdd Zdd Zdd ZdS )EnginezC
    Image object is a dict with source path, options and size
    c                 C   sz  |d dkr| dtjrd|d d< |d |d d< tjd}||d	 d
  |d D ]}|d | }|d|  |durH|d|  q.d}d|v rS|d }tjr_|dks_|d dt|d   }t|ddF}	||	j	 t
tt|}tj|tjtjd}
|
 }|
 \}}|rtdd||f |rtd| ||	  W d   dS 1 sw   Y  dS )z,
        Writes the thumbnail image
        formatJPEGprogressivelineoptions	interlacequality source[0]z-%sNz%sonflattenoffz-flattenz.%srb)suffixmodestdoutstderrzPThe command '%s' exited with a non-zero exit code and printed this to stderr:
%szCaptured stderr: %s)getr   THUMBNAIL_PROGRESSIVETHUMBNAIL_CONVERTsplitappendTHUMBNAIL_FLATTENr   r   namelistmapr   
subprocessPopenPIPEwaitcommunicateEngineErrorjoinloggererrorwriteread)selfimager   	thumbnailargskvr   r   fpp
returncodeouterr r;   j/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/sorl/thumbnail/engines/convert_engine.pyr.      sF   
"zEngine.writec                 C   s   t |d  d S )Nr   )osremove)r0   r1   r;   r;   r<   cleanupF   s   zEngine.cleanupc                 C   s^   t j|j\}}tdd|d}||  W d   n1 s"w   Y  |jt ddS )zM
        Returns the backend image objects from a ImageFile instance
        wbF)r   deleter   N)r   r   size)r=   pathsplitextr"   r   r.   r/   r   )r0   r   _r   r6   r;   r;   r<   	get_imageI   s
   zEngine.get_imagec                 C   s   |d du r=t jd}||d d  tj|tjtjd}|  t	t
|j }t|dt|df|d< |d S )	z?
        Returns the image width and height as a tuple
        rB   Nr   r   r   r   xy)r   THUMBNAIL_IDENTIFYr   r    r%   r&   r'   r(   size_rematchstrr   r/   intgroup)r0   r1   r3   r7   mr;   r;   r<   get_image_sizeT   s    zEngine.get_image_sizec                 C   s   t dd0}|| |  tjd}||jd  tj	|tj
tj
d}| }W d   |dkS 1 s8w   Y  |dkS )z
        This is not very good for imagemagick because it will say anything is
        valid that it can use as input.
        r@   )r   r   r   r   Nr   )r   r.   flushr   rI   r   r    r"   r%   r&   r'   r(   )r0   raw_datar6   r3   r7   retcoder;   r;   r<   is_valid_imagea   s   


zEngine.is_valid_imagec                 C   sh   t j }|dd|d g tj|tjtjd}|  |j	 
 }zt|W S  ty3   Y d S w )Nz-formatz%[exif:orientation]r   r   )r   rI   r   extendr%   r&   r'   r(   r   r/   striprM   
ValueError)r0   r1   r3   r7   resultr;   r;   r<   _get_exif_orientationo   s   

zEngine._get_exif_orientationc                 C   s   t jdr_| |}|r]|d }|dkrd |d< |S |dkr%d|d< |S |dkr/d |d	< |S |d
kr=d|d< d |d< |S |dkrGd|d< |S |dkrUd|d< d |d< |S |dkr]d|d< |S d |d d< |S )Nz
gm convertr      flop   180rotate   flip   90      z-90   zauto-orient)r   r   endswithrY   )r0   r1   orientationr   r;   r;   r<   _orientationz   s:   

zEngine._orientationc                 C   s   |  |}|o
|dv S )N)ra   rc   rd   re   )rY   )r0   r1   rg   r;   r;   r<   _flip_dimensions   s   
zEngine._flip_dimensionsc                 C   s   ||d d< |S )z
        `Valid colorspaces
        <http://www.graphicsmagick.org/GraphicsMagick.html#details-colorspace>`_.
        Backends need to implement the following::

            RGB, GRAY
        r   
colorspacer;   )r0   r1   rj   r;   r;   r<   _colorspace   s   zEngine._colorspacec                 C   s(   d||||f |d d< ||f|d< |S )z!
        Crops the image
        %sx%s+%s+%sr   croprB   r;   )r0   r1   widthheightx_offsety_offsetr;   r;   r<   _crop   s   zEngine._cropc                 C   s8   d|| || ||f |d d< || || f|d< |S )zg
        Crops the image to a set of x,y coordinates (x,y) is top left, (x2,y2) is bottom left
        rl   r   rm   rB   r;   )r0   r1   rG   rH   x2y2r;   r;   r<   _cropbox   s    zEngine._cropboxc                 C   s$   d||f |d d< ||f|d< |S )z0
        Does the resizing of the image
        z%sx%s!r   scalerB   r;   )r0   r1   rn   ro   r;   r;   r<   _scale   s   zEngine._scalec                 C   s>   | d|d d< d|d d< d|d |d f |d d	< |S )
z 
        Pads the image
        padding_colorr   
backgroundcentergravityz%sx%sr      extent)r   )r0   r1   geometryr   r;   r;   r<   _padding   s   zEngine._paddingN)__name__
__module____qualname____doc__r.   r?   rF   rP   rT   rY   rh   ri   rk   rr   ru   rw   r   r;   r;   r;   r<   r      s    .r   c                   @   s   e Zd ZdS )r*   N)r   r   r   r;   r;   r;   r<   r*      s    r*   )loggingr=   rer%   collectionsr   django.core.files.tempr   django.utils.encodingr   sorl.thumbnail.baser   sorl.thumbnail.confr   sorl.thumbnail.engines.baser   	getLoggerr   r,   compilerJ   r   	Exceptionr*   r;   r;   r;   r<   <module>   s    

 8