|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Http\Controllers\Api\Remote\Backups; |
| 4 | + |
| 5 | +use Carbon\CarbonImmutable; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Illuminate\Http\JsonResponse; |
| 8 | +use League\Flysystem\AwsS3v3\AwsS3Adapter; |
| 9 | +use Pterodactyl\Http\Controllers\Controller; |
| 10 | +use Pterodactyl\Extensions\Backups\BackupManager; |
| 11 | +use Pterodactyl\Repositories\Eloquent\BackupRepository; |
| 12 | + |
| 13 | +class BackupRemoteUploadController extends Controller |
| 14 | +{ |
| 15 | + // I would use 1024 but I'm unsure if AWS or other S3 servers, |
| 16 | + // use SI gigabyte (base 10), or the proper IEC gibibyte (base 2). |
| 17 | + // const PART_SIZE = 5 * 1000 * 1000 * 1000; |
| 18 | + const PART_SIZE = 5 * 1024 * 1024 * 1024; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Pterodactyl\Repositories\Eloquent\BackupRepository |
| 22 | + */ |
| 23 | + private $repository; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Pterodactyl\Extensions\Backups\BackupManager |
| 27 | + */ |
| 28 | + private $backupManager; |
| 29 | + |
| 30 | + /** |
| 31 | + * BackupRemoteUploadController constructor. |
| 32 | + * |
| 33 | + * @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository |
| 34 | + * @param \Pterodactyl\Extensions\Backups\BackupManager $backupManager |
| 35 | + */ |
| 36 | + public function __construct(BackupRepository $repository, BackupManager $backupManager) |
| 37 | + { |
| 38 | + $this->repository = $repository; |
| 39 | + $this->backupManager = $backupManager; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * ? |
| 44 | + * |
| 45 | + * @param \Illuminate\Http\Request $request |
| 46 | + * @param string $backup |
| 47 | + * |
| 48 | + * @return \Illuminate\Http\JsonResponse |
| 49 | + * |
| 50 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 51 | + * @throws \Exception |
| 52 | + */ |
| 53 | + public function __invoke(Request $request, string $backup) |
| 54 | + { |
| 55 | + $size = $request->query('size', null); |
| 56 | + if ($size === null) { |
| 57 | + return new JsonResponse([], JsonResponse::HTTP_BAD_REQUEST); |
| 58 | + } |
| 59 | + |
| 60 | + /** @var \Pterodactyl\Models\Backup $model */ |
| 61 | + $model = $this->repository->findFirstWhere([[ 'uuid', '=', $backup ]]); |
| 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 | + return new JsonResponse([], JsonResponse::HTTP); |
| 73 | + } |
| 74 | + |
| 75 | + $path = sprintf('%s/%s.tar.gz', $model->server->uuid, $model->uuid); |
| 76 | + |
| 77 | + $client = $adapter->getClient(); |
| 78 | + |
| 79 | + $result = $client->execute($client->getCommand('CreateMultipartUpload', [ |
| 80 | + 'Bucket' => $adapter->getBucket(), |
| 81 | + 'Key' => $path, |
| 82 | + 'ContentType' => 'application/x-gzip', |
| 83 | + ])); |
| 84 | + $uploadId = $result->get('UploadId'); |
| 85 | + |
| 86 | + $completeMultipartUpload = $client->createPresignedRequest( |
| 87 | + $client->getCommand('CompleteMultipartUpload', [ |
| 88 | + 'Bucket' => $adapter->getBucket(), |
| 89 | + 'Key' => $path, |
| 90 | + 'ContentType' => 'application/x-gzip', |
| 91 | + 'UploadId' => $uploadId, |
| 92 | + ]), |
| 93 | + CarbonImmutable::now()->addMinutes(30) |
| 94 | + ); |
| 95 | + |
| 96 | + $abortMultipartUpload = $client->createPresignedRequest( |
| 97 | + $client->getCommand('AbortMultipartUpload', [ |
| 98 | + 'Bucket' => $adapter->getBucket(), |
| 99 | + 'Key' => $path, |
| 100 | + 'ContentType' => 'application/x-gzip', |
| 101 | + 'UploadId' => $uploadId, |
| 102 | + ]), |
| 103 | + CarbonImmutable::now()->addMinutes(45) |
| 104 | + ); |
| 105 | + |
| 106 | + $partCount = (int) $size / (self::PART_SIZE); |
| 107 | + |
| 108 | + $parts = []; |
| 109 | + for ($i = 0; $i < $partCount; $i++) { |
| 110 | + $part = $client->createPresignedRequest( |
| 111 | + $client->getCommand('UploadPart', [ |
| 112 | + 'Bucket' => $adapter->getBucket(), |
| 113 | + 'Key' => $path, |
| 114 | + 'ContentType' => 'application/x-gzip', |
| 115 | + 'UploadId' => $uploadId, |
| 116 | + 'PartNumber' => $i + 1, |
| 117 | + ]), |
| 118 | + CarbonImmutable::now()->addMinutes(30) |
| 119 | + ); |
| 120 | + |
| 121 | + array_push($parts, $part->getUri()->__toString()); |
| 122 | + } |
| 123 | + |
| 124 | + return new JsonResponse([ |
| 125 | + 'CompleteMultipartUpload' => $completeMultipartUpload->getUri()->__toString(), |
| 126 | + 'AbortMultipartUpload' => $abortMultipartUpload->getUri()->__toString(), |
| 127 | + 'Parts' => $parts, |
| 128 | + 'PartSize' => self::PART_SIZE, |
| 129 | + ], JsonResponse::HTTP_OK); |
| 130 | + } |
| 131 | +} |
0 commit comments