Skip to content

Commit 60cb9e3

Browse files
authored
Added the McLogCleaner-Plugin (#99)
* Added the McLogCleaner-Plugin * Fixed some things coderrabbitai wanted me to change :D * Fixed some things coderrabbitai wanted me to change :D * Fixed some things coderrabbitai wanted me to change :D * Trying to fix things coderabitai want me to do * Trying to fix things coderabitai want me to do * Trying to fix things coderabitai want me to do * Fixed typos * Fixed an issue seen by coderabbitai * Fixed an issue seen by coderabbitai * Fixed an issue seen by coderabbitai * Fixed an issue seen by coderabbitai * Fixed an issue seen by coderabbitai * Fixed an issue seen by coderabbitai * Fixed an issue seen by coderabbitai * Updated version to 1.1.1 to fit to my repo (and the version in discord) * Deleted unnecessary config an enum. The feature-check is now inside of the Action. Updated README.md. * Fix * Fix * Fix
1 parent 4836c68 commit 60cb9e3

6 files changed

Lines changed: 227 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ npm i -g yarn
2424
- [Billing](/billing) - Allows users to purchase servers via Stripe - **Proof of Concept - Do absolutely NOT use in production!**
2525
- [Generic OIDC Providers](/generic-oidc-providers) - Create generic OIDC providers for authentication
2626
- [Legal Pages](/legal-pages) - Adds legal pages (Imprint, Privacy Policy, ToS) to the panel
27+
- [McLogCleaner](/mclogcleaner) - Delete old logs with ease
2728
- [MCLogs Uploader](/mclogs-uploader) - Upload console logs to mclo.gs
2829
- [Minecraft Modrinth](/minecraft-modrinth) - Download Minecraft mods & plugins from Modrinth
2930
- [PasteFox Share](/pastefox-share) - Share console logs via pastefox.com

mclogcleaner/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# McLogCleaner (by JuggleGaming)
2+
3+
McLogCleaner automatically deletes all `.log.gz` files from the server’s `logs` folder.
4+
5+
> **Note:** `latest.log` will always remain intact and is never deleted.
6+
7+
## Setup
8+
To use this plugin, add `mclogcleaner` as a _feature_ to the egg you want to run it with.
9+
10+
## Log Deletion Options
11+
When you click **Delete logs**, a dropdown menu appears where you can choose the **minimum age (in days)** of log files to delete:
12+
- Logs older than 7 days
13+
- Logs older than 30 days
14+
- All logs (regardless of age)
15+
- A custom age in days

mclogcleaner/plugin.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"id": "mclogcleaner",
3+
"name": "McLogCleaner",
4+
"author": "JuggleGaming",
5+
"version": "1.1.1",
6+
"description": "Clean your Minecraft-logs with ease",
7+
"category": "plugin",
8+
"url": "https://github.com/pelican-dev/plugins/tree/main/mclogcleaner",
9+
"update_url": null,
10+
"namespace": "JuggleGaming\\McLogCleaner",
11+
"class": "McLogCleanerPlugin",
12+
"panels": null,
13+
"panel_version": null,
14+
"composer_packages": null
15+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
namespace JuggleGaming\McLogCleaner\Filament\Components\Actions;
4+
5+
use App\Models\Server;
6+
use Carbon\Carbon;
7+
use Exception;
8+
use Filament\Actions\Action;
9+
use Filament\Facades\Filament;
10+
use Filament\Forms\Components\Select;
11+
use Filament\Forms\Components\TextInput;
12+
use Filament\Notifications\Notification;
13+
use Filament\Support\Enums\Size;
14+
use Illuminate\Support\Facades\Http;
15+
16+
class McLogCleanAction extends Action
17+
{
18+
public static function getDefaultName(): ?string
19+
{
20+
return 'clean_logs';
21+
}
22+
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
$this->hidden(function () {
27+
/** @var Server|null $server */
28+
$server = Filament::getTenant();
29+
if (!$server) {
30+
return true;
31+
}
32+
$server->loadMissing('egg');
33+
$features = $server->egg->features ?? [];
34+
35+
return !in_array('mclogcleaner', $features, true);
36+
});
37+
38+
$this->label('Delete logs');
39+
$this->icon('tabler-trash');
40+
$this->color('danger');
41+
$this->size(Size::ExtraLarge);
42+
$this->requiresConfirmation()
43+
->modalHeading('Delete logs')
44+
->modalDescription('Choose which logs should be deleted.')
45+
->modalSubmitActionLabel('Delete logs')
46+
->form([
47+
Select::make('mode')
48+
->label('Delete logs')
49+
->options([
50+
7 => 'Older than 7 days',
51+
30 => 'Older than 30 days',
52+
-1 => 'Delete all logs',
53+
'custom' => 'Custom (days)',
54+
])
55+
->default(7)
56+
->required()
57+
->reactive(),
58+
TextInput::make('custom_days')
59+
->label('Delete logs older than (days)')
60+
->numeric()
61+
->minValue(1)
62+
->maxValue(365)
63+
->placeholder('e.g. 14')
64+
->required(fn ($get) => $get('mode') === 'custom')
65+
->visible(fn ($get) => $get('mode') === 'custom'),
66+
]);
67+
68+
$this->action(function (array $data) {
69+
/** @var Server|null $server */
70+
$server = Filament::getTenant();
71+
$mode = $data['mode'];
72+
if ($mode !== 'custom') {
73+
$mode = (int) $mode;
74+
}
75+
if ($mode === 'custom') {
76+
$days = max(1, (int) $data['custom_days']);
77+
} elseif ($mode === -1) {
78+
$days = 0;
79+
} else {
80+
$days = $mode;
81+
}
82+
try {
83+
$files = Http::daemon($server->node)
84+
->get("/api/servers/{$server->uuid}/files/list-directory", [
85+
'directory' => 'logs',
86+
])
87+
->throw()
88+
->json();
89+
if (!is_array($files)) {
90+
throw new Exception('Invalid log directory response.');
91+
}
92+
$threshold = now()->subDays($days)->startOfDay();
93+
$logsToDelete = collect($files)
94+
->filter(fn ($file) => str_ends_with($file['name'], '.log.gz'))
95+
->filter(function ($file) use ($days, $threshold) {
96+
if ($days === 0) {
97+
return true;
98+
}
99+
$logDate = $this->extractLogDate($file['name']);
100+
if (!$logDate) {
101+
return false;
102+
}
103+
104+
return $logDate->lessThan($threshold);
105+
})
106+
->pluck('name')
107+
->map(fn ($name) => 'logs/' . $name)
108+
->values()
109+
->all();
110+
if (empty($logsToDelete)) {
111+
Notification::make()
112+
->title('McLogCleaner')
113+
->body('No logs matching your selection were found.')
114+
->success()
115+
->send();
116+
117+
return;
118+
}
119+
Http::daemon($server->node)
120+
->post("/api/servers/{$server->uuid}/files/delete", [
121+
'root' => '/',
122+
'files' => $logsToDelete,
123+
])
124+
->throw();
125+
Notification::make()
126+
->title('Logfolder cleaned')
127+
->body(count($logsToDelete) . ' files were deleted.')
128+
->success()
129+
->send();
130+
} catch (\Throwable $e) {
131+
report($e);
132+
Notification::make()
133+
->title('Cleanup failed.')
134+
->body('An error occurred while deleting log files. Please try again later.')
135+
->danger()
136+
->send();
137+
}
138+
});
139+
}
140+
141+
private function extractLogDate(string $filename): ?Carbon
142+
{
143+
if (preg_match('/(\d{4}-\d{2}-\d{2})/', $filename, $matches)) {
144+
$date = Carbon::createFromFormat('Y-m-d', $matches[1]);
145+
146+
return $date ? $date->startOfDay() : null;
147+
}
148+
149+
return null;
150+
}
151+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace JuggleGaming\McLogCleaner;
4+
5+
use Filament\Contracts\Plugin;
6+
use Filament\Panel;
7+
8+
class McLogCleanerPlugin implements Plugin
9+
{
10+
public function getId(): string
11+
{
12+
return 'mclogcleaner';
13+
}
14+
15+
public function register(Panel $panel): void
16+
{
17+
//
18+
}
19+
20+
public function boot(Panel $panel): void
21+
{
22+
//
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace JuggleGaming\McLogCleaner\Providers;
4+
5+
use App\Enums\HeaderActionPosition;
6+
use App\Filament\Server\Pages\Console;
7+
use Illuminate\Support\ServiceProvider;
8+
use JuggleGaming\McLogCleaner\Filament\Components\Actions\McLogCleanAction;
9+
10+
class McLogCleanerPluginProvider extends ServiceProvider
11+
{
12+
public function register(): void
13+
{
14+
Console::registerCustomHeaderActions(HeaderActionPosition::Before, McLogCleanAction::make());
15+
}
16+
17+
public function boot(): void
18+
{
19+
//
20+
}
21+
}

0 commit comments

Comments
 (0)