# HG changeset patch # User Matti Hamalainen # Date 1544619120 -7200 # Node ID d0bcc6c7fc660ea515a47c391af48a14476b74e4 # Parent d536c57fa952c19d5ef2d8ecb48061d23858687a Automatically clean any unnecessary "trash" from thumbnail and medium image directories. diff -r d536c57fa952 -r d0bcc6c7fc66 mgtool.php --- 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);