Proxy file downloads through the panel rather than having to get creative with download tokens
This commit is contained in:
parent
78ccdf93b6
commit
0b9c6bd21d
4 changed files with 78 additions and 43 deletions
|
@ -2,15 +2,12 @@
|
|||
|
||||
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Illuminate\Http\Response;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use GuzzleHttp\Exception\TransferException;
|
||||
use Illuminate\Contracts\Routing\ResponseFactory;
|
||||
use Pterodactyl\Repositories\Wings\DaemonFileRepository;
|
||||
use Pterodactyl\Transformers\Daemon\FileObjectTransformer;
|
||||
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
||||
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
||||
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest;
|
||||
|
@ -18,34 +15,35 @@ 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;
|
||||
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\WriteFileContentRequest;
|
||||
|
||||
class FileController extends ClientApiController
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Cache\Factory
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Wings\DaemonFileRepository
|
||||
*/
|
||||
private $fileRepository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Routing\ResponseFactory
|
||||
*/
|
||||
private $responseFactory;
|
||||
|
||||
/**
|
||||
* FileController constructor.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory
|
||||
* @param \Pterodactyl\Repositories\Wings\DaemonFileRepository $fileRepository
|
||||
* @param \Illuminate\Contracts\Cache\Repository $cache
|
||||
*/
|
||||
public function __construct(DaemonFileRepository $fileRepository, CacheRepository $cache)
|
||||
{
|
||||
public function __construct(
|
||||
ResponseFactory $responseFactory,
|
||||
DaemonFileRepository $fileRepository
|
||||
) {
|
||||
parent::__construct();
|
||||
|
||||
$this->cache = $cache;
|
||||
$this->fileRepository = $fileRepository;
|
||||
$this->responseFactory = $responseFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -91,6 +89,39 @@ class FileController extends ClientApiController
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest $request
|
||||
* @param \Pterodactyl\Models\Server $server
|
||||
* @return \Symfony\Component\HttpFoundation\StreamedResponse
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function download(GetFileContentsRequest $request, Server $server)
|
||||
{
|
||||
set_time_limit(0);
|
||||
|
||||
$request = $this->fileRepository->setServer($server)->streamContent(
|
||||
$request->get('file')
|
||||
);
|
||||
|
||||
$body = $request->getBody();
|
||||
|
||||
preg_match('/filename=(?<name>.*)$/', $request->getHeaderLine('Content-Disposition'), $matches);
|
||||
|
||||
return $this->responseFactory->streamDownload(
|
||||
function () use ($body) {
|
||||
while (! $body->eof()) {
|
||||
echo $body->read(128);
|
||||
}
|
||||
},
|
||||
$matches['name'] ?? 'download',
|
||||
[
|
||||
'Content-Type' => $request->getHeaderLine('Content-Type'),
|
||||
'Content-Length' => $request->getHeaderLine('Content-Length'),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the contents of the specified file to the server.
|
||||
*
|
||||
|
@ -171,27 +202,4 @@ class FileController extends ClientApiController
|
|||
|
||||
return Response::create('', Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure a reference to a file to download in the cache so that when the
|
||||
* user hits the Daemon and it verifies with the Panel they'll actually be able
|
||||
* to download that file.
|
||||
*
|
||||
* Returns the token that needs to be used when downloading the file.
|
||||
*
|
||||
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest $request
|
||||
* @param \Pterodactyl\Models\Server $server
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function download(DownloadFileRequest $request, Server $server): JsonResponse
|
||||
{
|
||||
$token = Uuid::uuid4()->toString();
|
||||
|
||||
$this->cache->put(
|
||||
'Server:Downloads:' . $token, ['server' => $server->uuid, 'path' => $request->route()->parameter('file')], Carbon::now()->addMinutes(5)
|
||||
);
|
||||
|
||||
return JsonResponse::create(['token' => $token]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue