misc_pterodactyl-panel/app/Http/Controllers/Api/Client/Servers/FileController.php

280 lines
9.2 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Carbon\CarbonImmutable;
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse;
2022-05-29 17:56:39 +00:00
use Pterodactyl\Facades\Activity;
use Pterodactyl\Services\Nodes\NodeJWTService;
use Pterodactyl\Repositories\Wings\DaemonFileRepository;
2022-05-22 20:23:22 +00:00
use Pterodactyl\Transformers\Api\Client\FileObjectTransformer;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
2019-05-05 00:26:24 +00:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\PullFileRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ChmodFilesRequest;
2019-05-05 00:26:24 +00:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CompressFilesRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DecompressFilesRequest;
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;
class FileController extends ClientApiController
{
/**
* @var \Pterodactyl\Repositories\Wings\DaemonFileRepository
*/
private $fileRepository;
/**
* @var \Pterodactyl\Services\Nodes\NodeJWTService
*/
private $jwtService;
/**
* FileController constructor.
*/
public function __construct(
NodeJWTService $jwtService,
DaemonFileRepository $fileRepository
) {
parent::__construct();
$this->fileRepository = $fileRepository;
$this->jwtService = $jwtService;
}
/**
* Returns a listing of files in a given directory.
*
2019-07-28 03:23:51 +00:00
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function directory(ListFilesRequest $request, Server $server): array
{
$contents = $this->fileRepository
->setServer($server)
->getDirectory($request->get('directory') ?? '/');
2019-07-28 03:23:51 +00:00
return $this->fractal->collection($contents)
->transformWith($this->getTransformer(FileObjectTransformer::class))
->toArray();
}
/**
* Return the contents of a specified file for the user.
*
* @throws \Throwable
*/
public function contents(GetFileContentsRequest $request, Server $server): Response
{
$response = $this->fileRepository->setServer($server)->getContent(
2021-01-26 03:20:51 +00:00
$request->get('file'),
config('pterodactyl.files.max_edit_size')
);
2022-05-29 17:56:39 +00:00
Activity::event('server:file.read')->property('file', $request->get('file'))->log();
return new Response($response, Response::HTTP_OK, ['Content-Type' => 'text/plain']);
}
/**
* Generates a one-time token with a link that the user can use to
* download a given file.
*
* @return array
*
* @throws \Throwable
*/
public function download(GetFileContentsRequest $request, Server $server)
{
2022-05-29 17:56:39 +00:00
$token = $this->jwtService
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
->setClaims([
'file_path' => rawurldecode($request->get('file')),
'server_uuid' => $server->uuid,
])
->handle($server->node, $request->user()->id . $server->uuid);
Activity::event('server:file.download')->property('file', $request->get('file'))->log();
return [
'object' => 'signed_url',
'attributes' => [
'url' => sprintf(
'%s/download/file?token=%s',
$server->node->getConnectionAddress(),
$token->toString()
),
],
];
}
2019-05-27 22:30:49 +00:00
/**
* Writes the contents of the specified file to the server.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
2019-05-27 22:30:49 +00:00
*/
public function write(WriteFileContentRequest $request, Server $server): JsonResponse
2019-05-27 22:30:49 +00:00
{
2022-05-29 17:56:39 +00:00
$this->fileRepository->setServer($server)->putContent($request->get('file'), $request->getContent());
2022-05-29 17:56:39 +00:00
Activity::event('server:file.write')->property('file', $request->get('file'))->log();
2019-05-27 22:30:49 +00:00
return new JsonResponse([], Response::HTTP_NO_CONTENT);
2019-05-27 22:30:49 +00:00
}
/**
* Creates a new folder on the server.
*
* @throws \Throwable
*/
public function create(CreateFolderRequest $request, Server $server): JsonResponse
{
2022-05-29 17:56:39 +00:00
$this->fileRepository
->setServer($server)
->createDirectory($request->input('name'), $request->input('root', '/'));
2022-05-29 17:56:39 +00:00
Activity::event('server:file.create-directory')
->property('name', $request->input('name'))
->property('directory', $request->input('root'))
->log();
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
* Renames a file on the remote machine.
*
* @throws \Throwable
*/
public function rename(RenameFileRequest $request, Server $server): JsonResponse
{
2022-05-29 17:56:39 +00:00
$this->fileRepository
->setServer($server)
->renameFiles($request->input('root'), $request->input('files'));
2022-05-29 17:56:39 +00:00
Activity::event('server:file.rename')
->property('directory', $request->input('root'))
->property('files', $request->input('files'))
->log();
2019-05-05 00:26:24 +00:00
return new JsonResponse([], Response::HTTP_NO_CONTENT);
2019-05-05 00:26:24 +00:00
}
/**
* Copies a file on the server.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
2019-05-05 00:26:24 +00:00
*/
public function copy(CopyFileRequest $request, Server $server): JsonResponse
2019-05-05 00:26:24 +00:00
{
2022-05-29 17:56:39 +00:00
$this->fileRepository
->setServer($server)
->copyFile($request->input('location'));
2021-01-17 19:52:44 +00:00
2022-05-29 17:56:39 +00:00
Activity::event('server:file.copy')->property('file', $request->input('location'))->log();
2019-05-05 00:26:24 +00:00
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function compress(CompressFilesRequest $request, Server $server): array
{
2022-05-29 17:56:39 +00:00
$file = $this->fileRepository->setServer($server)->compressFiles(
$request->input('root'),
$request->input('files')
);
2022-05-29 17:56:39 +00:00
Activity::event('server:file.compress')
->property('directory', $request->input('root'))
->property('files', $request->input('files'))
->log();
return $this->fractal->item($file)
->transformWith($this->getTransformer(FileObjectTransformer::class))
->toArray();
2019-05-05 00:26:24 +00:00
}
/**
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function decompress(DecompressFilesRequest $request, Server $server): JsonResponse
{
2022-05-29 17:56:39 +00:00
set_time_limit(300);
2022-05-29 17:56:39 +00:00
$this->fileRepository->setServer($server)->decompressFile(
$request->input('root'),
$request->input('file')
);
2022-05-29 17:56:39 +00:00
Activity::event('server:file.decompress')
->property('directory', $request->input('root'))
->property('files', $request->input('file'))
->log();
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
2019-05-05 00:26:24 +00:00
/**
2020-07-11 22:37:59 +00:00
* Deletes files or folders for the server in the given root directory.
2019-05-05 00:26:24 +00:00
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
2019-05-05 00:26:24 +00:00
*/
public function delete(DeleteFileRequest $request, Server $server): JsonResponse
2019-05-05 00:26:24 +00:00
{
2022-05-29 17:56:39 +00:00
$this->fileRepository->setServer($server)->deleteFiles(
$request->input('root'),
$request->input('files')
);
2022-05-29 17:56:39 +00:00
Activity::event('server:file.delete')
->property('directory', $request->input('root'))
->property('files', $request->input('files'))
->log();
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
* Updates file permissions for file(s) in the given root directory.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function chmod(ChmodFilesRequest $request, Server $server): JsonResponse
{
2022-05-29 17:56:39 +00:00
$this->fileRepository->setServer($server)->chmodFiles(
$request->input('root'),
$request->input('files')
);
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
* Requests that a file be downloaded from a remote location by Wings.
*
* @throws \Throwable
*/
public function pull(PullFileRequest $request, Server $server): JsonResponse
{
2022-05-29 17:56:39 +00:00
$this->fileRepository->setServer($server)->pull(
$request->input('url'),
$request->input('directory'),
$request->safe(['filename', 'use_header', 'foreground'])
);
Activity::event('server:file.pull')
->property('directory', $request->input('directory'))
->property('url', $request->input('url'))
->log();
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
}