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

279 lines
9.8 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 Pterodactyl\Models\AuditLog;
use Pterodactyl\Services\Nodes\NodeJWTService;
use Illuminate\Contracts\Routing\ResponseFactory;
use Pterodactyl\Repositories\Wings\DaemonFileRepository;
2019-07-28 03:23:51 +00:00
use Pterodactyl\Transformers\Daemon\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
{
2021-03-05 17:03:12 +00:00
private DaemonFileRepository $fileRepository;
private ResponseFactory $responseFactory;
private NodeJWTService $jwtService;
/**
* FileController constructor.
*/
public function __construct(
2021-03-05 17:03:12 +00:00
DaemonFileRepository $fileRepository,
ResponseFactory $responseFactory,
2021-03-05 17:03:12 +00:00
NodeJWTService $jwtService
) {
parent::__construct();
$this->fileRepository = $fileRepository;
$this->responseFactory = $responseFactory;
$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
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
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')
);
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.
*
* @throws \Throwable
*/
2021-03-05 17:03:12 +00:00
public function download(GetFileContentsRequest $request, Server $server): array
{
2021-01-17 23:25:49 +00:00
$token = $server->audit(AuditLog::SERVER__FILESYSTEM_DOWNLOAD, function (AuditLog $audit, Server $server) use ($request) {
$audit->metadata = ['file' => $request->get('file')];
return $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);
});
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.
*
2021-03-05 17:03:12 +00:00
* @throws \Throwable
2019-05-27 22:30:49 +00:00
*/
2021-03-05 17:03:12 +00:00
public function write(WriteFileContentRequest $request, Server $server): Response
2019-05-27 22:30:49 +00:00
{
2021-01-17 23:25:49 +00:00
$server->audit(AuditLog::SERVER__FILESYSTEM_WRITE, function (AuditLog $audit, Server $server) use ($request) {
2021-01-17 19:52:44 +00:00
$audit->subaction = 'write_content';
$audit->metadata = ['file' => $request->get('file')];
$this->fileRepository
->setServer($server)
->putContent($request->get('file'), $request->getContent());
});
2019-05-27 22:30:49 +00:00
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
2019-05-27 22:30:49 +00:00
}
/**
* Creates a new folder on the server.
*
* @throws \Throwable
*/
2021-03-05 17:03:12 +00:00
public function create(CreateFolderRequest $request, Server $server): Response
{
2021-01-17 23:25:49 +00:00
$server->audit(AuditLog::SERVER__FILESYSTEM_WRITE, function (AuditLog $audit, Server $server) use ($request) {
2021-01-17 19:52:44 +00:00
$audit->subaction = 'create_folder';
$audit->metadata = ['file' => $request->input('root', '/') . $request->input('name')];
$this->fileRepository
->setServer($server)
->createDirectory($request->input('name'), $request->input('root', '/'));
});
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
}
/**
* Renames a file on the remote machine.
*
* @throws \Throwable
*/
2021-03-05 17:03:12 +00:00
public function rename(RenameFileRequest $request, Server $server): Response
{
2021-01-17 23:25:49 +00:00
$server->audit(AuditLog::SERVER__FILESYSTEM_RENAME, function (AuditLog $audit, Server $server) use ($request) {
$audit->metadata = ['root' => $request->input('root'), 'files' => $request->input('files')];
$this->fileRepository
->setServer($server)
->renameFiles($request->input('root'), $request->input('files'));
});
2019-05-05 00:26:24 +00:00
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
2019-05-05 00:26:24 +00:00
}
/**
* Copies a file on the server.
*
2021-03-05 17:03:12 +00:00
* @throws \Throwable
2019-05-05 00:26:24 +00:00
*/
2021-03-05 17:03:12 +00:00
public function copy(CopyFileRequest $request, Server $server): Response
2019-05-05 00:26:24 +00:00
{
2021-01-17 23:25:49 +00:00
$server->audit(AuditLog::SERVER__FILESYSTEM_WRITE, function (AuditLog $audit, Server $server) use ($request) {
2021-01-17 19:52:44 +00:00
$audit->subaction = 'copy_file';
$audit->metadata = ['file' => $request->input('location')];
$this->fileRepository
->setServer($server)
->copyFile($request->input('location'));
});
2019-05-05 00:26:24 +00:00
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
}
/**
2021-03-05 17:03:12 +00:00
* @throws \Throwable
*/
public function compress(CompressFilesRequest $request, Server $server): array
{
2021-01-17 23:25:49 +00:00
$file = $server->audit(AuditLog::SERVER__FILESYSTEM_COMPRESS, function (AuditLog $audit, Server $server) use ($request) {
// Allow up to five minutes for this request to process before timing out.
set_time_limit(300);
$audit->metadata = ['root' => $request->input('root'), 'files' => $request->input('files')];
return $this->fileRepository->setServer($server)
->compressFiles(
2021-01-26 03:20:51 +00:00
$request->input('root'),
$request->input('files')
);
});
return $this->fractal->item($file)
->transformWith($this->getTransformer(FileObjectTransformer::class))
->toArray();
2019-05-05 00:26:24 +00:00
}
/**
2021-03-05 17:03:12 +00:00
* @throws \Throwable
*/
2021-03-05 17:03:12 +00:00
public function decompress(DecompressFilesRequest $request, Server $server): Response
{
2021-01-17 23:25:49 +00:00
$file = $server->audit(AuditLog::SERVER__FILESYSTEM_DECOMPRESS, function (AuditLog $audit, Server $server) use ($request) {
// Allow up to five minutes for this request to process before timing out.
set_time_limit(300);
$audit->metadata = ['root' => $request->input('root'), 'files' => $request->input('file')];
$this->fileRepository->setServer($server)
->decompressFile($request->input('root'), $request->input('file'));
});
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
}
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
*
2021-03-05 17:03:12 +00:00
* @throws \Throwable
2019-05-05 00:26:24 +00:00
*/
2021-03-05 17:03:12 +00:00
public function delete(DeleteFileRequest $request, Server $server): Response
2019-05-05 00:26:24 +00:00
{
2021-01-17 23:25:49 +00:00
$server->audit(AuditLog::SERVER__FILESYSTEM_DELETE, function (AuditLog $audit, Server $server) use ($request) {
$audit->metadata = ['root' => $request->input('root'), 'files' => $request->input('files')];
$this->fileRepository->setServer($server)
->deleteFiles(
2021-01-26 03:20:51 +00:00
$request->input('root'),
$request->input('files')
);
});
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
}
/**
* Updates file permissions for file(s) in the given root directory.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
2021-03-05 17:03:12 +00:00
public function chmod(ChmodFilesRequest $request, Server $server): Response
{
$this->fileRepository->setServer($server)
->chmodFiles(
2021-01-23 20:33:34 +00:00
$request->input('root'),
$request->input('files')
);
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
}
/**
* Requests that a file be downloaded from a remote location by Wings.
*
* @throws \Throwable
*/
2021-03-05 17:03:12 +00:00
public function pull(PullFileRequest $request, Server $server): Response
{
2021-01-17 23:25:49 +00:00
$server->audit(AuditLog::SERVER__FILESYSTEM_PULL, function (AuditLog $audit, Server $server) use ($request) {
$audit->metadata = ['directory' => $request->input('directory'), 'url' => $request->input('url')];
$this->fileRepository->setServer($server)->pull($request->input('url'), $request->input('directory'));
});
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
}
}