2020-07-12 16:42:32 -06:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-07-12 16:42:32 -06:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
|
|
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Knuckles\Scribe\Attributes\Endpoint;
|
|
|
|
|
use Knuckles\Scribe\Attributes\Group;
|
|
|
|
|
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
|
|
|
|
|
use Knuckles\Scribe\Attributes\Subgroup;
|
|
|
|
|
use Pterodactyl\Enum\JwtScope;
|
2020-07-12 16:42:32 -06:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\UploadFileRequest;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
|
use Pterodactyl\Services\Nodes\NodeJWTService;
|
2020-07-12 16:42:32 -06:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
|
|
|
|
|
#[Subgroup('Server Files', 'Browse, read, mutate, archive, and transfer server files through Wings.')]
|
2020-07-12 16:42:32 -06:00
|
|
|
class FileUploadController extends ClientApiController
|
|
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
private const array SIGNED_URL_EXAMPLE = [
|
|
|
|
|
'object' => 'signed_url',
|
|
|
|
|
'attributes' => [
|
|
|
|
|
'url' => 'https://node.example.test/upload/file?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.example.signature',
|
|
|
|
|
],
|
|
|
|
|
];
|
2020-07-12 16:42:32 -06:00
|
|
|
|
|
|
|
|
/**
|
2022-10-14 10:59:20 -06:00
|
|
|
* Returns an url where files can be uploaded to.
|
2020-07-12 16:42:32 -06:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
#[Endpoint('Get file upload URL', 'Returns a short-lived signed URL for uploading a file directly to Wings.')]
|
|
|
|
|
#[ScribeResponse(self::SIGNED_URL_EXAMPLE, description: 'Signed upload URL returned.')]
|
|
|
|
|
public function __invoke(UploadFileRequest $request, NodeJWTService $jwtService, Server $server): JsonResponse
|
2020-07-12 16:42:32 -06:00
|
|
|
{
|
|
|
|
|
return new JsonResponse([
|
|
|
|
|
'object' => 'signed_url',
|
|
|
|
|
'attributes' => [
|
2026-10-05 18:10:18 -04:00
|
|
|
'url' => $this->getUploadUrl($server, $request->user(), $jwtService),
|
2020-07-12 16:42:32 -06:00
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-10-14 10:59:20 -06:00
|
|
|
* Returns an url where files can be uploaded to.
|
2020-07-12 16:42:32 -06:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
protected function getUploadUrl(Server $server, User $user, NodeJWTService $jwtService): string
|
2020-07-12 16:42:32 -06:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$token = $jwtService
|
2020-07-12 16:42:32 -06:00
|
|
|
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
|
2022-07-04 17:34:56 -04:00
|
|
|
->setUser($user)
|
|
|
|
|
->setClaims(['server_uuid' => $server->uuid])
|
2026-05-23 11:15:36 -07:00
|
|
|
->setScopes(JwtScope::FileUpload)
|
2026-10-05 18:10:18 -04:00
|
|
|
->handle($server->node, $user->id.$server->uuid);
|
2020-07-12 16:42:32 -06:00
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
|
'%s/upload/file?token=%s',
|
|
|
|
|
$server->node->getConnectionAddress(),
|
2021-01-23 12:09:16 -08:00
|
|
|
$token->toString()
|
2020-07-12 16:42:32 -06:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|