misc_pterodactyl-panel/app/Console/Commands/Maintenance/CleanServiceBackupFilesCommand.php

55 lines
1.4 KiB
PHP
Raw Normal View History

<?php
2016-12-14 21:56:25 +00:00
namespace Pterodactyl\Console\Commands\Maintenance;
2017-12-17 19:07:38 +00:00
use SplFileInfo;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
class CleanServiceBackupFilesCommand extends Command
{
2021-01-23 20:33:34 +00:00
public const BACKUP_THRESHOLD_MINUTES = 5;
/**
* @var string
*/
protected $description = 'Clean orphaned .bak files created when modifying services.';
/**
* @var \Illuminate\Contracts\Filesystem\Filesystem
*/
protected $disk;
/**
* @var string
*/
protected $signature = 'p:maintenance:clean-service-backups';
/**
* CleanServiceBackupFilesCommand constructor.
*/
2017-12-17 19:07:38 +00:00
public function __construct(FilesystemFactory $filesystem)
{
parent::__construct();
$this->disk = $filesystem->disk();
}
/**
* Handle command execution.
*/
public function handle()
{
$files = $this->disk->files('services/.bak');
2017-12-17 19:07:38 +00:00
collect($files)->each(function (SplFileInfo $file) {
$lastModified = Carbon::createFromTimestamp($this->disk->lastModified($file->getPath()));
if ($lastModified->diffInMinutes(Carbon::now()) > self::BACKUP_THRESHOLD_MINUTES) {
$this->disk->delete($file->getPath());
$this->info(trans('command/messages.maintenance.deleting_service_backup', ['file' => $file->getFilename()]));
}
});
}
}