Files
pterodactyl-panel/app/Http/Controllers/Api/Client/Servers/FileController.php
T

442 lines
18 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Carbon\CarbonImmutable;
use Illuminate\Auth\Access\AuthorizationException;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
2026-10-05 18:10:18 -04:00
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\QueryParam;
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
use Knuckles\Scribe\Attributes\Subgroup;
use Pterodactyl\Contracts\Files\ChangesFilePermissions;
use Pterodactyl\Contracts\Files\CompressesFiles;
use Pterodactyl\Contracts\Files\CopiesFiles;
use Pterodactyl\Contracts\Files\CreatesDirectories;
use Pterodactyl\Contracts\Files\DecompressesFiles;
use Pterodactyl\Contracts\Files\DeletesFiles;
use Pterodactyl\Contracts\Files\ListsDirectories;
use Pterodactyl\Contracts\Files\PullsFiles;
use Pterodactyl\Contracts\Files\ReadsFileContents;
use Pterodactyl\Contracts\Files\RenamesFiles;
use Pterodactyl\Contracts\Files\WritesFileContents;
use Pterodactyl\Enum\JwtScope;
use Pterodactyl\Enum\Permissions;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseField;
2022-05-29 13:56:39 -04:00
use Pterodactyl\Facades\Activity;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Fractal;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
2021-01-23 12:09:16 -08:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ChmodFilesRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CompressFilesRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DecompressFilesRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\PullFileRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest;
2019-05-27 15:30:49 -07:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\WriteFileContentRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Server;
use Pterodactyl\Services\Nodes\NodeJWTService;
use Pterodactyl\Support\JsonValueGuard;
use Pterodactyl\Transformers\Api\Client\FileObjectTransformer;
use Throwable;
2026-10-05 18:10:18 -04:00
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
#[Subgroup('Server Files', 'Browse, read, mutate, archive, and transfer server files through Wings.')]
#[ResponseField('attributes.mode', nullable: true)]
#[ResponseField('attributes.mode_bits', nullable: true)]
#[ResponseField('attributes.size', 'integer', nullable: true)]
class FileController extends ClientApiController
{
2026-10-05 18:10:18 -04:00
private const array FILE_OBJECT_EXAMPLE = [
'object' => 'file_object',
'attributes' => [
'name' => 'server.properties',
'mode' => '-rw-r--r--',
'mode_bits' => '0644',
'size' => 1024,
'is_file' => true,
'is_symlink' => false,
'mimetype' => 'text/plain',
'created_at' => '2026-06-29T12:00:00+00:00',
'modified_at' => '2026-06-29T12:00:00+00:00',
],
];
private const array ARCHIVE_OBJECT_EXAMPLE = [
'object' => 'file_object',
'attributes' => [
'name' => 'archive-2026-06-29.tar.gz',
'mode' => '-rw-r--r--',
'mode_bits' => '0644',
'size' => 4096,
'is_file' => true,
'is_symlink' => false,
'mimetype' => 'application/gzip',
'created_at' => '2026-06-29T12:00:00+00:00',
'modified_at' => '2026-06-29T12:00:00+00:00',
],
];
private const array FILE_LIST_EXAMPLE = [
'object' => 'list',
'data' => [
self::FILE_OBJECT_EXAMPLE,
],
];
private const array SIGNED_URL_EXAMPLE = [
'object' => 'signed_url',
'attributes' => [
'url' => 'https://node.example.test/download/file?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.example.signature',
],
];
private const array DAEMON_ERROR = [
'errors' => [
[
'code' => 'DaemonConnectionException',
'status' => '502',
'detail' => 'There was an error while communicating with the machine running this server.',
],
],
];
/**
* Returns a listing of files in a given directory.
*
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
*
* @throws DaemonConnectionException
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('List files', 'Returns files and folders in a server directory.')]
#[QueryParam('directory', 'string', 'Directory to list. Defaults to the server root.', required: false, example: '/config', nullable: true)]
#[ScribeResponse(self::FILE_LIST_EXAMPLE, description: 'Directory contents returned.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not list the directory.')]
public function directory(ListFilesRequest $request, ListsDirectories $operation, Server $server): array
{
2026-10-05 18:10:18 -04:00
$directory = JsonValueGuard::nullableString($request->validated('directory')) ?? '/';
$contents = $operation->list($server, $directory);
2019-07-27 20:23:51 -07:00
2026-10-05 18:10:18 -04:00
return Fractal::collection($contents)
2019-07-27 20:23:51 -07:00
->transformWith($this->getTransformer(FileObjectTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
}
/**
* Return the contents of a specified file for the user.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Get file contents', 'Returns raw text contents for a server file.')]
#[QueryParam('file', 'string', 'Path to the file to read.', required: true, example: '/server.properties')]
#[ScribeResponse("motd=A Minecraft Server\nserver-port=25565", description: 'File contents returned as text/plain.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not read the file.')]
public function contents(GetFileContentsRequest $request, ReadsFileContents $operation, Server $server): Response
{
2026-10-05 18:10:18 -04:00
$file = JsonValueGuard::string($request->validated('file'));
$response = $operation->read($server, $file);
2021-01-17 11:46:08 -08:00
2026-10-05 18:10:18 -04:00
Activity::event('server:file.read')->property('file', $file)->log();
2022-05-29 13:56:39 -04:00
2021-01-17 11:46:08 -08:00
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.
*
2026-10-05 18:10:18 -04:00
*
* @return array{object: string, attributes: array{url: string}}
*
* @throws Throwable
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Get file download URL', 'Returns a short-lived signed URL for downloading a server file directly from Wings.')]
#[QueryParam('file', 'string', 'Path to the file to download.', required: true, example: '/server.properties')]
#[ScribeResponse(self::SIGNED_URL_EXAMPLE, description: 'Signed download URL returned.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not create the download URL.')]
public function download(GetFileContentsRequest $request, NodeJWTService $jwtService, Server $server): array
{
2026-10-05 18:10:18 -04:00
$file = JsonValueGuard::string($request->validated('file'));
$token = $jwtService
2022-05-29 13:56:39 -04:00
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
->setUser($request->user())
2022-05-29 13:56:39 -04:00
->setClaims([
2026-10-05 18:10:18 -04:00
'file_path' => rawurldecode($file),
2022-05-29 13:56:39 -04:00
'server_uuid' => $server->uuid,
])
->setScopes(JwtScope::FileDownload)
2026-10-05 18:10:18 -04:00
->handle($server->node, $request->user()->id.$server->uuid);
2022-05-29 13:56:39 -04:00
2026-10-05 18:10:18 -04:00
Activity::event('server:file.download')->property('file', $file)->log();
return [
'object' => 'signed_url',
'attributes' => [
'url' => sprintf(
'%s/download/file?token=%s',
$server->node->getConnectionAddress(),
2021-01-23 12:09:16 -08:00
$token->toString()
),
],
];
}
2019-05-27 15:30:49 -07:00
/**
* Writes the contents of the specified file to the server.
*
2026-10-05 18:10:18 -04:00
* @throws DaemonConnectionException
2019-05-27 15:30:49 -07:00
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Write file contents', 'Writes the raw request body to a server file. This endpoint does not expect a JSON wrapper for the file contents.')]
#[QueryParam('file', 'string', 'Path to the file to write.', required: true, example: '/server.properties')]
#[ScribeResponse(status: 204, description: 'File contents written.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not write the file.')]
public function write(WriteFileContentRequest $request, WritesFileContents $operation, ListsDirectories $lister, Server $server): JsonResponse
2019-05-27 15:30:49 -07:00
{
2026-10-05 18:10:18 -04:00
$file = JsonValueGuard::string($request->validated('file'));
$permission = $this->fileExists($lister, $server, $file) ? Permissions::FileUpdate : Permissions::FileCreate;
throw_unless($request->user()->can($permission->value, $server), AuthorizationException::class);
2026-10-05 18:10:18 -04:00
$operation->write($server, $file, $request->getContent());
2021-01-17 11:46:08 -08:00
2026-10-05 18:10:18 -04:00
Activity::event('server:file.write')->property('file', $file)->log();
2019-05-27 15:30:49 -07:00
return new JsonResponse([], Response::HTTP_NO_CONTENT);
2019-05-27 15:30:49 -07:00
}
/**
* Creates a new folder on the server.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Create folder', 'Creates a folder in a server directory.')]
#[ScribeResponse(status: 204, description: 'Folder created.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not create the folder.')]
public function create(CreateFolderRequest $request, CreatesDirectories $operation, Server $server): JsonResponse
{
2026-10-05 18:10:18 -04:00
$name = JsonValueGuard::string($request->validated('name'));
$root = JsonValueGuard::string($request->validated('root', '/'));
$operation->create($server, $name, $root);
2021-01-17 11:46:08 -08:00
2022-05-29 13:56:39 -04:00
Activity::event('server:file.create-directory')
2026-10-05 18:10:18 -04:00
->property('name', $name)
->property('directory', $root)
2022-05-29 13:56:39 -04:00
->log();
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
* Renames a file on the remote machine.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Rename files', 'Renames one or more files or folders in a server directory.')]
#[ScribeResponse(status: 204, description: 'Files renamed.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not rename the files.')]
public function rename(RenameFileRequest $request, RenamesFiles $operation, Server $server): JsonResponse
{
2026-10-05 18:10:18 -04:00
$root = JsonValueGuard::nullableString($request->validated('root'));
$files = JsonValueGuard::jsonArray($request->validated('files'));
$operation->rename($server, $root, $files);
2021-01-17 11:46:08 -08:00
2022-05-29 13:56:39 -04:00
Activity::event('server:file.rename')
2026-10-05 18:10:18 -04:00
->property('directory', $root)
->property('files', $files)
2022-05-29 13:56:39 -04:00
->log();
2019-05-04 17:26:24 -07:00
return new JsonResponse([], Response::HTTP_NO_CONTENT);
2019-05-04 17:26:24 -07:00
}
/**
* Copies a file on the server.
*
2026-10-05 18:10:18 -04:00
* @throws DaemonConnectionException
2019-05-04 17:26:24 -07:00
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Copy file', 'Creates a copy of a file on the server.')]
#[ScribeResponse(status: 204, description: 'File copied.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not copy the file.')]
public function copy(CopyFileRequest $request, CopiesFiles $operation, Server $server): JsonResponse
2019-05-04 17:26:24 -07:00
{
2026-10-05 18:10:18 -04:00
$location = JsonValueGuard::string($request->validated('location'));
$operation->copy($server, $location);
2021-01-17 11:52:44 -08:00
2026-10-05 18:10:18 -04:00
Activity::event('server:file.copy')->property('file', $location)->log();
2019-05-04 17:26:24 -07:00
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
2026-10-05 18:10:18 -04:00
* @return ApiPayload
*
* @throws DaemonConnectionException
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Compress files', 'Creates an archive from one or more files or folders in a server directory.')]
#[ScribeResponse(self::ARCHIVE_OBJECT_EXAMPLE, description: 'Archive created.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not create the archive.')]
public function compress(CompressFilesRequest $request, CompressesFiles $operation, Server $server): array
{
2026-10-05 18:10:18 -04:00
$root = JsonValueGuard::nullableString($request->validated('root'));
$files = JsonValueGuard::jsonArray($request->validated('files'));
$file = $operation->compress($server,
$root,
$files
);
2022-05-29 13:56:39 -04:00
Activity::event('server:file.compress')
2026-10-05 18:10:18 -04:00
->property('directory', $root)
->property('files', $files)
2022-05-29 13:56:39 -04:00
->log();
2026-10-05 18:10:18 -04:00
return Fractal::item($file)
->transformWith($this->getTransformer(FileObjectTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
2019-05-04 17:26:24 -07:00
}
/**
2026-10-05 18:10:18 -04:00
* @throws DaemonConnectionException
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Decompress file', 'Extracts an archive in a server directory.')]
#[ScribeResponse(status: 204, description: 'Archive extracted.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not extract the archive.')]
public function decompress(DecompressFilesRequest $request, DecompressesFiles $operation, Server $server): JsonResponse
{
2026-10-05 18:10:18 -04:00
$root = JsonValueGuard::nullableString($request->validated('root'));
$file = JsonValueGuard::string($request->validated('file'));
2026-10-05 18:10:18 -04:00
$operation->decompress($server,
$root,
$file
);
2022-05-29 13:56:39 -04:00
Activity::event('server:file.decompress')
2026-10-05 18:10:18 -04:00
->property('directory', $root)
->property('files', $file)
2022-05-29 13:56:39 -04:00
->log();
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
2019-05-04 17:26:24 -07:00
/**
2020-07-11 15:37:59 -07:00
* Deletes files or folders for the server in the given root directory.
2019-05-04 17:26:24 -07:00
*
2026-10-05 18:10:18 -04:00
* @throws DaemonConnectionException
2019-05-04 17:26:24 -07:00
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Delete files', 'Deletes one or more files or folders from a server directory.')]
#[ScribeResponse(status: 204, description: 'Files deleted.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not delete the files.')]
public function delete(DeleteFileRequest $request, DeletesFiles $operation, Server $server): JsonResponse
2019-05-04 17:26:24 -07:00
{
2026-10-05 18:10:18 -04:00
$root = JsonValueGuard::nullableString($request->validated('root'));
$files = JsonValueGuard::jsonArray($request->validated('files'));
$operation->delete($server,
$root,
$files
2022-05-29 13:56:39 -04:00
);
2021-01-17 11:46:08 -08:00
2022-05-29 13:56:39 -04:00
Activity::event('server:file.delete')
2026-10-05 18:10:18 -04:00
->property('directory', $root)
->property('files', $files)
2022-05-29 13:56:39 -04:00
->log();
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
* Updates file permissions for file(s) in the given root directory.
*
2026-10-05 18:10:18 -04:00
* @throws DaemonConnectionException
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Change file permissions', 'Updates POSIX mode bits for one or more files or folders in a server directory.')]
#[ScribeResponse(status: 204, description: 'File permissions updated.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not update file permissions.')]
public function chmod(ChmodFilesRequest $request, ChangesFilePermissions $operation, Server $server): JsonResponse
{
2026-10-05 18:10:18 -04:00
$root = JsonValueGuard::nullableString($request->validated('root'));
$files = JsonValueGuard::jsonArray($request->validated('files'));
$operation->change($server,
$root,
$files
2022-05-29 13:56:39 -04:00
);
Activity::event('server:file.chmod')
->property('directory', $root)
->property('files', $files)
->log();
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
2020-12-24 09:15:03 -08:00
* Requests that a file be downloaded from a remote location by Wings.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Pull remote file', 'Requests that Wings download a remote file into a server directory.')]
#[ScribeResponse(status: 204, description: 'Remote file pull queued or completed.')]
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not pull the remote file.')]
public function pull(PullFileRequest $request, PullsFiles $operation, Server $server): JsonResponse
{
2026-10-05 18:10:18 -04:00
$url = JsonValueGuard::string($request->validated('url'));
$directory = JsonValueGuard::nullableString($request->validated('directory'));
$operation->pull($server,
$url,
$directory,
JsonValueGuard::jsonArray($request->safe()->only(['filename', 'use_header', 'foreground']))
2022-05-29 13:56:39 -04:00
);
Activity::event('server:file.pull')
2026-10-05 18:10:18 -04:00
->property('directory', $directory)
->property('url', $url)
2022-05-29 13:56:39 -04:00
->log();
2020-12-24 09:15:03 -08:00
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
* @throws DaemonConnectionException
*/
private function fileExists(ListsDirectories $lister, Server $server, string $file): bool
{
$segments = [];
foreach (explode('/', $file) as $segment) {
if ($segment === '..') {
array_pop($segments);
} elseif ($segment !== '' && $segment !== '.') {
$segments[] = $segment;
}
}
$name = array_pop($segments);
if ($name === null) {
return true;
}
try {
$contents = $lister->list($server, '/'.implode('/', $segments));
} catch (DaemonConnectionException $daemonConnectionException) {
if ($daemonConnectionException->getStatusCode() === Response::HTTP_NOT_FOUND) {
return false;
}
throw $daemonConnectionException;
}
foreach ($contents as $entry) {
if (($entry['name'] ?? null) === $name) {
return true;
}
}
return false;
}
}