2019-03-17 00:10:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2019-05-02 03:54:40 +00:00
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
2019-03-17 00:10:04 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
2019-05-02 03:54:40 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest;
|
2019-03-17 00:10:04 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DownloadFileRequest;
|
|
|
|
|
|
|
|
class FileController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Cache\Factory
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
2019-05-02 03:54:40 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $fileRepository;
|
|
|
|
|
2019-03-17 00:10:04 +00:00
|
|
|
/**
|
|
|
|
* FileController constructor.
|
|
|
|
*
|
2019-05-02 03:54:40 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface $fileRepository
|
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache
|
2019-03-17 00:10:04 +00:00
|
|
|
*/
|
2019-05-02 03:54:40 +00:00
|
|
|
public function __construct(FileRepositoryInterface $fileRepository, CacheRepository $cache)
|
2019-03-17 00:10:04 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->cache = $cache;
|
2019-05-02 03:54:40 +00:00
|
|
|
$this->fileRepository = $fileRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a listing of files in a given directory.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function listDirectory(ListFilesRequest $request): JsonResponse
|
|
|
|
{
|
|
|
|
return JsonResponse::create([
|
|
|
|
'contents' => $this->fileRepository->setServer($request->getModel(Server::class))->getDirectory(
|
|
|
|
$request->get('directory') ?? '/'
|
|
|
|
),
|
|
|
|
]);
|
2019-03-17 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function download(DownloadFileRequest $request): JsonResponse
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\Server $server */
|
|
|
|
$server = $request->getModel(Server::class);
|
|
|
|
$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]);
|
|
|
|
}
|
|
|
|
}
|