Path: blob/1.0-develop/app/Http/Controllers/Api/Remote/Backups/BackupStatusController.php
10284 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('You do not have permission to access that backup.');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$model->server->update(['status' => null]);9899Activity::event($request->boolean('successful') ? 'server:backup.restore-complete' : 'server.backup.restore-failed')100->subject($model, $model->server)101->property('name', $model->name)102->log();103104return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);105}106107/**108* Marks a multipart upload in a given S3-compatible instance as failed or successful for109* the given backup.110*111* @throws \Exception112* @throws DisplayException113*/114protected function completeMultipartUpload(Backup $backup, S3Filesystem $adapter, bool $successful, ?array $parts): void115{116// This should never really happen, but if it does don't let us fall victim to Amazon's117// wildly fun error messaging. Just stop the process right here.118if (empty($backup->upload_id)) {119// A failed backup doesn't need to error here, this can happen if the backup encounters120// an error before we even start the upload. AWS gives you tooling to clear these failed121// multipart uploads as needed too.122if (!$successful) {123return;124}125126throw new DisplayException('Cannot complete backup request: no upload_id present on model.');127}128129$params = [130'Bucket' => $adapter->getBucket(),131'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid),132'UploadId' => $backup->upload_id,133];134135$client = $adapter->getClient();136if (!$successful) {137$client->execute($client->getCommand('AbortMultipartUpload', $params));138139return;140}141142// Otherwise send a CompleteMultipartUpload request.143$params['MultipartUpload'] = [144'Parts' => [],145];146147if (is_null($parts)) {148$params['MultipartUpload']['Parts'] = $client->execute($client->getCommand('ListParts', $params))['Parts'];149} else {150foreach ($parts as $part) {151$params['MultipartUpload']['Parts'][] = [152'ETag' => $part['etag'],153'PartNumber' => $part['part_number'],154];155}156}157158$client->execute($client->getCommand('CompleteMultipartUpload', $params));159}160}161162163