2019-03-17 00:10:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2020-04-07 03:28:14 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2019-05-02 04:45:39 +00:00
|
|
|
use Illuminate\Http\Response;
|
2019-03-17 00:10:04 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2019-07-28 03:23:51 +00:00
|
|
|
use GuzzleHttp\Exception\TransferException;
|
2020-04-07 03:28:14 +00:00
|
|
|
use Pterodactyl\Services\Nodes\NodeJWTService;
|
2019-10-26 21:36:37 +00:00
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory;
|
2019-09-06 04:41:20 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonFileRepository;
|
2019-07-28 03:23:51 +00:00
|
|
|
use Pterodactyl\Transformers\Daemon\FileObjectTransformer;
|
2019-03-17 00:10:04 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
2019-07-28 03:23:51 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2019-05-05 00:26:24 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest;
|
2019-05-02 03:54:40 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest;
|
2019-05-05 00:26:24 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest;
|
2019-05-04 23:04:59 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest;
|
2019-05-02 04:45:39 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest;
|
2019-05-25 23:24:13 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest;
|
2019-05-27 22:30:49 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\WriteFileContentRequest;
|
2019-03-17 00:10:04 +00:00
|
|
|
|
|
|
|
class FileController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
2019-10-26 21:36:37 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonFileRepository
|
2019-03-17 00:10:04 +00:00
|
|
|
*/
|
2019-10-26 21:36:37 +00:00
|
|
|
private $fileRepository;
|
2019-03-17 00:10:04 +00:00
|
|
|
|
2019-05-25 23:24:13 +00:00
|
|
|
/**
|
2019-10-26 21:36:37 +00:00
|
|
|
* @var \Illuminate\Contracts\Routing\ResponseFactory
|
2019-05-02 03:54:40 +00:00
|
|
|
*/
|
2019-10-26 21:36:37 +00:00
|
|
|
private $responseFactory;
|
2019-05-02 03:54:40 +00:00
|
|
|
|
2020-04-07 03:28:14 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Nodes\NodeJWTService
|
|
|
|
*/
|
|
|
|
private $jwtService;
|
|
|
|
|
2019-03-17 00:10:04 +00:00
|
|
|
/**
|
|
|
|
* FileController constructor.
|
|
|
|
*
|
2019-10-26 21:36:37 +00:00
|
|
|
* @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory
|
2020-04-07 03:28:14 +00:00
|
|
|
* @param \Pterodactyl\Services\Nodes\NodeJWTService $jwtService
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonFileRepository $fileRepository
|
2019-03-17 00:10:04 +00:00
|
|
|
*/
|
2019-10-26 21:36:37 +00:00
|
|
|
public function __construct(
|
|
|
|
ResponseFactory $responseFactory,
|
2020-04-07 03:28:14 +00:00
|
|
|
NodeJWTService $jwtService,
|
2019-10-26 21:36:37 +00:00
|
|
|
DaemonFileRepository $fileRepository
|
|
|
|
) {
|
2019-03-17 00:10:04 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
2019-05-02 03:54:40 +00:00
|
|
|
$this->fileRepository = $fileRepository;
|
2019-10-26 21:36:37 +00:00
|
|
|
$this->responseFactory = $responseFactory;
|
2020-04-07 03:28:14 +00:00
|
|
|
$this->jwtService = $jwtService;
|
2019-05-02 03:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a listing of files in a given directory.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest $request
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-07-28 03:23:51 +00:00
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2019-05-02 03:54:40 +00:00
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function listDirectory(ListFilesRequest $request, Server $server): array
|
2019-05-02 03:54:40 +00:00
|
|
|
{
|
2019-07-28 03:23:51 +00:00
|
|
|
try {
|
|
|
|
$contents = $this->fileRepository
|
2019-09-06 04:41:20 +00:00
|
|
|
->setServer($server)
|
2019-07-28 03:23:51 +00:00
|
|
|
->getDirectory($request->get('directory') ?? '/');
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->fractal->collection($contents)
|
|
|
|
->transformWith($this->getTransformer(FileObjectTransformer::class))
|
|
|
|
->toArray();
|
2019-03-17 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 23:24:13 +00:00
|
|
|
/**
|
|
|
|
* Return the contents of a specified file for the user.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest $request
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-05-25 23:24:13 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
2019-09-06 04:41:20 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException
|
2019-05-25 23:24:13 +00:00
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function getFileContents(GetFileContentsRequest $request, Server $server): Response
|
2019-05-25 23:24:13 +00:00
|
|
|
{
|
|
|
|
return Response::create(
|
2019-09-06 04:41:20 +00:00
|
|
|
$this->fileRepository->setServer($server)->getContent(
|
|
|
|
$request->get('file'), config('pterodactyl.files.max_edit_size')
|
2019-09-29 22:32:22 +00:00
|
|
|
),
|
|
|
|
Response::HTTP_OK,
|
|
|
|
['Content-Type' => 'text/plain']
|
2019-05-25 23:24:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-26 21:36:37 +00:00
|
|
|
/**
|
2020-04-07 03:28:14 +00:00
|
|
|
* Generates a one-time token with a link that the user can use to
|
|
|
|
* download a given file.
|
|
|
|
*
|
2019-10-26 21:36:37 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2020-04-07 03:28:14 +00:00
|
|
|
* @return array
|
2019-10-26 21:36:37 +00:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function download(GetFileContentsRequest $request, Server $server)
|
|
|
|
{
|
2020-04-07 03:28:14 +00:00
|
|
|
$token = $this->jwtService
|
|
|
|
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
|
|
|
|
->setClaims([
|
|
|
|
'file_path' => $request->get('file'),
|
|
|
|
'server_uuid' => $server->uuid,
|
|
|
|
])
|
|
|
|
->handle($server->node, $request->user()->id . $server->uuid);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'object' => 'signed_url',
|
|
|
|
'attributes' => [
|
|
|
|
'url' => sprintf(
|
|
|
|
'%s/download/file?token=%s',
|
|
|
|
$server->node->getConnectionAddress(),
|
|
|
|
$token->__toString()
|
|
|
|
),
|
|
|
|
],
|
|
|
|
];
|
2019-10-26 21:36:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 22:30:49 +00:00
|
|
|
/**
|
|
|
|
* Writes the contents of the specified file to the server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\WriteFileContentRequest $request
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-05-27 22:30:49 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function writeFileContents(WriteFileContentRequest $request, Server $server): Response
|
2019-05-27 22:30:49 +00:00
|
|
|
{
|
2019-09-06 04:41:20 +00:00
|
|
|
$this->fileRepository->setServer($server)->putContent(
|
2019-05-27 22:30:49 +00:00
|
|
|
$request->get('file'),
|
|
|
|
$request->getContent()
|
|
|
|
);
|
|
|
|
|
|
|
|
return Response::create('', Response::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
|
2019-05-02 04:45:39 +00:00
|
|
|
/**
|
|
|
|
* Creates a new folder on the server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest $request
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-05-02 04:45:39 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function createFolder(CreateFolderRequest $request, Server $server): Response
|
2019-05-02 04:45:39 +00:00
|
|
|
{
|
|
|
|
$this->fileRepository
|
2019-09-06 04:41:20 +00:00
|
|
|
->setServer($server)
|
2020-07-11 05:10:41 +00:00
|
|
|
->createDirectory($request->input('name'), $request->input('root', '/'));
|
2019-05-02 04:45:39 +00:00
|
|
|
|
2019-05-04 23:04:59 +00:00
|
|
|
return Response::create('', Response::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renames a file on the remote machine.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest $request
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-05-04 23:04:59 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function renameFile(RenameFileRequest $request, Server $server): Response
|
2019-05-04 23:04:59 +00:00
|
|
|
{
|
|
|
|
$this->fileRepository
|
2019-09-06 04:41:20 +00:00
|
|
|
->setServer($server)
|
2019-05-04 23:04:59 +00:00
|
|
|
->renameFile($request->input('rename_from'), $request->input('rename_to'));
|
2019-05-05 00:26:24 +00:00
|
|
|
|
|
|
|
return Response::create('', Response::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies a file on the server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest $request
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-05-05 00:26:24 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function copyFile(CopyFileRequest $request, Server $server): Response
|
2019-05-05 00:26:24 +00:00
|
|
|
{
|
|
|
|
$this->fileRepository
|
2019-09-06 04:41:20 +00:00
|
|
|
->setServer($server)
|
2019-05-05 00:26:24 +00:00
|
|
|
->copyFile($request->input('location'));
|
|
|
|
|
|
|
|
return Response::create('', Response::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a file or folder from the server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest $request
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-05-05 00:26:24 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function delete(DeleteFileRequest $request, Server $server): Response
|
2019-05-05 00:26:24 +00:00
|
|
|
{
|
|
|
|
$this->fileRepository
|
2019-09-06 04:41:20 +00:00
|
|
|
->setServer($server)
|
2019-05-05 00:26:24 +00:00
|
|
|
->deleteFile($request->input('location'));
|
2019-05-04 23:04:59 +00:00
|
|
|
|
|
|
|
return Response::create('', Response::HTTP_NO_CONTENT);
|
2019-05-02 04:45:39 +00:00
|
|
|
}
|
2019-03-17 00:10:04 +00:00
|
|
|
}
|