2019-05-02 03:54:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Wings;
|
|
|
|
|
|
|
|
use stdClass;
|
2019-05-25 23:24:13 +00:00
|
|
|
use Exception;
|
2019-09-06 03:33:27 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
use Pterodactyl\Models\Server;
|
2019-05-02 03:54:40 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2019-05-25 23:24:13 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException;
|
2019-05-02 03:54:40 +00:00
|
|
|
|
2019-09-06 03:33:27 +00:00
|
|
|
class DaemonFileRepository extends DaemonRepository
|
2019-05-02 03:54:40 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Return stat information for a given file.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return \stdClass
|
|
|
|
*
|
2019-05-25 23:24:13 +00:00
|
|
|
* @throws \Exception
|
2019-05-02 03:54:40 +00:00
|
|
|
* @throws \GuzzleHttp\Exception\TransferException
|
|
|
|
*/
|
|
|
|
public function getFileStat(string $path): stdClass
|
|
|
|
{
|
2019-05-25 23:24:13 +00:00
|
|
|
throw new Exception('Function not implemented.');
|
2019-05-02 03:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-25 23:24:13 +00:00
|
|
|
* Return the contents of a given file.
|
2019-05-02 03:54:40 +00:00
|
|
|
*
|
2019-05-25 23:24:13 +00:00
|
|
|
* @param string $path
|
|
|
|
* @param int|null $notLargerThan the maximum content length in bytes
|
2019-05-02 03:54:40 +00:00
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @throws \GuzzleHttp\Exception\TransferException
|
2019-05-25 23:24:13 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException
|
2019-05-02 03:54:40 +00:00
|
|
|
*/
|
2019-05-25 23:24:13 +00:00
|
|
|
public function getContent(string $path, int $notLargerThan = null): string
|
2019-05-02 03:54:40 +00:00
|
|
|
{
|
2019-09-06 03:33:27 +00:00
|
|
|
Assert::isInstanceOf(Server::class, $this->server);
|
|
|
|
|
2019-05-25 23:24:13 +00:00
|
|
|
$response = $this->getHttpClient()->get(
|
2019-09-06 03:33:27 +00:00
|
|
|
sprintf('/api/servers/%s/files/contents', $this->server->uuid),
|
2019-05-25 23:24:13 +00:00
|
|
|
[
|
|
|
|
'query' => ['file' => $path],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$length = (int) $response->getHeader('Content-Length')[0] ?? 0;
|
|
|
|
|
|
|
|
if ($notLargerThan && $length > $notLargerThan) {
|
|
|
|
throw new FileSizeTooLargeException(
|
|
|
|
trans('server.files.exceptions.max_size')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response->getBody()->__toString();
|
2019-05-02 03:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-25 23:24:13 +00:00
|
|
|
* Save new contents to a given file. This works for both creating and updating
|
|
|
|
* a file.
|
2019-05-02 03:54:40 +00:00
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $content
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
*
|
|
|
|
* @throws \GuzzleHttp\Exception\TransferException
|
|
|
|
*/
|
|
|
|
public function putContent(string $path, string $content): ResponseInterface
|
|
|
|
{
|
2019-09-06 03:33:27 +00:00
|
|
|
Assert::isInstanceOf(Server::class, $this->server);
|
|
|
|
|
2019-05-25 23:24:13 +00:00
|
|
|
return $this->getHttpClient()->post(
|
2019-09-06 03:33:27 +00:00
|
|
|
sprintf('/api/servers/%s/files/write', $this->server->uuid),
|
2019-05-25 23:24:13 +00:00
|
|
|
[
|
|
|
|
'query' => ['file' => $path],
|
|
|
|
'body' => $content,
|
|
|
|
]
|
|
|
|
);
|
2019-05-02 03:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a directory listing for a given path.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \GuzzleHttp\Exception\TransferException
|
|
|
|
*/
|
|
|
|
public function getDirectory(string $path): array
|
|
|
|
{
|
2019-09-06 03:33:27 +00:00
|
|
|
Assert::isInstanceOf(Server::class, $this->server);
|
|
|
|
|
2019-05-02 03:54:40 +00:00
|
|
|
$response = $this->getHttpClient()->get(
|
2019-09-06 03:33:27 +00:00
|
|
|
sprintf('/api/servers/%s/files/list-directory', $this->server->uuid),
|
2019-05-25 23:24:13 +00:00
|
|
|
[
|
|
|
|
'query' => ['directory' => $path],
|
|
|
|
]
|
2019-05-02 03:54:40 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return json_decode($response->getBody(), true);
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
public function createDirectory(string $name, string $path): ResponseInterface
|
|
|
|
{
|
2019-09-06 03:33:27 +00:00
|
|
|
Assert::isInstanceOf(Server::class, $this->server);
|
|
|
|
|
2019-05-02 04:45:39 +00:00
|
|
|
return $this->getHttpClient()->post(
|
2019-09-06 03:33:27 +00:00
|
|
|
sprintf('/api/servers/%s/files/create-directory', $this->server->uuid),
|
2019-05-02 04:45:39 +00:00
|
|
|
[
|
2019-05-02 04:58:35 +00:00
|
|
|
'json' => [
|
|
|
|
'name' => $name,
|
|
|
|
'path' => $path,
|
|
|
|
],
|
2019-05-02 04:45:39 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2019-05-04 23:04:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renames or moves a file on the remote machine.
|
|
|
|
*
|
|
|
|
* @param string $from
|
|
|
|
* @param string $to
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
*/
|
|
|
|
public function renameFile(string $from, string $to): ResponseInterface
|
|
|
|
{
|
2019-09-06 03:33:27 +00:00
|
|
|
Assert::isInstanceOf(Server::class, $this->server);
|
|
|
|
|
2019-05-04 23:04:59 +00:00
|
|
|
return $this->getHttpClient()->put(
|
2019-09-06 03:33:27 +00:00
|
|
|
sprintf('/api/servers/%s/files/rename', $this->server->uuid),
|
2019-05-04 23:04:59 +00:00
|
|
|
[
|
|
|
|
'json' => [
|
|
|
|
'rename_from' => $from,
|
|
|
|
'rename_to' => $to,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2019-05-05 00:26:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy a given file and give it a unique name.
|
|
|
|
*
|
|
|
|
* @param string $location
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
*/
|
|
|
|
public function copyFile(string $location): ResponseInterface
|
|
|
|
{
|
2019-09-06 03:33:27 +00:00
|
|
|
Assert::isInstanceOf(Server::class, $this->server);
|
|
|
|
|
2019-05-05 00:26:24 +00:00
|
|
|
return $this->getHttpClient()->post(
|
2019-09-06 03:33:27 +00:00
|
|
|
sprintf('/api/servers/%s/files/copy', $this->server->uuid),
|
2019-05-05 00:26:24 +00:00
|
|
|
[
|
|
|
|
'json' => [
|
|
|
|
'location' => $location,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a file or folder for the server.
|
|
|
|
*
|
|
|
|
* @param string $location
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
*/
|
|
|
|
public function deleteFile(string $location): ResponseInterface
|
|
|
|
{
|
2019-09-06 03:33:27 +00:00
|
|
|
Assert::isInstanceOf(Server::class, $this->server);
|
|
|
|
|
2019-05-05 00:26:24 +00:00
|
|
|
return $this->getHttpClient()->post(
|
2019-09-06 03:33:27 +00:00
|
|
|
sprintf('/api/servers/%s/files/delete', $this->server->uuid),
|
2019-05-05 00:26:24 +00:00
|
|
|
[
|
|
|
|
'json' => [
|
|
|
|
'location' => $location,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2019-05-02 03:54:40 +00:00
|
|
|
}
|