feat(Logator): add delete and reset

This commit is contained in:
Matthieu Bessat 2021-08-17 15:08:06 +02:00
parent b0185a9ac0
commit df5630a701
2 changed files with 32 additions and 2 deletions

View file

@ -18,7 +18,6 @@ if ($_SERVER['REQUEST_URI'] === '/') {
} }
if ($_SERVER['REQUEST_URI'] === '/loggers') { if ($_SERVER['REQUEST_URI'] === '/loggers') {
echo json_encode([ echo json_encode([
'success' => true, 'success' => true,
'data' => getDirsAsArray($logAt) 'data' => getDirsAsArray($logAt)
@ -26,12 +25,13 @@ if ($_SERVER['REQUEST_URI'] === '/loggers') {
exit(); exit();
} }
if (strpos($_SERVER['REQUEST_URI'], '/logger-details') === 0) { if (strpos($_SERVER['REQUEST_URI'], '/logger') === 0) {
if (!isset($_GET['id']) || $_GET['id'] === '') { if (!isset($_GET['id']) || $_GET['id'] === '') {
echo json_encode([ echo json_encode([
'success' => false, 'success' => false,
'message' => 'Bro, I need an ID bro. Why are you doing that to me?' 'message' => 'Bro, I need an ID bro. Why are you doing that to me?'
]); ]);
exit();
} }
$id = preg_replace('/[^0-9a-zA-Z]/', '', $_GET['id']); $id = preg_replace('/[^0-9a-zA-Z]/', '', $_GET['id']);
$path = $logAt . '/' . $id; $path = $logAt . '/' . $id;
@ -44,6 +44,27 @@ if (strpos($_SERVER['REQUEST_URI'], '/logger-details') === 0) {
exit(); exit();
} }
if (isset($_GET['delete']) && ($_GET['delete'] === 'true' || $_GET['delete'] === 'yes')) {
$count = deleteAllFiles($path);
rmdir($path);
echo json_encode([
'success' => true,
'message' => 'Logger deleted!',
'records_deleted_count' => $count
]);
exit();
}
if (isset($_GET['reset']) && ($_GET['reset'] === 'true' || $_GET['reset'] === 'yes')) {
$count = deleteAllFiles($path);
echo json_encode([
'success' => true,
'message' => 'Logger reseted!',
'records_deleted_count' => $count
]);
exit();
}
$data = []; $data = [];
foreach (getFilesAsArray($path) as $file) { foreach (getFilesAsArray($path) as $file) {
$data[] = json_decode(file_get_contents($path . '/' . $file), true); $data[] = json_decode(file_get_contents($path . '/' . $file), true);

View file

@ -54,3 +54,12 @@ function getDirsAsArray($path) {
}, $dirs); }, $dirs);
return array_values($dirs); return array_values($dirs);
} }
function deleteAllFiles($path) {
$count = 0;
foreach (getFilesAsArray($path) as $file) {
unlink($path . '/' . $file);
$count++;
}
return $count;
}