Path: blob/1.0-develop/app/Services/Backups/DeleteBackupService.php
10260 views
<?php12namespace Pterodactyl\Services\Backups;34use Illuminate\Http\Response;5use Pterodactyl\Models\Backup;6use GuzzleHttp\Exception\ClientException;7use Illuminate\Database\ConnectionInterface;8use Pterodactyl\Extensions\Backups\BackupManager;9use Pterodactyl\Repositories\Wings\DaemonBackupRepository;10use Pterodactyl\Exceptions\Service\Backup\BackupLockedException;11use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;1213class DeleteBackupService14{15public function __construct(16private ConnectionInterface $connection,17private BackupManager $manager,18private DaemonBackupRepository $daemonBackupRepository,19) {20}2122/**23* Deletes a backup from the system. If the backup is stored in S3 a request24* will be made to delete that backup from the disk as well.25*26* @throws \Throwable27*/28public function handle(Backup $backup): void29{30// If the backup is marked as failed it can still be deleted, even if locked31// since the UI doesn't allow you to unlock a failed backup in the first place.32//33// I also don't really see any reason you'd have a locked, failed backup to keep34// around. The logic that updates the backup to the failed state will also remove35// the lock, so this condition should really never happen.36if ($backup->is_locked && ($backup->is_successful && !is_null($backup->completed_at))) {37throw new BackupLockedException();38}3940if ($backup->disk === Backup::ADAPTER_AWS_S3) {41$this->deleteFromS3($backup);4243return;44}4546$this->connection->transaction(function () use ($backup) {47try {48$this->daemonBackupRepository->setServer($backup->server)->delete($backup);49} catch (DaemonConnectionException $exception) {50$previous = $exception->getPrevious();51// Don't fail the request if the Daemon responds with a 404, just assume the backup52// doesn't actually exist and remove its reference from the Panel as well.53if (!$previous instanceof ClientException || $previous->getResponse()->getStatusCode() !== Response::HTTP_NOT_FOUND) {54throw $exception;55}56}5758$backup->delete();59});60}6162/**63* Deletes a backup from an S3 disk.64*65* @throws \Throwable66*/67protected function deleteFromS3(Backup $backup): void68{69$this->connection->transaction(function () use ($backup) {70$backup->delete();7172/** @var \Pterodactyl\Extensions\Filesystem\S3Filesystem $adapter */73$adapter = $this->manager->adapter(Backup::ADAPTER_AWS_S3);7475// @phpstan-ignore-next-line method.notFound76$adapter->getClient()->deleteObject([77'Bucket' => $adapter->getBucket(),78'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid),79]);80});81}82}838485