From 758a828fdffbabadc59313396f64a150f1b835ab Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 16 Mar 2022 20:19:10 -0600 Subject: [PATCH] Update MediaGarbageCollector --- .../Commands/MediaGarbageCollector.php | 96 +++++++++---------- 1 file changed, 45 insertions(+), 51 deletions(-) diff --git a/app/Console/Commands/MediaGarbageCollector.php b/app/Console/Commands/MediaGarbageCollector.php index 886b1e4ad..11d330ee8 100644 --- a/app/Console/Commands/MediaGarbageCollector.php +++ b/app/Console/Commands/MediaGarbageCollector.php @@ -5,62 +5,56 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use App\{Media, Status}; use Carbon\Carbon; +use App\Services\MediaStorageService; class MediaGarbageCollector extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'media:gc'; + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'media:gc'; - /** - * The console command description. - * - * @var string - */ - protected $description = 'Delete media uploads not attached to any active statuses'; + /** + * The console command description. + * + * @var string + */ + protected $description = 'Delete media uploads not attached to any active statuses'; - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() - { - parent::__construct(); - } + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $limit = 20000; - - $gc = Media::doesntHave('status') - ->where('created_at', '<', Carbon::now()->subHours(1)->toDateTimeString()) - ->orderBy('created_at','asc') - ->take($limit) - ->get(); + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $limit = 20000; - $bar = $this->output->createProgressBar($gc->count()); - $bar->start(); - foreach($gc as $media) { - $path = storage_path("app/$media->media_path"); - $thumb = storage_path("app/$media->thumbnail_path"); - if(is_file($path)) { - unlink($path); - } - if(is_file($thumb)) { - unlink($thumb); - } - $media->forceDelete(); - $bar->advance(); - } - $bar->finish(); - } + $gc = Media::whereNull('status_id') + ->where('created_at', '<', Carbon::now()->subHours(12)->toDateTimeString()) + ->orderBy('created_at','asc') + ->take($limit) + ->get(); + + $bar = $this->output->createProgressBar($gc->count()); + $bar->start(); + foreach($gc as $media) { + MediaStorageService::delete($media); + $media->forceDelete(); + $bar->advance(); + } + $bar->finish(); + } }