misc_pterodactyl-panel/app/Http/Controllers/Api/Remote/Backups/BackupRemoteUploadController.php

101 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Http\Controllers\Api\Remote\Backups;
use Carbon\CarbonImmutable;
use Illuminate\Http\Request;
use Pterodactyl\Models\Backup;
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-12-27 19:34:55 +00:00
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class BackupRemoteUploadController extends Controller
{
2021-01-23 20:33:34 +00:00
public const PART_SIZE = 5 * 1024 * 1024 * 1024;
2021-03-05 17:03:12 +00:00
private BackupRepository $repository;
private BackupManager $backupManager;
/**
* BackupRemoteUploadController constructor.
*/
2020-12-06 22:19:54 +00:00
public function __construct(BackupRepository $repository, BackupManager $backupManager)
{
$this->repository = $repository;
$this->backupManager = $backupManager;
}
/**
2021-03-05 17:03:12 +00:00
* Returns the required pre-signed urls to upload a backup to S3 cloud storage.
*
* @throws \Exception
* @throws \Throwable
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
2021-03-05 17:03:12 +00:00
public function __invoke(Request $request, string $backup): JsonResponse
{
// Get the size query parameter.
$size = (int) $request->query('size');
if (empty($size)) {
throw new BadRequestHttpException('A non-empty "size" query parameter must be provided.');
}
/** @var \Pterodactyl\Models\Backup $backup */
$backup = Backup::query()->where('uuid', $backup)->firstOrFail();
// Prevent backups that have already been completed from trying to
// be uploaded again.
2021-01-23 20:33:34 +00:00
if (!is_null($backup->completed_at)) {
2020-12-27 19:34:55 +00:00
throw new ConflictHttpException('This backup is already in a completed state.');
}
// Ensure we are using the S3 adapter.
$adapter = $this->backupManager->adapter();
2021-01-23 20:33:34 +00:00
if (!$adapter instanceof AwsS3Adapter) {
throw new BadRequestHttpException('The configured backup adapter is not an S3 compatible adapter.');
}
// The path where backup will be uploaded to
$path = sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid);
// Get the S3 client
$client = $adapter->getClient();
2020-12-06 22:19:54 +00:00
$expires = CarbonImmutable::now()->addMinutes(config('backups.presigned_url_lifespan', 60));
// Params for generating the presigned urls
$params = [
'Bucket' => $adapter->getBucket(),
'Key' => $path,
'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.
$params['UploadId'] = $result->get('UploadId');
// Create as many UploadPart presigned urls as needed
$parts = [];
2021-01-23 20:33:34 +00:00
for ($i = 0; $i < ($size / self::PART_SIZE); ++$i) {
$parts[] = $client->createPresignedRequest(
2021-01-23 20:33:34 +00:00
$client->getCommand('UploadPart', array_merge($params, ['PartNumber' => $i + 1])),
$expires
)->getUri()->__toString();
}
// Set the upload_id on the backup in the database.
2020-12-27 19:34:55 +00:00
$backup->update(['upload_id' => $params['UploadId']]);
return new JsonResponse([
'parts' => $parts,
'part_size' => self::PART_SIZE,
]);
}
}