Mager updated

This commit is contained in:
Simon Pocrnjič
2025-09-27 17:45:55 +02:00
parent d17e34941b
commit 7227c888d4
74 changed files with 6339 additions and 342 deletions
@@ -0,0 +1,64 @@
<?php
namespace App\Console\Commands;
use App\Models\Document;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class PruneDocumentPreviews extends Command
{
protected $signature = 'documents:prune-previews {--days=90 : Delete previews older than this many days} {--dry-run : Show what would be deleted without deleting}';
protected $description = 'Deletes generated document preview files older than N days and clears their metadata.';
public function handle(): int
{
$days = (int) $this->option('days');
if ($days < 1) { $days = 90; }
$cutoff = Carbon::now()->subDays($days);
$previewDisk = config('files.preview_disk', 'public');
$query = Document::query()
->whereNotNull('preview_path')
->whereNotNull('preview_generated_at')
->where('preview_generated_at', '<', $cutoff);
$count = $query->count();
if ($count === 0) {
$this->info('No stale previews found.');
return self::SUCCESS;
}
$this->info("Found {$count} previews older than {$days} days.");
$dry = (bool) $this->option('dry-run');
$query->chunkById(200, function ($docs) use ($previewDisk, $dry) {
foreach ($docs as $doc) {
$path = $doc->preview_path;
if (!$path) { continue; }
if ($dry) {
$this->line("Would delete: {$previewDisk}://{$path} (document #{$doc->id})");
continue;
}
try {
Storage::disk($previewDisk)->delete($path);
} catch (\Throwable $e) {
// ignore errors, continue processing
}
$doc->preview_path = null;
$doc->preview_mime = null;
$doc->preview_generated_at = null;
$doc->save();
}
});
if ($dry) {
$this->info('Dry run completed. No files were deleted.');
} else {
$this->info('Stale previews deleted and metadata cleared.');
}
return self::SUCCESS;
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule): void
{
// Optionally prune old previews daily
if (config('files.enable_preview_prune', true)) {
$days = (int) config('files.preview_retention_days', 90);
if ($days < 1) { $days = 90; }
$schedule->command('documents:prune-previews', [
'--days' => $days,
])->dailyAt('02:00');
}
}
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}