pixelfed/app/Console/Commands/MediaGarbageCollector.php

67 lines
1.5 KiB
PHP
Raw Normal View History

2018-10-21 03:48:06 +00:00
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\{Media, Status};
use Carbon\Carbon;
class MediaGarbageCollector extends Command
{
/**
* 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';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2019-08-08 06:14:16 +00:00
$limit = 20000;
$gc = Media::doesntHave('status')
->where('created_at', '<', Carbon::now()->subHours(1)->toDateTimeString())
2018-10-21 03:48:06 +00:00
->orderBy('created_at','asc')
2019-08-08 06:14:16 +00:00
->take($limit)
2018-10-21 03:48:06 +00:00
->get();
2019-08-08 06:14:16 +00:00
$bar = $this->output->createProgressBar($gc->count());
$bar->start();
2018-10-21 03:48:06 +00:00
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();
2019-08-08 06:14:16 +00:00
$bar->advance();
2018-10-21 03:48:06 +00:00
}
2019-08-08 06:14:16 +00:00
$bar->finish();
2018-10-21 03:48:06 +00:00
}
}