2020-10-31 17:44:20 -06:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-10-31 17:44:20 -06:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Backups;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Exception;
|
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2020-10-31 17:44:20 -06:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Pterodactyl\Contracts\Backups\PresignsBackupUploads;
|
2025-12-20 15:55:13 -08:00
|
|
|
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
|
use Pterodactyl\Http\Requests\Api\Remote\RemoteRequestNode;
|
|
|
|
|
use Pterodactyl\Models\Backup;
|
2020-11-01 10:11:45 -07:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
|
|
|
|
use Throwable;
|
2020-10-31 17:44:20 -06:00
|
|
|
|
|
|
|
|
class BackupRemoteUploadController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
2020-11-01 10:11:45 -07:00
|
|
|
* Returns the required presigned urls to upload a backup to S3 cloud storage.
|
2020-10-31 17:44:20 -06:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @throws Exception
|
|
|
|
|
* @throws Throwable
|
|
|
|
|
* @throws ModelNotFoundException
|
2020-10-31 17:44:20 -06:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function __invoke(Request $request, PresignsBackupUploads $presignBackupUpload, string $backup): JsonResponse
|
2020-10-31 17:44:20 -06:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$node = RemoteRequestNode::get($request);
|
2024-04-10 17:38:09 -06:00
|
|
|
|
2020-11-01 10:11:45 -07:00
|
|
|
// Get the size query parameter.
|
2026-10-05 18:10:18 -04:00
|
|
|
$size = $request->integer('size');
|
|
|
|
|
throw_if(empty($size), BadRequestHttpException::class, 'A non-empty "size" query parameter must be provided.');
|
2020-10-31 17:44:20 -06:00
|
|
|
|
2025-12-20 15:55:13 -08:00
|
|
|
$model = Backup::query()->where('uuid', $backup)->firstOrFail();
|
2020-10-31 17:44:20 -06:00
|
|
|
|
2024-05-04 16:06:13 -06:00
|
|
|
// Check that the backup is "owned" by the node making the request. This avoids other nodes
|
|
|
|
|
// from messing with backups that they don't own.
|
|
|
|
|
$server = $model->server;
|
2026-10-05 18:10:18 -04:00
|
|
|
throw_if($server->node_id !== $node->id, HttpForbiddenException::class, 'Requesting node does not have permission to access this server.');
|
2024-05-04 16:06:13 -06:00
|
|
|
|
2020-10-31 17:44:20 -06:00
|
|
|
// Prevent backups that have already been completed from trying to
|
|
|
|
|
// be uploaded again.
|
2026-10-05 18:10:18 -04:00
|
|
|
throw_if(($model->completed_at) !== null, ConflictHttpException::class, 'This backup is already in a completed state.');
|
2022-09-25 22:38:49 +02:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return new JsonResponse($presignBackupUpload->presign($model, $size));
|
2022-09-25 22:38:49 +02:00
|
|
|
}
|
2020-10-31 17:44:20 -06:00
|
|
|
}
|