
    h                     j    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  G d d	e      Zy)
    N)BaseCommand)import_string)UnidentifiedImageError)settings)File)
load_modelc                   .    e Zd ZdZd Zd Zd Zd Zd Zy)CommandzLCheck for orphaned files, missing file references, and set image dimensions.c                    |j                  ddddd       |j                  ddddd	       |j                  d
dddd       |j                  ddddd       |j                  ddddd       |j                  dddddd       y )Nz	--orphans
store_trueorphansFz&Scan media folders for orphaned files.)actiondestdefaulthelpz--delete-orphansdelete_orphansz#Delete orphaned files from storage.z	--missingmissingz/Check file references and report missing files.z--delete-missingdelete_missingzADelete database entries if files are missing in the media folder.z--image-dimensionsimage_dimensionsz)Set image dimensions if they are not set.z	--noinputz
--no-inputstore_falseinteractiveTz1Do not prompt the user for any interactive input.)add_argument)selfparsers     U/home/dcms/DCMS/lib/python3.12/site-packages/filer/management/commands/filer_check.pyadd_argumentszCommand.add_arguments   s    9 	 	
 	!6 	 	
 	B 	 	
 	!T 	 	
 	 #< 	 	
 	 D 	 	
    c                    |d   r| j                  |       |d   rZ|d   rDt        d      dk7  r6| j                  j                  d       | j                  j	                          y | j                  |       |d   s|d   r_|d   rI|d   rDt        d	      dk7  r6| j                  j                  d
       | j                  j	                          y | j                  |       |d   r| j                  |       y y )Nr   r   r   zh
This will delete missing file references from the database.
Type 'yes' to continue, or 'no' to cancel: yesz3Aborted: Missing file references were not deleted.
r   r   zZ
This will delete orphaned files from storage.
Type 'yes' to continue, or 'no' to cancel: z*Aborted: Orphaned files were not deleted.
r   )verify_referencesinputstdoutwriteflushverify_storagesr   )r   argsoptionss      r   handlezCommand.handle=   s   9""7+#$}%B  KK%%&\]KK%%'""7+9)9!:'(W]-CB  KK%%&STKK%%'  )%&!!'* 'r   c           	         t         j                  j                         D ]K  }|j                  j                  j                  |j                  j                        r>|d   r$|j                          d|j                   d| d}nd|j                   d| d}|j                  dd      d	kD  r9| j                  j                  |d
z          | j                  j                          |j                  d      s| j                  j                  t        j                  j                  t!        |j                        t!        |            d
z          | j                  j                          N y)z
        Checks that every file reference in the database exists in storage.
        If a file is missing, either report it or delete the reference based on the provided options.
        r   z Deleted missing file reference '/z' from the database.zFile reference 'z' is missing in storage.	verbosity      
N)r   objectsallfilestorageexistsnamedeletefoldergetr"   r#   r$   ospathjoinstr)r   r'   r1   verbose_msgs       r   r    zCommand.verify_referencesY   s   
 LL$$& 	(D99$$++DIINN;+,KKM$DT[[MQRSWRXXl"mK$4T[[M4&H`"aK;;{A.2KK%%kD&89KK%%'[[-KK%%bggll3t{{3CSY&ORV&VWKK%%'	(r   c                 r     fdt         j                  j                         D ]  \  }}|j                  d      }|s t	        |d                }|j                  di       j                  d      r|d   d   |_        |dv r|n|j                  dd      } ||j                  dd      |        y	)
z
        Scans all storages defined in FILER_STORAGES (e.g., public and private)
        for orphaned files, then reports or deletes them based on the options.
        c           	      R   | j                  |      sy | j                  |      \  }}|D ]-  }t        j                  j	                  ||      }t        j                  j	                  ||      }t
        j                  j                  |      j                         rs
d   r| j                  |       d| d}nd| d}
j                  dd      dkD  r9j                  j                  |d	z          j                  j                          
j                  d      sj                  j                  |d	z          j                  j                          0 |D ]H  }	 | t        j                  j	                  ||	      t        j                  j	                  ||	             J y )
N)r1   r   zDeleted orphaned file ''zFound orphaned file 'r+   r,   r-   r.   )r3   listdirr8   r9   r:   r   r/   filterr5   r7   r"   r#   r$   )r2   prefixlabel_prefix
child_dirsfilesfilenameactual_pathrelfilenamemessagechildr'   r   walks             r   rK   z%Command.verify_storages.<locals>.walkr   sU   >>&) ' 7J! , ggll68< ggll<B||***<CCE/0{3$;K="J$9+a"H{{;2Q6))'D.9))+ [1))+*<=))+, $ ^Wbggll659277<<V[;\]^r   mainENGINEOPTIONSlocation)publicprivateUPLOAD_TO_PREFIX N)filer_settingsFILER_STORAGESitemsr7   r   rO   )r   r'   storage_namestorage_configstorage_settingsr2   rC   rK   s   ``     @r   r%   zCommand.verify_storagesl   s    	^0 -;,I,I,O,O,Q 		V(L.-11&9#?m$4X$>?AG##Ir266zB#3I#>z#J +7;P+P<VfVjVjk}  @B  WCL*../A2FU		Vr   c                    ddl m} ddl}ddlm} ddlm} t        t        j                        }|j                  j                   |d       |d      z        }| j                  j                  d	|j                          d
dz          | j                  j                          |D ]  }t!        |dd      r|j"                  n|}		 |	j$                  }
|
j'                  d       |j$                  j*                  j-                         j/                  d      r4|j1                  |
      5 }|j2                  \  |_        |_        ddd       nT	 |j9                  |
      5 }|j2                  \  |_        |_        |j:                  j=                  |      |_        ddd       |jC                          
 y# t(        $ r Y w xY w# 1 sw Y   -xY w# 1 sw Y   9xY w# t@        $ r Y @w xY w)z
        For images without set dimensions (_width == 0 or None), try to read their dimensions
        and save them, handling SVG files and possible image errors.
        r   )QN)Image)PILImage)_widthT)_width__isnullzSetting dimensions for z imagesr.   file_ptrz.svg)"django.db.modelsr[   easy_thumbnailseasy_thumbnails.VILr\   filer.utils.compatibilityr]   r   rT   FILER_IMAGE_MODELr/   rA   r"   r#   countr$   getattrr`   r1   seekFileNotFoundErrorr4   lowerendswithloadsizer^   _heightopenutilsis_transparent_transparentr   save)r   r'   r[   rb   VILImager]   
ImageModelimages_without_dimensionsimagefile_holderimgfile	vil_image	pil_images                r   r   zCommand.image_dimensions   s   
 	'96 @ @A
$.$6$6$=$=aqkA]aLb>b$c!34M4S4S4U3VV]^aeef. 	E,3E:t,L%..RWK%**Q zz$$&//7]]7+ Ay2;../EL%-A A!w/ ]96?nn3em-<-B-B-Q-QR[-\*]
 JJL%	
 % A A] ] . sH   F4)GG:GG4	G GG	G	G	G)(G)N)	__name__
__module____qualname__r   r   r(   r    r%   r    r   r   r
   r
      s#    YD+
Z+8(&'VR"r   r
   )r8   django.core.management.baser   django.utils.module_loadingr   PILr   filerr   rT   filer.models.filemodelsr   filer.utils.loaderr   r
   r   r   r   <module>r      s(    	 3 5 & , ( )jk jr   