You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pixelfed/app/Services/Internal/SoftwareUpdateService.php

94 lines
2.6 KiB
PHTML

<?php
namespace App\Services\Internal;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
9 months ago
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
class SoftwareUpdateService
{
const CACHE_KEY = 'pf:services:software-update:';
public static function cacheKey(): string
{
9 months ago
return self::CACHE_KEY.'latest:v1.0.0';
}
public static function get($flushCache = false): array
{
$curVersion = config('pixelfed.version');
if ($flushCache) {
Cache::forget(self::cacheKey());
Cache::forget('api:nodeinfo');
}
9 months ago
$versions = Cache::remember(self::cacheKey(), 1800, function () {
return self::fetchLatest();
});
9 months ago
if (! $versions || ! isset($versions['latest'], $versions['latest']['version'])) {
$hideWarning = (bool) config('instance.software-update.disable_failed_warning');
9 months ago
return [
'current' => $curVersion,
'latest' => [
'version' => null,
'published_at' => null,
'url' => null,
],
9 months ago
'running_latest' => $hideWarning ? true : null,
'ahead_of_latest' => false,
];
}
$latestVersion = $versions['latest']['version'];
$cmp = self::compareVersions($curVersion, $latestVersion);
return [
'current' => $curVersion,
'latest' => [
'version' => $latestVersion,
'published_at' => $versions['latest']['published_at'],
'url' => $versions['latest']['url'],
],
'running_latest' => $cmp >= 0,
'ahead_of_latest' => $cmp > 0,
];
}
public static function compareVersions($current, $latest): int
{
return version_compare(
self::normalizeVersion($current),
self::normalizeVersion($latest)
);
}
public static function normalizeVersion($version): string
{
return ltrim(trim((string) $version), 'vV');
}
public static function fetchLatest()
{
try {
$res = Http::withOptions(['allow_redirects' => false])
->timeout(5)
->connectTimeout(5)
->retry(2, 500)
->get('https://versions.pixelfed.org/versions.json');
} catch (RequestException|ConnectionException|\Exception) {
return;
}
9 months ago
if (! $res->ok()) {
return;
}
return $res->json();
}
}