misc_pterodactyl-panel/app/Repositories/Daemon/FileRepository.php

117 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Repositories\Daemon;
use stdClass;
use Psr\Http\Message\ResponseInterface;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
class FileRepository extends BaseRepository implements FileRepositoryInterface
{
/**
* Return stat information for a given file.
*
* @param string $path
* @return \stdClass
*
2018-05-13 16:19:35 +00:00
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getFileStat(string $path): stdClass
{
$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',
rawurlencode($file['dirname'] . $file['basename'])
));
return json_decode($response->getBody());
}
/**
* Return the contents of a given file if it can be edited in the Panel.
*
* @param string $path
* @return string
*
2018-05-13 16:19:35 +00:00
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getContent(string $path): string
{
$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',
rawurlencode($file['dirname'] . $file['basename'])
));
return object_get(json_decode($response->getBody()), 'content');
}
/**
* 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
*/
public function putContent(string $path, string $content): ResponseInterface
{
$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', [
'json' => [
'path' => rawurlencode($file['dirname'] . $file['basename']),
'content' => $content,
],
]);
}
/**
* 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
*/
public function getDirectory(string $path): array
{
$response = $this->getHttpClient()->request('GET', sprintf('server/directory/%s', rawurlencode($path)));
$contents = json_decode($response->getBody());
$files = $folders = [];
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,
];
}
}