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-12-27 19:34:55 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
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
|
|
|
|
{
|
2022-09-25 20:38:49 +00:00
|
|
|
public const DEFAULT_MAX_PART_SIZE = 5 * 1024 * 1024 * 1024;
|
2020-10-31 23:44:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Extensions\Backups\BackupManager
|
|
|
|
*/
|
|
|
|
private $backupManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BackupRemoteUploadController constructor.
|
|
|
|
*/
|
2020-12-06 22:19:54 +00:00
|
|
|
public function __construct(BackupRepository $repository, BackupManager $backupManager)
|
2020-10-31 23:44:20 +00:00
|
|
|
{
|
|
|
|
$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
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
2020-12-26 18:59:21 +00:00
|
|
|
* @throws \Throwable
|
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-12-16 21:15:07 +00:00
|
|
|
$size = (int) $request->query('size');
|
2020-11-01 22:46:01 +00:00
|
|
|
if (empty($size)) {
|
|
|
|
throw new BadRequestHttpException('A non-empty "size" query parameter must be provided.');
|
2020-10-31 23:44:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 22:46:01 +00:00
|
|
|
/** @var \Pterodactyl\Models\Backup $backup */
|
|
|
|
$backup = 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.
|
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.');
|
2020-10-31 23:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we are using the S3 adapter.
|
|
|
|
$adapter = $this->backupManager->adapter();
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$adapter instanceof AwsS3Adapter) {
|
2020-12-06 20:53:55 +00:00
|
|
|
throw new BadRequestHttpException('The configured backup adapter is not an S3 compatible adapter.');
|
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-11-01 22:46:01 +00:00
|
|
|
$path = sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid);
|
2020-10-31 23:44:20 +00:00
|
|
|
|
2020-11-01 17:11:45 +00:00
|
|
|
// Get the S3 client
|
2020-10-31 23:44:20 +00:00
|
|
|
$client = $adapter->getClient();
|
2020-12-06 22:19:54 +00:00
|
|
|
$expires = CarbonImmutable::now()->addMinutes(config('backups.presigned_url_lifespan', 60));
|
2020-10-31 23:44:20 +00:00
|
|
|
|
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));
|
|
|
|
|
2020-11-01 22:46:01 +00:00
|
|
|
// Get the UploadId from the CreateMultipartUpload request, this is needed to create
|
|
|
|
// the other presigned urls.
|
|
|
|
$params['UploadId'] = $result->get('UploadId');
|
2020-10-31 23:44:20 +00:00
|
|
|
|
2022-09-25 20:38:49 +00:00
|
|
|
// Retrieve configured part size
|
|
|
|
$maxPartSize = $this->getConfiguredMaxPartSize();
|
|
|
|
|
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 = [];
|
2022-09-25 20:38:49 +00:00
|
|
|
for ($i = 0; $i < ($size / $maxPartSize); ++$i) {
|
2020-11-01 22:46:01 +00:00
|
|
|
$parts[] = $client->createPresignedRequest(
|
2021-01-23 20:33:34 +00:00
|
|
|
$client->getCommand('UploadPart', array_merge($params, ['PartNumber' => $i + 1])),
|
|
|
|
$expires
|
2020-11-01 22:46:01 +00:00
|
|
|
)->getUri()->__toString();
|
2020-10-31 23:44:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 18:59:21 +00:00
|
|
|
// Set the upload_id on the backup in the database.
|
2020-12-27 19:34:55 +00:00
|
|
|
$backup->update(['upload_id' => $params['UploadId']]);
|
2020-12-26 18:59:21 +00:00
|
|
|
|
|
|
|
return new JsonResponse([
|
2020-11-01 17:11:45 +00:00
|
|
|
'parts' => $parts,
|
2022-09-25 20:38:49 +00:00
|
|
|
'part_size' => $maxPartSize,
|
2020-11-01 17:11:45 +00:00
|
|
|
]);
|
2020-10-31 23:44:20 +00:00
|
|
|
}
|
2022-09-25 20:38:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the configured maximum size of a single part in the multipart uplaod.
|
|
|
|
*
|
|
|
|
* The function tries to retrieve a configured value from the configuration.
|
|
|
|
* If no value is specified, a fallback value will be used.
|
|
|
|
*
|
|
|
|
* Note if the received config cannot be converted to int (0), is zero or is negative,
|
|
|
|
* the fallback value will be used too.
|
|
|
|
*
|
|
|
|
* The fallback value is {@see BackupRemoteUploadController::DEFAULT_MAX_PART_SIZE}.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getConfiguredMaxPartSize()
|
|
|
|
{
|
|
|
|
$maxPartSize = (int) config('backups.max_part_size', self::DEFAULT_MAX_PART_SIZE);
|
|
|
|
if ($maxPartSize <= 0) {
|
|
|
|
$maxPartSize = self::DEFAULT_MAX_PART_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $maxPartSize;
|
|
|
|
}
|
2020-10-31 23:44:20 +00:00
|
|
|
}
|