2017-08-26 23:08:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Daemon;
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
use stdClass;
|
2019-05-02 04:45:39 +00:00
|
|
|
use RuntimeException;
|
2018-01-06 00:27:47 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
class FileRepository extends BaseRepository implements FileRepositoryInterface
|
|
|
|
{
|
2018-01-06 00:27:47 +00:00
|
|
|
/**
|
|
|
|
* Return stat information for a given file.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return \stdClass
|
|
|
|
*
|
2018-05-13 16:19:35 +00:00
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
2018-01-06 00:27:47 +00:00
|
|
|
*/
|
|
|
|
public function getFileStat(string $path): stdClass
|
2017-08-26 23:08:11 +00:00
|
|
|
{
|
2018-05-13 15:38:31 +00:00
|
|
|
$file = str_replace('\\', '/', pathinfo($path));
|
2017-08-26 23:08:11 +00:00
|
|
|
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
|
|
|
|
|
|
|
|
$response = $this->getHttpClient()->request('GET', sprintf(
|
2017-10-01 02:00:24 +00:00
|
|
|
'server/file/stat/%s',
|
2017-08-26 23:08:11 +00:00
|
|
|
rawurlencode($file['dirname'] . $file['basename'])
|
|
|
|
));
|
|
|
|
|
|
|
|
return json_decode($response->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* Return the contents of a given file if it can be edited in the Panel.
|
|
|
|
*
|
|
|
|
* @param string $path
|
2018-01-21 18:31:41 +00:00
|
|
|
* @return string
|
2018-01-06 00:27:47 +00:00
|
|
|
*
|
2018-05-13 16:19:35 +00:00
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-21 18:31:41 +00:00
|
|
|
public function getContent(string $path): string
|
2017-08-26 23:08:11 +00:00
|
|
|
{
|
2018-05-13 15:38:31 +00:00
|
|
|
$file = str_replace('\\', '/', pathinfo($path));
|
2017-08-26 23:08:11 +00:00
|
|
|
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
|
|
|
|
|
|
|
|
$response = $this->getHttpClient()->request('GET', sprintf(
|
2017-10-01 02:00:24 +00:00
|
|
|
'server/file/f/%s',
|
2017-08-26 23:08:11 +00:00
|
|
|
rawurlencode($file['dirname'] . $file['basename'])
|
|
|
|
));
|
|
|
|
|
2017-09-03 21:32:52 +00:00
|
|
|
return object_get(json_decode($response->getBody()), 'content');
|
2017-08-26 23:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* Save new contents to a given file.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $content
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
*
|
2018-05-13 16:19:35 +00:00
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
public function putContent(string $path, string $content): ResponseInterface
|
2017-08-26 23:08:11 +00:00
|
|
|
{
|
2018-05-13 15:38:31 +00:00
|
|
|
$file = str_replace('\\', '/', pathinfo($path));
|
2017-08-26 23:08:11 +00:00
|
|
|
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
|
|
|
|
|
2017-10-01 02:00:24 +00:00
|
|
|
return $this->getHttpClient()->request('POST', 'server/file/save', [
|
2017-08-26 23:08:11 +00:00
|
|
|
'json' => [
|
|
|
|
'path' => rawurlencode($file['dirname'] . $file['basename']),
|
|
|
|
'content' => $content,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* Return a directory listing for a given path.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return array
|
|
|
|
*
|
2018-05-13 16:19:35 +00:00
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
public function getDirectory(string $path): array
|
2017-08-26 23:08:11 +00:00
|
|
|
{
|
2018-01-06 00:27:47 +00:00
|
|
|
$response = $this->getHttpClient()->request('GET', sprintf('server/directory/%s', rawurlencode($path)));
|
2017-08-26 23:08:11 +00:00
|
|
|
|
2018-08-07 06:14:13 +00:00
|
|
|
return json_decode($response->getBody());
|
2017-08-26 23:08:11 +00:00
|
|
|
}
|
2019-05-02 04:45:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new directory for the server in the given $path.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $path
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
*
|
|
|
|
* @throws \RuntimeException
|
|
|
|
*/
|
|
|
|
public function createDirectory(string $name, string $path): ResponseInterface
|
|
|
|
{
|
|
|
|
throw new RuntimeException('Not implemented.');
|
|
|
|
}
|
2017-08-26 23:08:11 +00:00
|
|
|
}
|