mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-09 16:24:51 +00:00
Add Media GC
This commit is contained in:
parent
1e16e18ae6
commit
f69598eb1b
1 changed files with 60 additions and 0 deletions
60
app/Console/Commands/MediaGarbageCollector.php
Normal file
60
app/Console/Commands/MediaGarbageCollector.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?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()
|
||||
{
|
||||
$gc = Media::whereNull('status_id')
|
||||
->where('created_at', '<', Carbon::now()->subHours(6)->toDateTimeString())
|
||||
->orderBy('created_at','asc')
|
||||
->take(500)
|
||||
->get();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue