Block creation of backups if it would put the server over it's limit
This commit is contained in:
parent
f1c3762f4d
commit
446dc8b33d
2 changed files with 28 additions and 0 deletions
20
app/Exceptions/Service/Backup/TooManyBackupsException.php
Normal file
20
app/Exceptions/Service/Backup/TooManyBackupsException.php
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pterodactyl\Exceptions\Service\Backup;
|
||||||
|
|
||||||
|
use Pterodactyl\Exceptions\DisplayException;
|
||||||
|
|
||||||
|
class TooManyBackupsException extends DisplayException
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* TooManyBackupsException constructor.
|
||||||
|
*
|
||||||
|
* @param int $backupLimit
|
||||||
|
*/
|
||||||
|
public function __construct(int $backupLimit)
|
||||||
|
{
|
||||||
|
parent::__construct(
|
||||||
|
sprintf('Cannot create a new backup, this server has reached its limit of %d backups.', $backupLimit)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ use Pterodactyl\Models\Server;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
||||||
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
|
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
|
||||||
|
use Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException;
|
||||||
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
||||||
|
|
||||||
class InitiateBackupService
|
class InitiateBackupService
|
||||||
|
@ -84,9 +85,16 @@ class InitiateBackupService
|
||||||
* @return \Pterodactyl\Models\Backup
|
* @return \Pterodactyl\Models\Backup
|
||||||
*
|
*
|
||||||
* @throws \Throwable
|
* @throws \Throwable
|
||||||
|
* @throws \Pterodactyl\Exceptions\Service\Backup\TooManyBackupsException
|
||||||
|
* @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
|
||||||
*/
|
*/
|
||||||
public function handle(Server $server, string $name = null): Backup
|
public function handle(Server $server, string $name = null): Backup
|
||||||
{
|
{
|
||||||
|
// Do not allow the user to continue if this server is already at its limit.
|
||||||
|
if (! $server->backup_limit || $server->backups()->count() >= $server->backup_limit) {
|
||||||
|
throw new TooManyBackupsException($server->backup_limit);
|
||||||
|
}
|
||||||
|
|
||||||
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, 10);
|
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, 10);
|
||||||
if ($previous->count() >= 2) {
|
if ($previous->count() >= 2) {
|
||||||
throw new TooManyRequestsHttpException(
|
throw new TooManyRequestsHttpException(
|
||||||
|
|
Loading…
Reference in a new issue