Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Console/Commands/Maintenance/CleanServiceBackupFilesCommand.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Console\Commands\Maintenance;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use Illuminate\Contracts\Filesystem\Filesystem;
8
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
9
10
class CleanServiceBackupFilesCommand extends Command
11
{
12
public const BACKUP_THRESHOLD_MINUTES = 5;
13
14
protected $description = 'Clean orphaned .bak files created when modifying services.';
15
16
protected $signature = 'p:maintenance:clean-service-backups';
17
18
protected Filesystem $disk;
19
20
/**
21
* CleanServiceBackupFilesCommand constructor.
22
*/
23
public function __construct(FilesystemFactory $filesystem)
24
{
25
parent::__construct();
26
27
$this->disk = $filesystem->disk();
28
}
29
30
/**
31
* Handle command execution.
32
*/
33
public function handle()
34
{
35
$files = $this->disk->files('services/.bak');
36
37
collect($files)->each(function (\SplFileInfo $file) {
38
$lastModified = Carbon::createFromTimestamp($this->disk->lastModified($file->getPath()));
39
if ((int) $lastModified->diffInMinutes(Carbon::now()) > self::BACKUP_THRESHOLD_MINUTES) {
40
$this->disk->delete($file->getPath());
41
$this->info(trans('command/messages.maintenance.deleting_service_backup', ['file' => $file->getFilename()]));
42
}
43
});
44
}
45
}
46
47