Tweaks to BackupRemoteUploadController.php
This commit is contained in:
parent
63ac81586d
commit
6af848ccea
1 changed files with 48 additions and 32 deletions
|
@ -4,11 +4,13 @@ namespace Pterodactyl\Http\Controllers\Api\Remote\Backups;
|
||||||
|
|
||||||
use Carbon\CarbonImmutable;
|
use Carbon\CarbonImmutable;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Pterodactyl\Models\Backup;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use League\Flysystem\AwsS3v3\AwsS3Adapter;
|
use League\Flysystem\AwsS3v3\AwsS3Adapter;
|
||||||
use Pterodactyl\Http\Controllers\Controller;
|
use Pterodactyl\Http\Controllers\Controller;
|
||||||
use Pterodactyl\Extensions\Backups\BackupManager;
|
use Pterodactyl\Extensions\Backups\BackupManager;
|
||||||
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
|
|
||||||
class BackupRemoteUploadController extends Controller
|
class BackupRemoteUploadController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -37,25 +39,26 @@ class BackupRemoteUploadController extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ?
|
* Returns the required presigned urls to upload a backup to S3 cloud storage.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
* @param \Illuminate\Http\Request $request
|
||||||
* @param string $backup
|
* @param string $backup
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\JsonResponse
|
* @return \Illuminate\Http\JsonResponse
|
||||||
*
|
*
|
||||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||||
*/
|
*/
|
||||||
public function __invoke(Request $request, string $backup)
|
public function __invoke(Request $request, string $backup)
|
||||||
{
|
{
|
||||||
|
// Get the size query parameter.
|
||||||
$size = $request->query('size', null);
|
$size = $request->query('size', null);
|
||||||
if ($size === null) {
|
if (is_null($size)) {
|
||||||
return new JsonResponse([], JsonResponse::HTTP_BAD_REQUEST);
|
throw new BadRequestHttpException('Missing size query parameter.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var \Pterodactyl\Models\Backup $model */
|
/** @var \Pterodactyl\Models\Backup $model */
|
||||||
$model = $this->repository->findFirstWhere([[ 'uuid', '=', $backup ]]);
|
$model = 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.
|
||||||
|
@ -66,52 +69,65 @@ class BackupRemoteUploadController extends Controller
|
||||||
// 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) {
|
||||||
return new JsonResponse([], JsonResponse::HTTP);
|
throw new BadRequestHttpException('Backups are not using the s3 storage driver');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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', $model->server->uuid, $model->uuid);
|
||||||
|
|
||||||
|
// Get the S3 client
|
||||||
$client = $adapter->getClient();
|
$client = $adapter->getClient();
|
||||||
|
|
||||||
$result = $client->execute($client->getCommand('CreateMultipartUpload', [
|
// Params for generating the presigned urls
|
||||||
|
$params = [
|
||||||
'Bucket' => $adapter->getBucket(),
|
'Bucket' => $adapter->getBucket(),
|
||||||
'Key' => $path,
|
'Key' => $path,
|
||||||
'ContentType' => 'application/x-gzip',
|
'ContentType' => 'application/x-gzip',
|
||||||
]));
|
];
|
||||||
|
|
||||||
|
// Execute the CreateMultipartUpload request
|
||||||
|
$result = $client->execute($client->getCommand('CreateMultipartUpload', $params));
|
||||||
|
|
||||||
|
// Get the UploadId from the CreateMultipartUpload request,
|
||||||
|
// this is needed to create the other presigned urls
|
||||||
$uploadId = $result->get('UploadId');
|
$uploadId = $result->get('UploadId');
|
||||||
|
|
||||||
|
// Create a CompleteMultipartUpload presigned url
|
||||||
$completeMultipartUpload = $client->createPresignedRequest(
|
$completeMultipartUpload = $client->createPresignedRequest(
|
||||||
$client->getCommand('CompleteMultipartUpload', [
|
$client->getCommand(
|
||||||
'Bucket' => $adapter->getBucket(),
|
'CompleteMultipartUpload',
|
||||||
'Key' => $path,
|
array_merge($params, [
|
||||||
'ContentType' => 'application/x-gzip',
|
|
||||||
'UploadId' => $uploadId,
|
'UploadId' => $uploadId,
|
||||||
]),
|
])
|
||||||
|
),
|
||||||
CarbonImmutable::now()->addMinutes(30)
|
CarbonImmutable::now()->addMinutes(30)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Create a AbortMultipartUpload presigned url
|
||||||
$abortMultipartUpload = $client->createPresignedRequest(
|
$abortMultipartUpload = $client->createPresignedRequest(
|
||||||
$client->getCommand('AbortMultipartUpload', [
|
$client->getCommand(
|
||||||
'Bucket' => $adapter->getBucket(),
|
'AbortMultipartUpload',
|
||||||
'Key' => $path,
|
array_merge($params, [
|
||||||
'ContentType' => 'application/x-gzip',
|
|
||||||
'UploadId' => $uploadId,
|
'UploadId' => $uploadId,
|
||||||
]),
|
])
|
||||||
|
),
|
||||||
CarbonImmutable::now()->addMinutes(45)
|
CarbonImmutable::now()->addMinutes(45)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Calculate the number of parts needed to upload the backup
|
||||||
$partCount = (int) $size / (self::PART_SIZE);
|
$partCount = (int) $size / (self::PART_SIZE);
|
||||||
|
|
||||||
|
// Create as many UploadPart presigned urls as needed
|
||||||
$parts = [];
|
$parts = [];
|
||||||
for ($i = 0; $i < $partCount; $i++) {
|
for ($i = 0; $i < $partCount; $i++) {
|
||||||
$part = $client->createPresignedRequest(
|
$part = $client->createPresignedRequest(
|
||||||
$client->getCommand('UploadPart', [
|
$client->getCommand(
|
||||||
'Bucket' => $adapter->getBucket(),
|
'UploadPart',
|
||||||
'Key' => $path,
|
array_merge($params, [
|
||||||
'ContentType' => 'application/x-gzip',
|
|
||||||
'UploadId' => $uploadId,
|
'UploadId' => $uploadId,
|
||||||
'PartNumber' => $i + 1,
|
'PartNumber' => $i + 1,
|
||||||
]),
|
])
|
||||||
|
),
|
||||||
CarbonImmutable::now()->addMinutes(30)
|
CarbonImmutable::now()->addMinutes(30)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -119,10 +135,10 @@ class BackupRemoteUploadController extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'CompleteMultipartUpload' => $completeMultipartUpload->getUri()->__toString(),
|
'complete_multipart_upload' => $completeMultipartUpload->getUri()->__toString(),
|
||||||
'AbortMultipartUpload' => $abortMultipartUpload->getUri()->__toString(),
|
'abort_multipart_upload' => $abortMultipartUpload->getUri()->__toString(),
|
||||||
'Parts' => $parts,
|
'parts' => $parts,
|
||||||
'PartSize' => self::PART_SIZE,
|
'part_size' => self::PART_SIZE,
|
||||||
], JsonResponse::HTTP_OK);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue