|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Http\Controllers\Api\Remote\Backups; |
| 4 | + |
| 5 | +use Carbon\CarbonImmutable; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Pterodactyl\Models\Backup; |
| 8 | +use Illuminate\Http\JsonResponse; |
| 9 | +use League\Flysystem\AwsS3v3\AwsS3Adapter; |
| 10 | +use Pterodactyl\Http\Controllers\Controller; |
| 11 | +use Pterodactyl\Extensions\Backups\BackupManager; |
| 12 | +use Pterodactyl\Repositories\Eloquent\BackupRepository; |
| 13 | +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
| 14 | + |
| 15 | +class BackupRemoteUploadController extends Controller |
| 16 | +{ |
| 17 | + const PART_SIZE = 5 * 1024 * 1024 * 1024; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var \Pterodactyl\Repositories\Eloquent\BackupRepository |
| 21 | + */ |
| 22 | + private $repository; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \Pterodactyl\Extensions\Backups\BackupManager |
| 26 | + */ |
| 27 | + private $backupManager; |
| 28 | + |
| 29 | + /** |
| 30 | + * BackupRemoteUploadController constructor. |
| 31 | + * |
| 32 | + * @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository |
| 33 | + * @param \Pterodactyl\Extensions\Backups\BackupManager $backupManager |
| 34 | + */ |
| 35 | + public function __construct(BackupRepository $repository, BackupManager $backupManager) |
| 36 | + { |
| 37 | + $this->repository = $repository; |
| 38 | + $this->backupManager = $backupManager; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns the required presigned urls to upload a backup to S3 cloud storage. |
| 43 | + * |
| 44 | + * @param \Illuminate\Http\Request $request |
| 45 | + * @param string $backup |
| 46 | + * |
| 47 | + * @return \Illuminate\Http\JsonResponse |
| 48 | + * |
| 49 | + * @throws \Exception |
| 50 | + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
| 51 | + */ |
| 52 | + public function __invoke(Request $request, string $backup) |
| 53 | + { |
| 54 | + // Get the size query parameter. |
| 55 | + $size = $request->query('size', null); |
| 56 | + if (is_null($size)) { |
| 57 | + throw new BadRequestHttpException('Missing size query parameter.'); |
| 58 | + } |
| 59 | + |
| 60 | + /** @var \Pterodactyl\Models\Backup $model */ |
| 61 | + $model = Backup::query()->where([[ 'uuid', '=', $backup ]])->firstOrFail(); |
| 62 | + |
| 63 | + // Prevent backups that have already been completed from trying to |
| 64 | + // be uploaded again. |
| 65 | + if (! is_null($model->completed_at)) { |
| 66 | + return new JsonResponse([], JsonResponse::HTTP_CONFLICT); |
| 67 | + } |
| 68 | + |
| 69 | + // Ensure we are using the S3 adapter. |
| 70 | + $adapter = $this->backupManager->adapter(); |
| 71 | + if (! $adapter instanceof AwsS3Adapter) { |
| 72 | + throw new BadRequestHttpException('Backups are not using the s3 storage driver'); |
| 73 | + } |
| 74 | + |
| 75 | + // The path where backup will be uploaded to |
| 76 | + $path = sprintf('%s/%s.tar.gz', $model->server->uuid, $model->uuid); |
| 77 | + |
| 78 | + // Get the S3 client |
| 79 | + $client = $adapter->getClient(); |
| 80 | + |
| 81 | + // Params for generating the presigned urls |
| 82 | + $params = [ |
| 83 | + 'Bucket' => $adapter->getBucket(), |
| 84 | + 'Key' => $path, |
| 85 | + 'ContentType' => 'application/x-gzip', |
| 86 | + ]; |
| 87 | + |
| 88 | + // Execute the CreateMultipartUpload request |
| 89 | + $result = $client->execute($client->getCommand('CreateMultipartUpload', $params)); |
| 90 | + |
| 91 | + // Get the UploadId from the CreateMultipartUpload request, |
| 92 | + // this is needed to create the other presigned urls |
| 93 | + $uploadId = $result->get('UploadId'); |
| 94 | + |
| 95 | + // Create a CompleteMultipartUpload presigned url |
| 96 | + $completeMultipartUpload = $client->createPresignedRequest( |
| 97 | + $client->getCommand( |
| 98 | + 'CompleteMultipartUpload', |
| 99 | + array_merge($params, [ |
| 100 | + 'UploadId' => $uploadId, |
| 101 | + ]) |
| 102 | + ), |
| 103 | + CarbonImmutable::now()->addMinutes(30) |
| 104 | + ); |
| 105 | + |
| 106 | + // Create a AbortMultipartUpload presigned url |
| 107 | + $abortMultipartUpload = $client->createPresignedRequest( |
| 108 | + $client->getCommand( |
| 109 | + 'AbortMultipartUpload', |
| 110 | + array_merge($params, [ |
| 111 | + 'UploadId' => $uploadId, |
| 112 | + ]) |
| 113 | + ), |
| 114 | + CarbonImmutable::now()->addMinutes(45) |
| 115 | + ); |
| 116 | + |
| 117 | + // Calculate the number of parts needed to upload the backup |
| 118 | + $partCount = (int) $size / (self::PART_SIZE); |
| 119 | + |
| 120 | + // Create as many UploadPart presigned urls as needed |
| 121 | + $parts = []; |
| 122 | + for ($i = 0; $i < $partCount; $i++) { |
| 123 | + $part = $client->createPresignedRequest( |
| 124 | + $client->getCommand( |
| 125 | + 'UploadPart', |
| 126 | + array_merge($params, [ |
| 127 | + 'UploadId' => $uploadId, |
| 128 | + 'PartNumber' => $i + 1, |
| 129 | + ]) |
| 130 | + ), |
| 131 | + CarbonImmutable::now()->addMinutes(30) |
| 132 | + ); |
| 133 | + |
| 134 | + array_push($parts, $part->getUri()->__toString()); |
| 135 | + } |
| 136 | + |
| 137 | + return new JsonResponse([ |
| 138 | + 'complete_multipart_upload' => $completeMultipartUpload->getUri()->__toString(), |
| 139 | + 'abort_multipart_upload' => $abortMultipartUpload->getUri()->__toString(), |
| 140 | + 'parts' => $parts, |
| 141 | + 'part_size' => self::PART_SIZE, |
| 142 | + ]); |
| 143 | + } |
| 144 | +} |
0 commit comments