2020-04-10 05:08:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Backups;
|
|
|
|
|
2020-05-10 02:19:45 +00:00
|
|
|
use Illuminate\Http\Response;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2020-05-10 02:19:45 +00:00
|
|
|
use GuzzleHttp\Exception\ClientException;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2020-05-10 02:43:58 +00:00
|
|
|
use Pterodactyl\Extensions\Backups\BackupManager;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
|
2020-05-10 02:19:45 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2020-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
class DeleteBackupService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonBackupRepository
|
|
|
|
*/
|
|
|
|
private $daemonBackupRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
2020-05-10 02:43:58 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Extensions\Backups\BackupManager
|
|
|
|
*/
|
|
|
|
private $manager;
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
/**
|
|
|
|
* DeleteBackupService constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ConnectionInterface $connection,
|
|
|
|
BackupRepository $repository,
|
2020-05-10 02:43:58 +00:00
|
|
|
BackupManager $manager,
|
2020-04-10 05:08:09 +00:00
|
|
|
DaemonBackupRepository $daemonBackupRepository
|
|
|
|
) {
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->daemonBackupRepository = $daemonBackupRepository;
|
|
|
|
$this->connection = $connection;
|
2020-05-10 02:43:58 +00:00
|
|
|
$this->manager = $manager;
|
2020-04-10 05:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a backup from the system.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
public function handle(Backup $backup)
|
|
|
|
{
|
2020-05-10 02:43:58 +00:00
|
|
|
if ($backup->disk === Backup::ADAPTER_AWS_S3) {
|
|
|
|
$this->deleteFromS3($backup);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
$this->connection->transaction(function () use ($backup) {
|
2020-05-10 02:19:45 +00:00
|
|
|
try {
|
|
|
|
$this->daemonBackupRepository->setServer($backup->server)->delete($backup);
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
$previous = $exception->getPrevious();
|
|
|
|
// Don't fail the request if the Daemon responds with a 404, just assume the backup
|
|
|
|
// doesn't actually exist and remove it's reference from the Panel as well.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$previous instanceof ClientException || $previous->getResponse()->getStatusCode() !== Response::HTTP_NOT_FOUND) {
|
2020-05-10 02:19:45 +00:00
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
$this->repository->delete($backup->id);
|
|
|
|
});
|
|
|
|
}
|
2020-05-10 02:43:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a backup from an S3 disk.
|
|
|
|
*
|
2020-05-10 03:01:45 +00:00
|
|
|
* @throws \Throwable
|
2020-05-10 02:43:58 +00:00
|
|
|
*/
|
|
|
|
protected function deleteFromS3(Backup $backup)
|
|
|
|
{
|
2020-05-10 03:01:45 +00:00
|
|
|
$this->connection->transaction(function () use ($backup) {
|
|
|
|
$this->repository->delete($backup->id);
|
2020-05-10 02:43:58 +00:00
|
|
|
|
2020-05-10 03:01:45 +00:00
|
|
|
/** @var \League\Flysystem\AwsS3v3\AwsS3Adapter $adapter */
|
|
|
|
$adapter = $this->manager->adapter(Backup::ADAPTER_AWS_S3);
|
|
|
|
|
|
|
|
$adapter->getClient()->deleteObject([
|
|
|
|
'Bucket' => $adapter->getBucket(),
|
|
|
|
'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid),
|
|
|
|
]);
|
|
|
|
});
|
2020-05-10 02:43:58 +00:00
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
}
|