api(application): add 'version' endpoint
This commit is contained in:
parent
545cc3bbd2
commit
12c68961db
5 changed files with 147 additions and 75 deletions
|
@ -6,6 +6,7 @@ use Exception;
|
|||
use GuzzleHttp\Client;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
||||
use Pterodactyl\Exceptions\Service\Helper\CdnVersionFetchingException;
|
||||
|
||||
|
@ -16,17 +17,17 @@ class SoftwareVersionService
|
|||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $result;
|
||||
private static array $result;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Cache\Repository
|
||||
*/
|
||||
protected $cache;
|
||||
protected CacheRepository $cache;
|
||||
|
||||
/**
|
||||
* @var \GuzzleHttp\Client
|
||||
*/
|
||||
protected $client;
|
||||
protected Client $client;
|
||||
|
||||
/**
|
||||
* SoftwareVersionService constructor.
|
||||
|
@ -44,46 +45,36 @@ class SoftwareVersionService
|
|||
self::$result = $this->cacheVersionData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current version of the panel that is being used.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVersion(): string
|
||||
{
|
||||
return config()->get('app.version');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest version of the panel from the CDN servers.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPanel()
|
||||
public function getLatestPanel(): string
|
||||
{
|
||||
return Arr::get(self::$result, 'panel') ?? 'error';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest version of the daemon from the CDN servers.
|
||||
* Get the latest version of wings from the CDN servers.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDaemon()
|
||||
public function getLatestWings(): string
|
||||
{
|
||||
return Arr::get(self::$result, 'wings') ?? 'error';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL to the discord server.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDiscord()
|
||||
{
|
||||
return Arr::get(self::$result, 'discord') ?? 'https://pterodactyl.io/discord';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL for donations.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDonations()
|
||||
{
|
||||
return Arr::get(self::$result, 'donations') ?? 'https://paypal.me/PterodactylSoftware';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the current version of the panel is the latest.
|
||||
*
|
||||
|
@ -91,26 +82,29 @@ class SoftwareVersionService
|
|||
*/
|
||||
public function isLatestPanel()
|
||||
{
|
||||
if (config()->get('app.version') === 'canary') {
|
||||
$version = $this->getVersion();
|
||||
if ($version === 'canary') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return version_compare(config()->get('app.version'), $this->getPanel()) >= 0;
|
||||
return version_compare($version, $this->getLatestPanel()) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a passed daemon version string is the latest.
|
||||
* Determine if a passed wings version is the latest.
|
||||
*
|
||||
* @param string $version
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLatestDaemon($version)
|
||||
public function isLatestWings(string $version)
|
||||
{
|
||||
if ($version === '0.0.0-canary') {
|
||||
// If the version is 'canary' or starts with 'dev-', mark it as the latest.
|
||||
if ($version === 'canary' || Str::startsWith($version, 'dev-')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return version_compare($version, $this->getDaemon()) >= 0;
|
||||
return version_compare($version, $this->getLatestWings()) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -124,14 +118,74 @@ class SoftwareVersionService
|
|||
try {
|
||||
$response = $this->client->request('GET', config()->get('pterodactyl.cdn.url'));
|
||||
|
||||
if ($response->getStatusCode() === 200) {
|
||||
return json_decode($response->getBody(), true);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new CdnVersionFetchingException;
|
||||
}
|
||||
|
||||
throw new CdnVersionFetchingException;
|
||||
return json_decode($response->getBody(), true);
|
||||
} catch (Exception $exception) {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Return version information for the footer.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function versionData(): array
|
||||
{
|
||||
return $this->cache->remember('git-version', 5, function () {
|
||||
$configVersion = config()->get('app.version');
|
||||
|
||||
if (file_exists(base_path('.git/HEAD'))) {
|
||||
$head = explode(' ', file_get_contents(base_path('.git/HEAD')));
|
||||
|
||||
if (array_key_exists(1, $head)) {
|
||||
$path = base_path('.git/' . trim($head[1]));
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($path) && file_exists($path)) {
|
||||
return [
|
||||
'version' => substr(file_get_contents($path), 0, 8),
|
||||
'is_git' => true,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'version' => $configVersion,
|
||||
'is_git' => false,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* ?
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getVersionData(): array
|
||||
{
|
||||
$versionData = $this->versionData();
|
||||
if ($versionData['is_git']) {
|
||||
$git = $versionData['version'];
|
||||
} else {
|
||||
$git = null;
|
||||
}
|
||||
|
||||
return [
|
||||
'panel' => [
|
||||
'current' => $this->getVersion(),
|
||||
'latest' => $this->getLatestPanel(),
|
||||
],
|
||||
|
||||
'wings' => [
|
||||
'latest' => $this->getLatestWings(),
|
||||
],
|
||||
|
||||
'git' => $git,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue