Path: blob/1.0-develop/app/Http/Controllers/Api/Remote/Backups/BackupStatusController.php
14056 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Remote\Backups;34use Carbon\CarbonImmutable;5use Illuminate\Http\Request;6use Pterodactyl\Models\Backup;7use Illuminate\Http\JsonResponse;8use Pterodactyl\Facades\Activity;9use Pterodactyl\Exceptions\DisplayException;10use Pterodactyl\Http\Controllers\Controller;11use Pterodactyl\Extensions\Backups\BackupManager;12use Pterodactyl\Extensions\Filesystem\S3Filesystem;13use Pterodactyl\Exceptions\Http\HttpForbiddenException;14use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;15use Pterodactyl\Http\Requests\Api\Remote\ReportBackupCompleteRequest;1617class BackupStatusController extends Controller18{19/**20* BackupStatusController constructor.21*/22public function __construct(private BackupManager $backupManager)23{24}2526/**27* Handles updating the state of a backup.28*29* @throws \Throwable30*/31public function index(ReportBackupCompleteRequest $request, string $backup): JsonResponse32{33// Get the node associated with the request.34/** @var \Pterodactyl\Models\Node $node */35$node = $request->attributes->get('node');3637/** @var Backup $model */38$model = Backup::query()39->where('uuid', $backup)40->firstOrFail();4142// Check that the backup is "owned" by the node making the request. This avoids other nodes43// from messing with backups that they don't own.44/** @var \Pterodactyl\Models\Server $server */45$server = $model->server;46if ($server->node_id !== $node->id) {47throw new HttpForbiddenException('Requesting node does not have permission to access this server.');48}4950if ($model->is_successful) {51throw new BadRequestHttpException('Cannot update the status of a backup that is already marked as completed.');52}5354$action = $request->boolean('successful') ? 'server:backup.complete' : 'server:backup.fail';55$log = Activity::event($action)->subject($model, $model->server)->property('name', $model->name);5657$log->transaction(function () use ($model, $request) {58$successful = $request->boolean('successful');5960$model->fill([61'is_successful' => $successful,62// Change the lock state to unlocked if this was a failed backup so that it can be63// deleted easily. Also does not make sense to have a locked backup on the system64// that is failed.65'is_locked' => $successful ? $model->is_locked : false,66'checksum' => $successful ? ($request->input('checksum_type') . ':' . $request->input('checksum')) : null,67'bytes' => $successful ? $request->input('size') : 0,68'completed_at' => CarbonImmutable::now(),69])->save();7071// Check if we are using the s3 backup adapter. If so, make sure we mark the backup as72// being completed in S3 correctly.73$adapter = $this->backupManager->adapter();74if ($adapter instanceof S3Filesystem) {75$this->completeMultipartUpload($model, $adapter, $successful, $request->input('parts'));76}77});7879return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);80}8182/**83* Handles toggling the restoration status of a server. The server status field should be84* set back to null, even if the restoration failed. This is not an unsolvable state for85* the server, and the user can keep trying to restore, or just use the reinstall button.86*87* The only thing the successful field does is update the entry value for the audit logs88* table tracking for this restoration.89*90* @throws \Throwable91*/92public function restore(Request $request, string $backup): JsonResponse93{94/** @var Backup $model */95$model = Backup::query()->where('uuid', $backup)->firstOrFail();9697$node = $request->attributes->get('node');98if (! $model->server->node->is($node)) {99throw new HttpForbiddenException('Requesting node does not have permission to access this server.');100}101102$model->server->update(['status' => null]);103104Activity::event($request->boolean('successful') ? 'server:backup.restore-complete' : 'server.backup.restore-failed')105->subject($model, $model->server)106->property('name', $model->name)107->log();108109return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);110}111112/**113* Marks a multipart upload in a given S3-compatible instance as failed or successful for114* the given backup.115*116* @throws \Exception117* @throws DisplayException118*/119protected function completeMultipartUpload(Backup $backup, S3Filesystem $adapter, bool $successful, ?array $parts): void120{121// This should never really happen, but if it does don't let us fall victim to Amazon's122// wildly fun error messaging. Just stop the process right here.123if (empty($backup->upload_id)) {124// A failed backup doesn't need to error here, this can happen if the backup encounters125// an error before we even start the upload. AWS gives you tooling to clear these failed126// multipart uploads as needed too.127if (!$successful) {128return;129}130131throw new DisplayException('Cannot complete backup request: no upload_id present on model.');132}133134$params = [135'Bucket' => $adapter->getBucket(),136'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid),137'UploadId' => $backup->upload_id,138];139140$client = $adapter->getClient();141if (!$successful) {142$client->execute($client->getCommand('AbortMultipartUpload', $params));143144return;145}146147// Otherwise send a CompleteMultipartUpload request.148$params['MultipartUpload'] = [149'Parts' => [],150];151152if (is_null($parts)) {153$params['MultipartUpload']['Parts'] = $client->execute($client->getCommand('ListParts', $params))['Parts'];154} else {155foreach ($parts as $part) {156$params['MultipartUpload']['Parts'][] = [157'ETag' => $part['etag'],158'PartNumber' => $part['part_number'],159];160}161}162163$client->execute($client->getCommand('CompleteMultipartUpload', $params));164}165}166167168