2017-08-26 23:08:11 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-08-26 23:08:11 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Daemon;
|
|
|
|
|
|
|
|
use Webmozart\Assert\Assert;
|
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
|
|
|
|
{
|
|
|
|
public function getFileStat($path)
|
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($path, 'First argument passed to getStat must be a non-empty string, received %s.');
|
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getContent($path)
|
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($path, 'First argument passed to getContent must be a non-empty string, received %s.');
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function putContent($path, $content)
|
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($path, 'First argument passed to putContent must be a non-empty string, received %s.');
|
|
|
|
Assert::string($content, 'Second argument passed to putContent must be a string, received %s.');
|
|
|
|
|
|
|
|
$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,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getDirectory($path)
|
|
|
|
{
|
|
|
|
Assert::string($path, 'First argument passed to getDirectory must be a string, received %s.');
|
|
|
|
|
|
|
|
$response = $this->getHttpClient()->request('GET', sprintf(
|
2017-10-01 02:00:24 +00:00
|
|
|
'server/directory/%s',
|
2017-08-26 23:08:11 +00:00
|
|
|
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,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|