changeset 255:d0bcc6c7fc66

Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 12 Dec 2018 14:52:00 +0200
parents d536c57fa952
children 6ad17bc3d1a6
files mgtool.php
diffstat 1 files changed, 43 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mgtool.php	Wed Dec 12 14:50:56 2018 +0200
+++ b/mgtool.php	Wed Dec 12 14:52:00 2018 +0200
@@ -364,6 +364,36 @@
 }
 
 
+function mgDeleteQuiet($path, &$noDelete)
+{
+  if (is_dir($path))
+  {
+    if (($dirHandle = @opendir($path)) === FALSE)
+      return mgError("Could not read directory '".$path."'.\n");
+
+    while (($dirFile = @readdir($dirHandle)) !== FALSE)
+    {
+      if ($dirFile != "." && $dirFile != "..")
+        mgDeleteQuiet($path."/".$dirFile, $noDelete);
+    }
+
+    closedir($dirHandle);
+
+    if (!array_key_exists($path, $noDelete))
+    {
+      //echo "DEL DIR '".$path."'\n";
+      rmdir($path);
+    }
+  }
+  else
+  if (file_exists($path) && !array_key_exists($path, $noDelete))
+  {
+    //echo "DEL FILE '".$path."'\n";
+    unlink($path);
+  }
+}
+
+
 //
 // Check if we have received the quit signal
 // and if yes, quit cleanly now.
@@ -542,6 +572,12 @@
     }
 
     // Start actual processing
+    $tnPath = $path."/".$galTNPath;
+    $medPath = $path."/".$galMedPath;
+    $generatedFiles = [];
+    $generatedFiles[$tnPath] = 1;
+    $generatedFiles[$medPath] = 1;
+
     $nentries = count($entries);
     $nentry = 0;
     echo $path." .. ";
@@ -566,8 +602,6 @@
           $edata[$ckey] = $cval;
       }
 
-      $tnPath = $path."/".$galTNPath;
-      $medPath = $path."/".$galMedPath;
 
       // Handle entry based on type
       if ($edata["type"] == 0)
@@ -577,6 +611,9 @@
         $tnFilename = $tnPath."/".$ename.".".mgGetSetting("tn_format");
         $capFilename = $path."/".$edata["base"].".txt";
 
+        $generatedFiles[$medFilename] = 1;
+        $generatedFiles[$tnFilename] = 1;
+
         // Check what we need to update ..
         if (!file_exists($medFilename) || filemtime($medFilename) < $edata["mtime"])
           $updFlags |= GUPD_MED_IMAGE;
@@ -654,6 +691,10 @@
       }
     }
 
+    // Delete any "trash" files from medium/thumbnail dirs
+    mgDeleteQuiet($tnPath, $generatedFiles);
+    mgDeleteQuiet($medPath, $generatedFiles);
+
     echo "\r".$path." ..... DONE\n";
 
     mgCheckQuit(TRUE);