Code cleanup for signed URL generation endpoint

This commit is contained in:
Dane Everitt 2020-11-01 14:46:01 -08:00
parent 6eff9d6211
commit 964a1436ce
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53

View file

@ -52,31 +52,32 @@ class BackupRemoteUploadController extends Controller
public function __invoke(Request $request, string $backup) public function __invoke(Request $request, string $backup)
{ {
// Get the size query parameter. // Get the size query parameter.
$size = $request->query('size', null); $size = (int)$request->query('size');
if (is_null($size)) { if (empty($size)) {
throw new BadRequestHttpException('Missing size query parameter.'); throw new BadRequestHttpException('A non-empty "size" query parameter must be provided.');
} }
/** @var \Pterodactyl\Models\Backup $model */ /** @var \Pterodactyl\Models\Backup $backup */
$model = Backup::query()->where([[ 'uuid', '=', $backup ]])->firstOrFail(); $backup = Backup::query()->where('uuid', $backup)->firstOrFail();
// Prevent backups that have already been completed from trying to // Prevent backups that have already been completed from trying to
// be uploaded again. // be uploaded again.
if (! is_null($model->completed_at)) { if (! is_null($backup->completed_at)) {
return new JsonResponse([], JsonResponse::HTTP_CONFLICT); return new JsonResponse([], JsonResponse::HTTP_CONFLICT);
} }
// Ensure we are using the S3 adapter. // Ensure we are using the S3 adapter.
$adapter = $this->backupManager->adapter(); $adapter = $this->backupManager->adapter();
if (! $adapter instanceof AwsS3Adapter) { if (! $adapter instanceof AwsS3Adapter) {
throw new BadRequestHttpException('Backups are not using the s3 storage driver'); throw new BadRequestHttpException('The configured backup adapter is not an S3 compatiable adapter.');
} }
// The path where backup will be uploaded to // The path where backup will be uploaded to
$path = sprintf('%s/%s.tar.gz', $model->server->uuid, $model->uuid); $path = sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid);
// Get the S3 client // Get the S3 client
$client = $adapter->getClient(); $client = $adapter->getClient();
$expires = CarbonImmutable::now()->addMinutes(30);
// Params for generating the presigned urls // Params for generating the presigned urls
$params = [ $params = [
@ -88,57 +89,27 @@ class BackupRemoteUploadController extends Controller
// Execute the CreateMultipartUpload request // Execute the CreateMultipartUpload request
$result = $client->execute($client->getCommand('CreateMultipartUpload', $params)); $result = $client->execute($client->getCommand('CreateMultipartUpload', $params));
// Get the UploadId from the CreateMultipartUpload request, // Get the UploadId from the CreateMultipartUpload request, this is needed to create
// this is needed to create the other presigned urls // the other presigned urls.
$uploadId = $result->get('UploadId'); $params['UploadId'] = $result->get('UploadId');
// Create a CompleteMultipartUpload presigned url
$completeMultipartUpload = $client->createPresignedRequest(
$client->getCommand(
'CompleteMultipartUpload',
array_merge($params, [
'UploadId' => $uploadId,
])
),
CarbonImmutable::now()->addMinutes(30)
);
// Create a AbortMultipartUpload presigned url
$abortMultipartUpload = $client->createPresignedRequest(
$client->getCommand(
'AbortMultipartUpload',
array_merge($params, [
'UploadId' => $uploadId,
])
),
CarbonImmutable::now()->addMinutes(45)
);
// Calculate the number of parts needed to upload the backup
$partCount = (int) $size / (self::PART_SIZE);
// Create as many UploadPart presigned urls as needed // Create as many UploadPart presigned urls as needed
$parts = []; $parts = [];
for ($i = 0; $i < $partCount; $i++) { for ($i = 0; $i < ($size / self::PART_SIZE); $i++) {
$part = $client->createPresignedRequest( $parts[] = $client->createPresignedRequest(
$client->getCommand( $client->getCommand('UploadPart', array_merge($params, ['PartNumber' => $i + 1])), $expires
'UploadPart', )->getUri()->__toString();
array_merge($params, [
'UploadId' => $uploadId,
'PartNumber' => $i + 1,
])
),
CarbonImmutable::now()->addMinutes(30)
);
array_push($parts, $part->getUri()->__toString());
} }
return new JsonResponse([ return new JsonResponse([
'complete_multipart_upload' => $completeMultipartUpload->getUri()->__toString(),
'abort_multipart_upload' => $abortMultipartUpload->getUri()->__toString(),
'parts' => $parts, 'parts' => $parts,
'part_size' => self::PART_SIZE, 'part_size' => self::PART_SIZE,
'complete_multipart_upload' => $client->createPresignedRequest(
$client->getCommand('CompleteMultipartUpload', $params), $expires
)->getUri()->__toString(),
'abort_multipart_upload' => $client->createPresignedRequest(
$client->getCommand('AbortMultipartUpload', $params), $expires->addMinutes(15)
)->getUri()->__toString(),
]); ]);
} }
} }