diff options
Diffstat (limited to 'includes/maintenance')
-rw-r--r-- | includes/maintenance/deleteUnusedAssets.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/includes/maintenance/deleteUnusedAssets.php b/includes/maintenance/deleteUnusedAssets.php new file mode 100644 index 0000000..780c80f --- /dev/null +++ b/includes/maintenance/deleteUnusedAssets.php @@ -0,0 +1,69 @@ +<?php + +global $action; +if (!isset($_SERVER['DOCUMENT_ROOT'])) $_SERVER['DOCUMENT_ROOT'] = "../.."; +if (!isset($action)) $action = "list"; + +if (isset($_SERVER["argv"][1])) { + if ($_SERVER['argv'][1] === "archive") { + $action = "archive"; + } elseif ($_SERVER['argv'][1] === "delete") { + $action = "delete"; + } +} + +$list = [ + str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd/general.json"), true)["uuid"]), + str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/ynmuc/general.json"), true)["uuid"]), + str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/other/general.json"), true)["uuid"]), + ...array_map(function ($i) { + return str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd/general.json"), true)["uuid"]) . str_replace("-", "", $i["uuid"]); + }, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd/members.json"), true)), + ...array_map(function ($i) { + return str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/ynmuc/general.json"), true)["uuid"]) . str_replace("-", "", $i["uuid"]); + }, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/ynmuc/members.json"), true)), + ...array_map(function ($i) { + return str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/other/general.json"), true)["uuid"]) . str_replace("-", "", $i["uuid"]); + }, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/other/members.json"), true)) +]; + +$count = 0; +$left = 0; + +$list = [ + ...array_map(function ($i) { + return $i . ".webp"; + }, $list), + ...array_map(function ($i) { + return $i . ".png"; + }, $list) +]; + +if ($action === "archive") { + if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/archive")) mkdir($_SERVER["DOCUMENT_ROOT"] . "/assets/archive"); + if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/archive/heads")) mkdir($_SERVER["DOCUMENT_ROOT"] . "/assets/archive/heads"); + if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/archive/bodies")) mkdir($_SERVER["DOCUMENT_ROOT"] . "/assets/archive/bodies"); + if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/archive/banners")) mkdir($_SERVER["DOCUMENT_ROOT"] . "/assets/archive/banners"); + if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/archive/avatars")) mkdir($_SERVER["DOCUMENT_ROOT"] . "/assets/archive/avatars"); +} + +foreach (["heads", "bodies", "banners", "avatars"] as $t) { + foreach (array_filter(scandir($_SERVER['DOCUMENT_ROOT'] . "/assets/$t"), function ($i) { + return !str_starts_with($i, "."); + }) as $file) { + if (!in_array($file, $list)) { + echo($file . "\n"); + $count++; + + if ($action === "archive") { + rename($_SERVER['DOCUMENT_ROOT'] . "/assets/$t/" . $file, $_SERVER['DOCUMENT_ROOT'] . "/assets/archive/$t/" . $file); + } elseif ($action === "delete") { + unlink($_SERVER['DOCUMENT_ROOT'] . "/assets/$t/" . $file); + } + } else { + $left++; + } + } +} + +echo($count . " file(s), leaving " . $left . " file(s), action: " . $action . "\n");
\ No newline at end of file |