cleanup photos older than x hours

This commit is contained in:
Alexander Gabriel 2026-06-28 15:23:39 +02:00
parent c796aea14e
commit 79789f41dd
4 changed files with 50 additions and 0 deletions

View File

@ -63,3 +63,5 @@ AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
VITE_APP_NAME="${APP_NAME}"
PHOTO_RETENTION_HOURS=24

View File

@ -0,0 +1,28 @@
<?php
namespace App\Console\Commands;
use App\Models\PhotoJob;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class CleanupPhotos extends Command
{
protected $signature = 'photos:cleanup';
protected $description = 'Delete photos and their jobs older than the configured retention period';
public function handle(): void
{
$hours = config('fotobox.photo_retention_hours');
$cutoff = now()->subHours($hours);
$jobs = PhotoJob::where('created_at', '<', $cutoff)->get();
foreach ($jobs as $job) {
Storage::disk('public')->delete($job->image_path);
$job->delete();
}
$this->info("Deleted {$jobs->count()} photo(s) older than {$hours} hour(s).");
}
}

17
config/fotobox.php Normal file
View File

@ -0,0 +1,17 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Photo Retention
|--------------------------------------------------------------------------
|
| Photos and their associated jobs older than this many hours will be
| automatically deleted by the photos:cleanup scheduled command.
|
*/
'photo_retention_hours' => (int) env('PHOTO_RETENTION_HOURS', 24),
];

View File

@ -2,7 +2,10 @@
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote');
Schedule::command('photos:cleanup')->hourly();