2019-03-17 00:10:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Ramsey\Uuid\Uuid;
|
2019-05-02 04:45:39 +00:00
|
|
|
use Illuminate\Http\Response;
|
2019-03-17 00:10:04 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2019-07-28 03:23:51 +00:00
|
|
|
use GuzzleHttp\Exception\TransferException;
|
|
|
|
use Pterodactyl\Transformers\Daemon\FileObjectTransformer;
|
2019-05-02 03:54:40 +00:00
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
2019-05-25 23:24:13 +00:00
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
2019-03-17 00:10:04 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
2019-05-02 03:54:40 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
|
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-03-17 00:10:04 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest;
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Cache\Factory
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
2019-05-25 23:24:13 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Config\Repository
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2019-05-02 03:54:40 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $fileRepository;
|
|
|
|
|
2019-03-17 00:10:04 +00:00
|
|
|
/**
|
|
|
|
* FileController constructor.
|
|
|
|
*
|
2019-05-25 23:24:13 +00:00
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
2019-05-02 03:54:40 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface $fileRepository
|
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache
|
2019-03-17 00:10:04 +00:00
|
|
|
*/
|
2019-05-25 23:24:13 +00:00
|
|
|
public function __construct(ConfigRepository $config, FileRepositoryInterface $fileRepository, CacheRepository $cache)
|
2019-03-17 00:10:04 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->cache = $cache;
|
2019-05-25 23:24:13 +00:00
|
|
|
$this->config = $config;
|
2019-05-02 03:54:40 +00:00
|
|
|
$this->fileRepository = $fileRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a listing of files in a given directory.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest $request
|
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-07-28 03:23:51 +00:00
|
|
|
public function listDirectory(ListFilesRequest $request): array
|
2019-05-02 03:54:40 +00:00
|
|
|
{
|
2019-07-28 03:23:51 +00:00
|
|
|
try {
|
|
|
|
$contents = $this->fileRepository
|
|
|
|
->setServer($request->getModel(Server::class))
|
|
|
|
->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
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function getFileContents(GetFileContentsRequest $request): Response
|
|
|
|
{
|
|
|
|
return Response::create(
|
|
|
|
$this->fileRepository->setServer($request->getModel(Server::class))->getContent(
|
|
|
|
$request->get('file'), $this->config->get('pterodactyl.files.max_edit_size')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function writeFileContents(WriteFileContentRequest $request): Response
|
|
|
|
{
|
|
|
|
$this->fileRepository->setServer($request->getModel(Server::class))->putContent(
|
|
|
|
$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
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function createFolder(CreateFolderRequest $request): Response
|
|
|
|
{
|
|
|
|
$this->fileRepository
|
|
|
|
->setServer($request->getModel(Server::class))
|
|
|
|
->createDirectory($request->input('name'), $request->input('directory', '/'));
|
|
|
|
|
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
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function renameFile(RenameFileRequest $request): Response
|
|
|
|
{
|
|
|
|
$this->fileRepository
|
|
|
|
->setServer($request->getModel(Server::class))
|
|
|
|
->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
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function copyFile(CopyFileRequest $request): Response
|
|
|
|
{
|
|
|
|
$this->fileRepository
|
|
|
|
->setServer($request->getModel(Server::class))
|
|
|
|
->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
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function delete(DeleteFileRequest $request): Response
|
|
|
|
{
|
|
|
|
$this->fileRepository
|
|
|
|
->setServer($request->getModel(Server::class))
|
|
|
|
->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
|
|
|
/**
|
|
|
|
* Configure a reference to a file to download in the cache so that when the
|
|
|
|
* user hits the Daemon and it verifies with the Panel they'll actually be able
|
|
|
|
* to download that file.
|
|
|
|
*
|
|
|
|
* Returns the token that needs to be used when downloading the file.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function download(DownloadFileRequest $request): JsonResponse
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\Server $server */
|
|
|
|
$server = $request->getModel(Server::class);
|
|
|
|
$token = Uuid::uuid4()->toString();
|
|
|
|
|
|
|
|
$this->cache->put(
|
|
|
|
'Server:Downloads:' . $token, ['server' => $server->uuid, 'path' => $request->route()->parameter('file')], Carbon::now()->addMinutes(5)
|
|
|
|
);
|
|
|
|
|
|
|
|
return JsonResponse::create(['token' => $token]);
|
|
|
|
}
|
|
|
|
}
|