Update client API endpoints to not use deprecated function

This commit is contained in:
Dane Everitt 2019-09-05 21:41:20 -07:00
parent 7543ef085d
commit b99ea53ca1
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
8 changed files with 74 additions and 58 deletions

View file

@ -3,23 +3,23 @@
namespace Pterodactyl\Http\Controllers\Api\Client; namespace Pterodactyl\Http\Controllers\Api\Client;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Transformers\Api\Client\ServerTransformer; use Pterodactyl\Transformers\Api\Client\ServerTransformer;
use Pterodactyl\Http\Requests\Api\Client\GetServersRequest; use Pterodactyl\Http\Requests\Api\Client\GetServersRequest;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class ClientController extends ClientApiController class ClientController extends ClientApiController
{ {
/** /**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface * @var \Pterodactyl\Repositories\Eloquent\ServerRepository
*/ */
private $repository; private $repository;
/** /**
* ClientController constructor. * ClientController constructor.
* *
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
*/ */
public function __construct(ServerRepositoryInterface $repository) public function __construct(ServerRepository $repository)
{ {
parent::__construct(); parent::__construct();

View file

@ -8,11 +8,10 @@ use Illuminate\Http\Response;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
use Pterodactyl\Repositories\Wings\DaemonFileRepository;
use Pterodactyl\Transformers\Daemon\FileObjectTransformer; use Pterodactyl\Transformers\Daemon\FileObjectTransformer;
use Illuminate\Contracts\Cache\Repository as CacheRepository; use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest; use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest; use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest;
@ -31,28 +30,21 @@ class FileController extends ClientApiController
private $cache; private $cache;
/** /**
* @var \Illuminate\Contracts\Config\Repository * @var \Pterodactyl\Repositories\Wings\DaemonFileRepository
*/
private $config;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface
*/ */
private $fileRepository; private $fileRepository;
/** /**
* FileController constructor. * FileController constructor.
* *
* @param \Illuminate\Contracts\Config\Repository $config * @param \Pterodactyl\Repositories\Wings\DaemonFileRepository $fileRepository
* @param \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface $fileRepository
* @param \Illuminate\Contracts\Cache\Repository $cache * @param \Illuminate\Contracts\Cache\Repository $cache
*/ */
public function __construct(ConfigRepository $config, FileRepositoryInterface $fileRepository, CacheRepository $cache) public function __construct(DaemonFileRepository $fileRepository, CacheRepository $cache)
{ {
parent::__construct(); parent::__construct();
$this->cache = $cache; $this->cache = $cache;
$this->config = $config;
$this->fileRepository = $fileRepository; $this->fileRepository = $fileRepository;
} }
@ -60,15 +52,16 @@ class FileController extends ClientApiController
* Returns a listing of files in a given directory. * Returns a listing of files in a given directory.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest $request
* @param \Pterodactyl\Models\Server $server
* @return array * @return array
* *
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function listDirectory(ListFilesRequest $request): array public function listDirectory(ListFilesRequest $request, Server $server): array
{ {
try { try {
$contents = $this->fileRepository $contents = $this->fileRepository
->setServer($request->getModel(Server::class)) ->setServer($server)
->getDirectory($request->get('directory') ?? '/'); ->getDirectory($request->get('directory') ?? '/');
} catch (TransferException $exception) { } catch (TransferException $exception) {
throw new DaemonConnectionException($exception, true); throw new DaemonConnectionException($exception, true);
@ -83,13 +76,15 @@ class FileController extends ClientApiController
* Return the contents of a specified file for the user. * Return the contents of a specified file for the user.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
* @throws \Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException
*/ */
public function getFileContents(GetFileContentsRequest $request): Response public function getFileContents(GetFileContentsRequest $request, Server $server): Response
{ {
return Response::create( return Response::create(
$this->fileRepository->setServer($request->getModel(Server::class))->getContent( $this->fileRepository->setServer($server)->getContent(
$request->get('file'), $this->config->get('pterodactyl.files.max_edit_size') $request->get('file'), config('pterodactyl.files.max_edit_size')
) )
); );
} }
@ -98,11 +93,12 @@ class FileController extends ClientApiController
* Writes the contents of the specified file to the server. * Writes the contents of the specified file to the server.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\WriteFileContentRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\WriteFileContentRequest $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function writeFileContents(WriteFileContentRequest $request): Response public function writeFileContents(WriteFileContentRequest $request, Server $server): Response
{ {
$this->fileRepository->setServer($request->getModel(Server::class))->putContent( $this->fileRepository->setServer($server)->putContent(
$request->get('file'), $request->get('file'),
$request->getContent() $request->getContent()
); );
@ -114,12 +110,13 @@ class FileController extends ClientApiController
* Creates a new folder on the server. * Creates a new folder on the server.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function createFolder(CreateFolderRequest $request): Response public function createFolder(CreateFolderRequest $request, Server $server): Response
{ {
$this->fileRepository $this->fileRepository
->setServer($request->getModel(Server::class)) ->setServer($server)
->createDirectory($request->input('name'), $request->input('directory', '/')); ->createDirectory($request->input('name'), $request->input('directory', '/'));
return Response::create('', Response::HTTP_NO_CONTENT); return Response::create('', Response::HTTP_NO_CONTENT);
@ -129,12 +126,13 @@ class FileController extends ClientApiController
* Renames a file on the remote machine. * Renames a file on the remote machine.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function renameFile(RenameFileRequest $request): Response public function renameFile(RenameFileRequest $request, Server $server): Response
{ {
$this->fileRepository $this->fileRepository
->setServer($request->getModel(Server::class)) ->setServer($server)
->renameFile($request->input('rename_from'), $request->input('rename_to')); ->renameFile($request->input('rename_from'), $request->input('rename_to'));
return Response::create('', Response::HTTP_NO_CONTENT); return Response::create('', Response::HTTP_NO_CONTENT);
@ -144,12 +142,13 @@ class FileController extends ClientApiController
* Copies a file on the server. * Copies a file on the server.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function copyFile(CopyFileRequest $request): Response public function copyFile(CopyFileRequest $request, Server $server): Response
{ {
$this->fileRepository $this->fileRepository
->setServer($request->getModel(Server::class)) ->setServer($server)
->copyFile($request->input('location')); ->copyFile($request->input('location'));
return Response::create('', Response::HTTP_NO_CONTENT); return Response::create('', Response::HTTP_NO_CONTENT);
@ -159,12 +158,13 @@ class FileController extends ClientApiController
* Deletes a file or folder from the server. * Deletes a file or folder from the server.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function delete(DeleteFileRequest $request): Response public function delete(DeleteFileRequest $request, Server $server): Response
{ {
$this->fileRepository $this->fileRepository
->setServer($request->getModel(Server::class)) ->setServer($server)
->deleteFile($request->input('location')); ->deleteFile($request->input('location'));
return Response::create('', Response::HTTP_NO_CONTENT); return Response::create('', Response::HTTP_NO_CONTENT);
@ -178,13 +178,12 @@ class FileController extends ClientApiController
* Returns the token that needs to be used when downloading the file. * Returns the token that needs to be used when downloading the file.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
* @throws \Exception * @throws \Exception
*/ */
public function download(DownloadFileRequest $request): JsonResponse public function download(DownloadFileRequest $request, Server $server): JsonResponse
{ {
/** @var \Pterodactyl\Models\Server $server */
$server = $request->getModel(Server::class);
$token = Uuid::uuid4()->toString(); $token = Uuid::uuid4()->toString();
$this->cache->put( $this->cache->put(

View file

@ -3,24 +3,24 @@
namespace Pterodactyl\Http\Controllers\Api\Client\Servers; namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
use Pterodactyl\Transformers\Api\Client\AllocationTransformer; use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest; use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
class NetworkController extends ClientApiController class NetworkController extends ClientApiController
{ {
/** /**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface * @var \Pterodactyl\Repositories\Eloquent\AllocationRepository
*/ */
private $repository; private $repository;
/** /**
* NetworkController constructor. * NetworkController constructor.
* *
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository * @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $repository
*/ */
public function __construct(AllocationRepositoryInterface $repository) public function __construct(AllocationRepository $repository)
{ {
parent::__construct(); parent::__construct();
@ -32,12 +32,11 @@ class NetworkController extends ClientApiController
* not they are currently assigned as the primary for this server. * not they are currently assigned as the primary for this server.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest $request
* @param \Pterodactyl\Models\Server $server
* @return array * @return array
*/ */
public function index(GetNetworkRequest $request): array public function index(GetNetworkRequest $request, Server $server): array
{ {
$server = $request->getModel(Server::class);
$allocations = $this->repository->findWhere([ $allocations = $this->repository->findWhere([
['server_id', '=', $server->id], ['server_id', '=', $server->id],
]); ]);

View file

@ -4,23 +4,23 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest; use Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest;
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
class PowerController extends ClientApiController class PowerController extends ClientApiController
{ {
/** /**
* @var \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface * @var \Pterodactyl\Repositories\Wings\DaemonPowerRepository
*/ */
private $repository; private $repository;
/** /**
* PowerController constructor. * PowerController constructor.
* *
* @param \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface $repository * @param \Pterodactyl\Repositories\Wings\DaemonPowerRepository $repository
*/ */
public function __construct(PowerRepositoryInterface $repository) public function __construct(DaemonPowerRepository $repository)
{ {
parent::__construct(); parent::__construct();
@ -31,16 +31,14 @@ class PowerController extends ClientApiController
* Send a power action to a server. * Send a power action to a server.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*
* @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException
*/ */
public function index(SendPowerRequest $request): Response public function index(SendPowerRequest $request, Server $server): Response
{ {
$server = $request->getModel(Server::class); $this->repository->setServer($server)->send(
$token = $request->attributes->get('server_token'); $request->input('signal')
);
$this->repository->setServer($server)->setToken($token)->sendSignal($request->input('signal'));
return $this->returnNoContent(); return $this->returnNoContent();
} }

View file

@ -31,15 +31,14 @@ class ResourceUtilizationController extends ClientApiController
* Return the current resource utilization for a server. * Return the current resource utilization for a server.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest $request
* @param \Pterodactyl\Models\Server $server
* @return array * @return array
* *
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function __invoke(GetServerRequest $request): array public function __invoke(GetServerRequest $request, Server $server): array
{ {
$stats = $this->repository $stats = $this->repository->setServer($server)->getDetails();
->setServer($request->getModel(Server::class))
->getDetails();
return $this->fractal->item($stats) return $this->fractal->item($stats)
->transformWith($this->getTransformer(StatsTransformer::class)) ->transformWith($this->getTransformer(StatsTransformer::class))

View file

@ -14,11 +14,12 @@ class ServerController extends ClientApiController
* client using the API. * client using the API.
* *
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest $request * @param \Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest $request
* @param \Pterodactyl\Models\Server $server
* @return array * @return array
*/ */
public function index(GetServerRequest $request): array public function index(GetServerRequest $request, Server $server): array
{ {
return $this->fractal->item($request->getModel(Server::class)) return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class)) ->transformWith($this->getTransformer(ServerTransformer::class))
->toArray(); ->toArray();
} }

View file

@ -89,6 +89,7 @@ abstract class ApplicationApiRequest extends FormRequest
* *
* @param string $model * @param string $model
* @return mixed * @return mixed
* @deprecated
* *
* @throws \Symfony\Component\Routing\Exception\InvalidParameterException * @throws \Symfony\Component\Routing\Exception\InvalidParameterException
*/ */

View file

@ -2,6 +2,25 @@
namespace Pterodactyl\Repositories\Wings; namespace Pterodactyl\Repositories\Wings;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server;
use Psr\Http\Message\ResponseInterface;
class DaemonPowerRepository extends DaemonRepository class DaemonPowerRepository extends DaemonRepository
{ {
/**
* Sends a power action to the server instance.
*
* @param string $action
* @return \Psr\Http\Message\ResponseInterface
*/
public function send(string $action): ResponseInterface
{
Assert::isInstanceOf($this->server, Server::class);
return $this->getHttpClient()->post(
sprintf('/api/servers/%s/power', $this->server->id),
['json' => ['action' => $action]]
);
}
} }