2018-10-21 03:48:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use App\{Media, Status};
|
|
|
|
use Carbon\Carbon;
|
2022-03-17 02:19:10 +00:00
|
|
|
use App\Services\MediaStorageService;
|
2018-10-21 03:48:06 +00:00
|
|
|
|
|
|
|
class MediaGarbageCollector extends Command
|
|
|
|
{
|
2022-03-17 02:19:10 +00:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'media:gc';
|
2018-10-21 03:48:06 +00:00
|
|
|
|
2022-03-17 02:19:10 +00:00
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Delete media uploads not attached to any active statuses';
|
2018-10-21 03:48:06 +00:00
|
|
|
|
2022-03-17 02:19:10 +00:00
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2018-10-21 03:48:06 +00:00
|
|
|
|
2022-03-17 02:19:10 +00:00
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$limit = 20000;
|
2018-10-21 03:48:06 +00:00
|
|
|
|
2022-03-17 02:19:10 +00:00
|
|
|
$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();
|
|
|
|
}
|
2018-10-21 03:48:06 +00:00
|
|
|
}
|