2017-08-26 23:08:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Daemon;
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
use stdClass;
|
|
|
|
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
|
|
|
{
|
|
|
|
$file = pathinfo($path);
|
|
|
|
$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
|
|
|
{
|
|
|
|
$file = pathinfo($path);
|
|
|
|
$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
|
|
|
{
|
|
|
|
$file = pathinfo($path);
|
|
|
|
$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
|
|
|
|
|
|
|
$contents = json_decode($response->getBody());
|
2018-01-06 00:27:47 +00:00
|
|
|
$files = $folders = [];
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
foreach ($contents as $value) {
|
|
|
|
if ($value->directory) {
|
|
|
|
array_push($folders, [
|
|
|
|
'entry' => $value->name,
|
|
|
|
'directory' => trim($path, '/'),
|
|
|
|
'size' => null,
|
|
|
|
'date' => strtotime($value->modified),
|
|
|
|
'mime' => $value->mime,
|
|
|
|
]);
|
|
|
|
} elseif ($value->file) {
|
|
|
|
array_push($files, [
|
|
|
|
'entry' => $value->name,
|
|
|
|
'directory' => trim($path, '/'),
|
|
|
|
'extension' => pathinfo($value->name, PATHINFO_EXTENSION),
|
|
|
|
'size' => human_readable($value->size),
|
|
|
|
'date' => strtotime($value->modified),
|
|
|
|
'mime' => $value->mime,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'files' => $files,
|
|
|
|
'folders' => $folders,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|