Skip to content

Commit 087c41d

Browse files
committed
Add endpoint to pull a remote file down
1 parent 5d03c0d commit 087c41d

File tree

4 files changed

+68
-13
lines changed

4 files changed

+68
-13
lines changed

app/Http/Controllers/Api/Client/Servers/FileController.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Pterodactyl\Transformers\Daemon\FileObjectTransformer;
1414
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
1515
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest;
16+
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\PullFileRequest;
1617
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest;
1718
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest;
1819
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest;
@@ -143,10 +144,7 @@ public function download(GetFileContentsRequest $request, Server $server)
143144
*/
144145
public function write(WriteFileContentRequest $request, Server $server): JsonResponse
145146
{
146-
$this->fileRepository->setServer($server)->putContent(
147-
$this->encode($request->get('file')),
148-
$request->getContent()
149-
);
147+
$this->fileRepository->setServer($server)->putContent($request->get('file'), $request->getContent());
150148

151149
return new JsonResponse([], Response::HTTP_NO_CONTENT);
152150
}
@@ -284,16 +282,18 @@ public function chmod(ChmodFilesRequest $request, Server $server): JsonResponse
284282
}
285283

286284
/**
287-
* Encodes a given file name & path in a format that should work for a good majority
288-
* of file names without too much confusing logic.
285+
* Requests that a file be downloaded from a remote location by Wings.
286+
*
287+
* @param $request
288+
* @param \Pterodactyl\Models\Server $server
289+
* @return \Illuminate\Http\JsonResponse
289290
*
290-
* @param string $path
291-
* @return string
291+
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
292292
*/
293-
private function encode(string $path): string
293+
public function pull(PullFileRequest $request, Server $server): JsonResponse
294294
{
295-
return Collection::make(explode('/', rawurldecode($path)))->map(function ($value) {
296-
return rawurlencode($value);
297-
})->join('/');
295+
$this->fileRepository->setServer($server)->pull($request->input('url'), $request->input('directory'));
296+
297+
return new JsonResponse([], Response::HTTP_NO_CONTENT);
298298
}
299299
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Files;
4+
5+
use Pterodactyl\Models\Permission;
6+
use Pterodactyl\Contracts\Http\ClientPermissionsRequest;
7+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
8+
9+
class PullFileRequest extends ClientApiRequest implements ClientPermissionsRequest
10+
{
11+
/**
12+
* @return string
13+
*/
14+
public function permission(): string
15+
{
16+
return Permission::ACTION_FILE_CREATE;
17+
}
18+
19+
/**
20+
* @return string[]
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'url' => 'required|string|url',
26+
'directory' => 'sometimes|nullable|string',
27+
];
28+
}
29+
}

app/Repositories/Wings/DaemonFileRepository.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getContent(string $path, int $notLargerThan = null): string
3737
throw new DaemonConnectionException($exception);
3838
}
3939

40-
$length = (int) $response->getHeader('Content-Length')[0] ?? 0;
40+
$length = (int)$response->getHeader('Content-Length')[0] ?? 0;
4141

4242
if ($notLargerThan && $length > $notLargerThan) {
4343
throw new FileSizeTooLargeException;
@@ -297,4 +297,29 @@ public function chmodFiles(?string $root, array $files): ResponseInterface
297297
throw new DaemonConnectionException($exception);
298298
}
299299
}
300+
301+
/**
302+
* Pulls a file from the given URL and saves it to the disk.
303+
*
304+
* @param string $url
305+
* @param string|null $directory
306+
* @return \Psr\Http\Message\ResponseInterface
307+
*
308+
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
309+
*/
310+
public function pull(string $url, ?string $directory): ResponseInterface
311+
{
312+
Assert::isInstanceOf($this->server, Server::class);
313+
314+
try {
315+
return $this->getHttpClient()->post(
316+
sprintf('/api/servers/%s/files/pull', $this->server->uuid),
317+
[
318+
'json' => ['url' => $url, 'directory' => $directory ?? '/'],
319+
]
320+
);
321+
} catch (TransferException $exception) {
322+
throw new DaemonConnectionException($exception);
323+
}
324+
}
300325
}

routes/api-client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
Route::post('/delete', 'Servers\FileController@delete');
6767
Route::post('/create-folder', 'Servers\FileController@create');
6868
Route::post('/chmod', 'Servers\FileController@chmod');
69+
Route::post('/pull', 'Servers\FileController@pull');
6970
Route::get('/upload', 'Servers\FileUploadController');
7071
});
7172

0 commit comments

Comments
 (0)