'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. * * @throws DaemonConnectionException */ #[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): ApiResourceCollection { $directory = $this->path($request, 'directory') ?? '/'; $contents = $operation->list($server, $directory); return FileObjectResource::collection($contents); } /** * Return the contents of a specified file for the user. * * @throws Throwable */ #[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 { $file = $request->string('file')->toString(); $response = $operation->read($server, $file); Activity::event('server:file.read')->property('file', $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{object: string, attributes: array{url: string}} * * @throws Throwable */ #[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, NodeJwt $jwt, Server $server): array { $file = $request->string('file')->toString(); $token = $jwt->issue( $server->node, JwtScope::FileDownload, identifiedBy: $request->user()->id.$server->uuid, expiresAt: CarbonImmutable::now()->addMinutes(15), user: $request->user(), claims: ['file_path' => rawurldecode($file), 'server_uuid' => $server->uuid], ); Activity::event('server:file.download')->property('file', $file)->log(); return [ 'object' => 'signed_url', 'attributes' => [ 'url' => sprintf( '%s/download/file?token=%s', $server->node->getConnectionAddress(), $token->toString() ), ], ]; } /** * Writes the contents of the specified file to the server. * * @throws DaemonConnectionException */ #[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 { $file = $request->string('file')->toString(); $permission = $this->fileExists($lister, $server, $file) ? Permissions::FileUpdate : Permissions::FileCreate; throw_unless($request->user()->can($permission->value, $server), AuthorizationException::class); $operation->write($server, $file, $request->getContent()); return new JsonResponse([], Response::HTTP_NO_CONTENT); } /** * Creates a new folder on the server. * * @throws Throwable */ #[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 { $operation->create($server, $request->string('name')->toString(), $this->path($request, 'root') ?? '/'); return new JsonResponse([], Response::HTTP_NO_CONTENT); } /** * Renames a file on the remote machine. * * @throws Throwable */ #[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 { $operation->rename($server, $this->path($request, 'root'), $request->safe()->array('files')); return new JsonResponse([], Response::HTTP_NO_CONTENT); } /** * Copies a file on the server. * * @throws DaemonConnectionException */ #[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 { $operation->copy($server, $request->string('location')->toString()); return new JsonResponse([], Response::HTTP_NO_CONTENT); } /** * @throws DaemonConnectionException */ #[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): FileObjectResource { $file = $operation->compress($server, $this->path($request, 'root'), $request->safe()->array('files')); return FileObjectResource::make($file); } /** * @throws DaemonConnectionException */ #[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 { $operation->decompress($server, $this->path($request, 'root'), $request->string('file')->toString()); return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT); } /** * Deletes files or folders for the server in the given root directory. * * @throws DaemonConnectionException */ #[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 { $operation->delete($server, $this->path($request, 'root'), $request->safe()->array('files')); return new JsonResponse([], Response::HTTP_NO_CONTENT); } /** * Updates file permissions for file(s) in the given root directory. * * @throws DaemonConnectionException */ #[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 { $operation->change($server, $this->path($request, 'root'), $request->safe()->array('files')); return new JsonResponse([], Response::HTTP_NO_CONTENT); } /** * Requests that a file be downloaded from a remote location by Wings. * * @throws Throwable */ #[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 { $url = $request->string('url')->toString(); $directory = $this->path($request, 'directory'); $options = $request->safe()->has('filename') ? ['filename' => $this->path($request, 'filename')] : []; foreach (['use_header', 'foreground'] as $flag) { if ($request->safe()->has($flag)) { $options[$flag] = $request->boolean($flag); } } $operation->pull($server, $url, $directory, $options); return new JsonResponse([], Response::HTTP_NO_CONTENT); } /** * File routes skip TrimStrings, so a path made only of spaces is kept as given; * only a missing or null value is read as absent. */ private function path(Request $request, string $key): ?string { return $request->input($key) === null ? null : $request->string($key)->toString(); } /** * @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; } }