2020-10-31 23:44:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Backups;
|
|
|
|
|
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
use Illuminate\Http\Request;
|
2020-11-01 17:11:45 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2020-10-31 23:44:20 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use League\Flysystem\AwsS3v3\AwsS3Adapter;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Pterodactyl\Extensions\Backups\BackupManager;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
2020-11-01 17:11:45 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2020-10-31 23:44:20 +00:00
|
|
|
|
|
|
|
class BackupRemoteUploadController extends Controller
|
|
|
|
{
|
|
|
|
const PART_SIZE = 5 * 1024 * 1024 * 1024;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Extensions\Backups\BackupManager
|
|
|
|
*/
|
|
|
|
private $backupManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BackupRemoteUploadController constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
|
|
|
|
* @param \Pterodactyl\Extensions\Backups\BackupManager $backupManager
|
|
|
|
*/
|
|
|
|
public function __construct(BackupRepository $repository, BackupManager $backupManager)
|
|
|
|
{
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->backupManager = $backupManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-01 17:11:45 +00:00
|
|
|
* Returns the required presigned urls to upload a backup to S3 cloud storage.
|
2020-10-31 23:44:20 +00:00
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string $backup
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
2020-11-01 17:11:45 +00:00
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
2020-10-31 23:44:20 +00:00
|
|
|
*/
|
|
|
|
public function __invoke(Request $request, string $backup)
|
|
|
|
{
|
2020-11-01 17:11:45 +00:00
|
|
|
// Get the size query parameter.
|
2020-10-31 23:44:20 +00:00
|
|
|
$size = $request->query('size', null);
|
2020-11-01 17:11:45 +00:00
|
|
|
if (is_null($size)) {
|
|
|
|
throw new BadRequestHttpException('Missing size query parameter.');
|
2020-10-31 23:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Backup $model */
|
2020-11-01 17:11:45 +00:00
|
|
|
$model = Backup::query()->where([[ 'uuid', '=', $backup ]])->firstOrFail();
|
2020-10-31 23:44:20 +00:00
|
|
|
|
|
|
|
// Prevent backups that have already been completed from trying to
|
|
|
|
// be uploaded again.
|
|
|
|
if (! is_null($model->completed_at)) {
|
|
|
|
return new JsonResponse([], JsonResponse::HTTP_CONFLICT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we are using the S3 adapter.
|
|
|
|
$adapter = $this->backupManager->adapter();
|
|
|
|
if (! $adapter instanceof AwsS3Adapter) {
|
2020-11-01 17:11:45 +00:00
|
|
|
throw new BadRequestHttpException('Backups are not using the s3 storage driver');
|
2020-10-31 23:44:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 17:11:45 +00:00
|
|
|
// The path where backup will be uploaded to
|
2020-10-31 23:44:20 +00:00
|
|
|
$path = sprintf('%s/%s.tar.gz', $model->server->uuid, $model->uuid);
|
|
|
|
|
2020-11-01 17:11:45 +00:00
|
|
|
// Get the S3 client
|
2020-10-31 23:44:20 +00:00
|
|
|
$client = $adapter->getClient();
|
|
|
|
|
2020-11-01 17:11:45 +00:00
|
|
|
// Params for generating the presigned urls
|
|
|
|
$params = [
|
2020-10-31 23:44:20 +00:00
|
|
|
'Bucket' => $adapter->getBucket(),
|
|
|
|
'Key' => $path,
|
|
|
|
'ContentType' => 'application/x-gzip',
|
2020-11-01 17:11:45 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// 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
|
2020-10-31 23:44:20 +00:00
|
|
|
$uploadId = $result->get('UploadId');
|
|
|
|
|
2020-11-01 17:11:45 +00:00
|
|
|
// Create a CompleteMultipartUpload presigned url
|
2020-10-31 23:44:20 +00:00
|
|
|
$completeMultipartUpload = $client->createPresignedRequest(
|
2020-11-01 17:11:45 +00:00
|
|
|
$client->getCommand(
|
|
|
|
'CompleteMultipartUpload',
|
|
|
|
array_merge($params, [
|
|
|
|
'UploadId' => $uploadId,
|
|
|
|
])
|
|
|
|
),
|
2020-10-31 23:44:20 +00:00
|
|
|
CarbonImmutable::now()->addMinutes(30)
|
|
|
|
);
|
|
|
|
|
2020-11-01 17:11:45 +00:00
|
|
|
// Create a AbortMultipartUpload presigned url
|
2020-10-31 23:44:20 +00:00
|
|
|
$abortMultipartUpload = $client->createPresignedRequest(
|
2020-11-01 17:11:45 +00:00
|
|
|
$client->getCommand(
|
|
|
|
'AbortMultipartUpload',
|
|
|
|
array_merge($params, [
|
|
|
|
'UploadId' => $uploadId,
|
|
|
|
])
|
|
|
|
),
|
2020-10-31 23:44:20 +00:00
|
|
|
CarbonImmutable::now()->addMinutes(45)
|
|
|
|
);
|
|
|
|
|
2020-11-01 17:11:45 +00:00
|
|
|
// Calculate the number of parts needed to upload the backup
|
2020-10-31 23:44:20 +00:00
|
|
|
$partCount = (int) $size / (self::PART_SIZE);
|
|
|
|
|
2020-11-01 17:11:45 +00:00
|
|
|
// Create as many UploadPart presigned urls as needed
|
2020-10-31 23:44:20 +00:00
|
|
|
$parts = [];
|
|
|
|
for ($i = 0; $i < $partCount; $i++) {
|
|
|
|
$part = $client->createPresignedRequest(
|
2020-11-01 17:11:45 +00:00
|
|
|
$client->getCommand(
|
|
|
|
'UploadPart',
|
|
|
|
array_merge($params, [
|
|
|
|
'UploadId' => $uploadId,
|
|
|
|
'PartNumber' => $i + 1,
|
|
|
|
])
|
|
|
|
),
|
2020-10-31 23:44:20 +00:00
|
|
|
CarbonImmutable::now()->addMinutes(30)
|
|
|
|
);
|
|
|
|
|
|
|
|
array_push($parts, $part->getUri()->__toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new JsonResponse([
|
2020-11-01 17:11:45 +00:00
|
|
|
'complete_multipart_upload' => $completeMultipartUpload->getUri()->__toString(),
|
|
|
|
'abort_multipart_upload' => $abortMultipartUpload->getUri()->__toString(),
|
|
|
|
'parts' => $parts,
|
|
|
|
'part_size' => self::PART_SIZE,
|
|
|
|
]);
|
2020-10-31 23:44:20 +00:00
|
|
|
}
|
|
|
|
}
|