diff --git a/app/Http/Controllers/Api/Client/Servers/FileController.php b/app/Http/Controllers/Api/Client/Servers/FileController.php index 961b7cd02..317115b29 100644 --- a/app/Http/Controllers/Api/Client/Servers/FileController.php +++ b/app/Http/Controllers/Api/Client/Servers/FileController.php @@ -13,6 +13,7 @@ use Pterodactyl\Repositories\Wings\DaemonFileRepository; use Pterodactyl\Transformers\Daemon\FileObjectTransformer; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; 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\DeleteFileRequest; use Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest; @@ -143,10 +144,7 @@ class FileController extends ClientApiController */ public function write(WriteFileContentRequest $request, Server $server): JsonResponse { - $this->fileRepository->setServer($server)->putContent( - $this->encode($request->get('file')), - $request->getContent() - ); + $this->fileRepository->setServer($server)->putContent($request->get('file'), $request->getContent()); return new JsonResponse([], Response::HTTP_NO_CONTENT); } @@ -284,16 +282,18 @@ class FileController extends ClientApiController } /** - * Encodes a given file name & path in a format that should work for a good majority - * of file names without too much confusing logic. + * Requests that a file be downloaded from a remote location by Wings. * - * @param string $path - * @return string + * @param $request + * @param \Pterodactyl\Models\Server $server + * @return \Illuminate\Http\JsonResponse + * + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ - private function encode(string $path): string + public function pull(PullFileRequest $request, Server $server): JsonResponse { - return Collection::make(explode('/', rawurldecode($path)))->map(function ($value) { - return rawurlencode($value); - })->join('/'); + $this->fileRepository->setServer($server)->pull($request->input('url'), $request->input('directory')); + + return new JsonResponse([], Response::HTTP_NO_CONTENT); } } diff --git a/app/Http/Requests/Api/Client/Servers/Files/PullFileRequest.php b/app/Http/Requests/Api/Client/Servers/Files/PullFileRequest.php new file mode 100644 index 000000000..02a2fd376 --- /dev/null +++ b/app/Http/Requests/Api/Client/Servers/Files/PullFileRequest.php @@ -0,0 +1,29 @@ + 'required|string|url', + 'directory' => 'sometimes|nullable|string', + ]; + } +} diff --git a/app/Repositories/Wings/DaemonFileRepository.php b/app/Repositories/Wings/DaemonFileRepository.php index c36a8abb0..da6cb7ae2 100644 --- a/app/Repositories/Wings/DaemonFileRepository.php +++ b/app/Repositories/Wings/DaemonFileRepository.php @@ -37,7 +37,7 @@ class DaemonFileRepository extends DaemonRepository throw new DaemonConnectionException($exception); } - $length = (int) $response->getHeader('Content-Length')[0] ?? 0; + $length = (int)$response->getHeader('Content-Length')[0] ?? 0; if ($notLargerThan && $length > $notLargerThan) { throw new FileSizeTooLargeException; @@ -297,4 +297,29 @@ class DaemonFileRepository extends DaemonRepository throw new DaemonConnectionException($exception); } } + + /** + * Pulls a file from the given URL and saves it to the disk. + * + * @param string $url + * @param string|null $directory + * @return \Psr\Http\Message\ResponseInterface + * + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException + */ + public function pull(string $url, ?string $directory): ResponseInterface + { + Assert::isInstanceOf($this->server, Server::class); + + try { + return $this->getHttpClient()->post( + sprintf('/api/servers/%s/files/pull', $this->server->uuid), + [ + 'json' => ['url' => $url, 'directory' => $directory ?? '/'], + ] + ); + } catch (TransferException $exception) { + throw new DaemonConnectionException($exception); + } + } } diff --git a/routes/api-client.php b/routes/api-client.php index 320c8ec55..62cad21c1 100644 --- a/routes/api-client.php +++ b/routes/api-client.php @@ -66,6 +66,7 @@ Route::group(['prefix' => '/servers/{server}', 'middleware' => [AuthenticateServ Route::post('/delete', 'Servers\FileController@delete'); Route::post('/create-folder', 'Servers\FileController@create'); Route::post('/chmod', 'Servers\FileController@chmod'); + Route::post('/pull', 'Servers\FileController@pull'); Route::get('/upload', 'Servers\FileUploadController'); });