2019-05-02 03:54:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Wings;
|
|
|
|
|
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;
|
2020-07-15 04:16:38 +00:00
|
|
|
use GuzzleHttp\Exception\TransferException;
|
2019-05-25 23:24:13 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException;
|
2020-07-15 04:16:38 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
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
|
|
|
{
|
|
|
|
/**
|
2019-05-25 23:24:13 +00:00
|
|
|
* Return the contents of a given file.
|
2019-05-02 03:54:40 +00:00
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param string $path
|
2019-05-25 23:24:13 +00:00
|
|
|
* @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
|
2020-07-15 04:16:38 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
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 04:16:36 +00:00
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
2019-09-06 03:33:27 +00:00
|
|
|
|
2020-07-15 04:16:38 +00:00
|
|
|
try {
|
|
|
|
$response = $this->getHttpClient()->get(
|
|
|
|
sprintf('/api/servers/%s/files/contents', $this->server->uuid),
|
|
|
|
[
|
|
|
|
'query' => ['file' => $path],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
2019-05-25 23:24:13 +00:00
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$length = (int) $response->getHeader('Content-Length')[0] ?? 0;
|
2019-05-25 23:24:13 +00:00
|
|
|
|
|
|
|
if ($notLargerThan && $length > $notLargerThan) {
|
2020-04-07 04:59:14 +00:00
|
|
|
throw new FileSizeTooLargeException;
|
2019-05-25 23:24:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
2020-07-15 04:16:38 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2019-05-02 03:54:40 +00:00
|
|
|
*/
|
|
|
|
public function putContent(string $path, string $content): ResponseInterface
|
|
|
|
{
|
2019-09-06 04:16:36 +00:00
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
2019-09-06 03:33:27 +00:00
|
|
|
|
2020-07-15 04:16:38 +00:00
|
|
|
try {
|
|
|
|
return $this->getHttpClient()->post(
|
|
|
|
sprintf('/api/servers/%s/files/write', $this->server->uuid),
|
|
|
|
[
|
|
|
|
'query' => ['file' => $path],
|
|
|
|
'body' => $content,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
2019-05-02 03:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a directory listing for a given path.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return array
|
|
|
|
*
|
2020-07-15 04:16:38 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2019-05-02 03:54:40 +00:00
|
|
|
*/
|
|
|
|
public function getDirectory(string $path): array
|
|
|
|
{
|
2019-09-06 04:16:36 +00:00
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
2019-09-06 03:33:27 +00:00
|
|
|
|
2020-07-15 04:16:38 +00:00
|
|
|
try {
|
|
|
|
$response = $this->getHttpClient()->get(
|
|
|
|
sprintf('/api/servers/%s/files/list-directory', $this->server->uuid),
|
|
|
|
[
|
|
|
|
'query' => ['directory' => $path],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
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
|
2020-07-15 04:16:38 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2019-05-02 04:45:39 +00:00
|
|
|
*/
|
|
|
|
public function createDirectory(string $name, string $path): ResponseInterface
|
|
|
|
{
|
2019-09-06 04:16:36 +00:00
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
2019-09-06 03:33:27 +00:00
|
|
|
|
2020-07-15 04:16:38 +00:00
|
|
|
try {
|
|
|
|
return $this->getHttpClient()->post(
|
|
|
|
sprintf('/api/servers/%s/files/create-directory', $this->server->uuid),
|
|
|
|
[
|
|
|
|
'json' => [
|
2020-09-25 03:27:02 +00:00
|
|
|
'name' => $name,
|
|
|
|
'path' => $path,
|
2020-07-15 04:16:38 +00:00
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
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.
|
|
|
|
*
|
2020-07-11 23:00:30 +00:00
|
|
|
* @param string|null $root
|
|
|
|
* @param array $files
|
2019-05-04 23:04:59 +00:00
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
2020-07-15 04:16:38 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2019-05-04 23:04:59 +00:00
|
|
|
*/
|
2020-07-11 23:00:30 +00:00
|
|
|
public function renameFiles(?string $root, array $files): ResponseInterface
|
2019-05-04 23:04:59 +00:00
|
|
|
{
|
2019-09-06 04:16:36 +00:00
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
2019-09-06 03:33:27 +00:00
|
|
|
|
2020-07-15 04:16:38 +00:00
|
|
|
try {
|
|
|
|
return $this->getHttpClient()->put(
|
|
|
|
sprintf('/api/servers/%s/files/rename', $this->server->uuid),
|
|
|
|
[
|
|
|
|
'json' => [
|
|
|
|
'root' => $root ?? '/',
|
|
|
|
'files' => $files,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
2019-05-04 23:04:59 +00:00
|
|
|
}
|
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
|
2020-07-15 04:16:38 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2019-05-05 00:26:24 +00:00
|
|
|
*/
|
|
|
|
public function copyFile(string $location): ResponseInterface
|
|
|
|
{
|
2019-09-06 04:16:36 +00:00
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
2019-09-06 03:33:27 +00:00
|
|
|
|
2020-07-15 04:16:38 +00:00
|
|
|
try {
|
|
|
|
return $this->getHttpClient()->post(
|
|
|
|
sprintf('/api/servers/%s/files/copy', $this->server->uuid),
|
|
|
|
[
|
|
|
|
'json' => [
|
2020-09-25 03:27:02 +00:00
|
|
|
'location' => $location,
|
2020-07-15 04:16:38 +00:00
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
2019-05-05 00:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a file or folder for the server.
|
|
|
|
*
|
2020-07-11 22:37:59 +00:00
|
|
|
* @param string|null $root
|
|
|
|
* @param array $files
|
2019-05-05 00:26:24 +00:00
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
2020-07-15 04:16:38 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2019-05-05 00:26:24 +00:00
|
|
|
*/
|
2020-07-11 22:37:59 +00:00
|
|
|
public function deleteFiles(?string $root, array $files): ResponseInterface
|
2019-05-05 00:26:24 +00:00
|
|
|
{
|
2019-09-06 04:16:36 +00:00
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
2019-09-06 03:33:27 +00:00
|
|
|
|
2020-07-15 04:16:38 +00:00
|
|
|
try {
|
|
|
|
return $this->getHttpClient()->post(
|
|
|
|
sprintf('/api/servers/%s/files/delete', $this->server->uuid),
|
|
|
|
[
|
|
|
|
'json' => [
|
|
|
|
'root' => $root ?? '/',
|
|
|
|
'files' => $files,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
2019-05-05 00:26:24 +00:00
|
|
|
}
|
2020-07-11 20:38:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compress the given files or folders in the given root.
|
|
|
|
*
|
|
|
|
* @param string|null $root
|
|
|
|
* @param array $files
|
|
|
|
* @return array
|
2020-07-15 04:16:38 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2020-07-11 20:38:49 +00:00
|
|
|
*/
|
|
|
|
public function compressFiles(?string $root, array $files): array
|
|
|
|
{
|
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
2020-07-15 04:16:38 +00:00
|
|
|
try {
|
|
|
|
$response = $this->getHttpClient()->post(
|
|
|
|
sprintf('/api/servers/%s/files/compress', $this->server->uuid),
|
|
|
|
[
|
|
|
|
'json' => [
|
|
|
|
'root' => $root ?? '/',
|
|
|
|
'files' => $files,
|
|
|
|
],
|
2020-08-07 03:25:35 +00:00
|
|
|
// Wait for up to 15 minutes for the archive to be completed when calling this endpoint
|
|
|
|
// since it will likely take quite awhile for large directories.
|
|
|
|
'timeout' => 60 * 15,
|
2020-07-15 04:16:38 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
2020-07-11 20:38:49 +00:00
|
|
|
|
|
|
|
return json_decode($response->getBody(), true);
|
|
|
|
}
|
2020-07-15 04:16:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decompresses a given archive file.
|
|
|
|
*
|
|
|
|
* @param string|null $root
|
|
|
|
* @param string $file
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
*/
|
|
|
|
public function decompressFile(?string $root, string $file): ResponseInterface
|
|
|
|
{
|
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return $this->getHttpClient()->post(
|
|
|
|
sprintf('/api/servers/%s/files/decompress', $this->server->uuid),
|
|
|
|
[
|
2020-07-15 18:29:52 +00:00
|
|
|
'json' => [
|
2020-07-15 04:16:38 +00:00
|
|
|
'root' => $root ?? '/',
|
|
|
|
'file' => $file,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
|
|
|
}
|
2020-11-29 20:49:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Chmods the given files.
|
|
|
|
*
|
|
|
|
* @param string|null $root
|
|
|
|
* @param array $files
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
*/
|
|
|
|
public function chmodFiles(?string $root, array $files): ResponseInterface
|
|
|
|
{
|
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return $this->getHttpClient()->post(
|
|
|
|
sprintf('/api/servers/%s/files/chmod', $this->server->uuid),
|
|
|
|
[
|
|
|
|
'json' => [
|
|
|
|
'root' => $root ?? '/',
|
|
|
|
'files' => $files,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
|
|
|
}
|
2020-12-24 17:15:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pulls a file from the given URL and saves it to the disk.
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param string|null $directory
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
*/
|
|
|
|
public function pull(string $url, ?string $directory): ResponseInterface
|
|
|
|
{
|
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return $this->getHttpClient()->post(
|
|
|
|
sprintf('/api/servers/%s/files/pull', $this->server->uuid),
|
|
|
|
[
|
|
|
|
'json' => ['url' => $url, 'directory' => $directory ?? '/'],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
|
|
|
}
|
2019-05-02 03:54:40 +00:00
|
|
|
}
|