1
0

SoftwareUpdateService.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Services\Internal;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Http\Client\ConnectionException;
  6. use Illuminate\Http\Client\RequestException;
  7. class SoftwareUpdateService
  8. {
  9. const CACHE_KEY = 'pf:services:software-update:';
  10. public static function cacheKey()
  11. {
  12. return self::CACHE_KEY . 'latest:v1.0.0';
  13. }
  14. public static function get()
  15. {
  16. $curVersion = config('pixelfed.version');
  17. $versions = Cache::remember(self::cacheKey(), 1800, function() {
  18. return self::fetchLatest();
  19. });
  20. if(!$versions || !isset($versions['latest'], $versions['latest']['version'])) {
  21. $hideWarning = (bool) config('instance.software-update.disable_failed_warning');
  22. return [
  23. 'current' => $curVersion,
  24. 'latest' => [
  25. 'version' => null,
  26. 'published_at' => null,
  27. 'url' => null,
  28. ],
  29. 'running_latest' => $hideWarning ? true : null
  30. ];
  31. }
  32. return [
  33. 'current' => $curVersion,
  34. 'latest' => [
  35. 'version' => $versions['latest']['version'],
  36. 'published_at' => $versions['latest']['published_at'],
  37. 'url' => $versions['latest']['url'],
  38. ],
  39. 'running_latest' => strval($versions['latest']['version']) === strval($curVersion)
  40. ];
  41. }
  42. public static function fetchLatest()
  43. {
  44. try {
  45. $res = Http::withOptions(['allow_redirects' => false])
  46. ->timeout(5)
  47. ->connectTimeout(5)
  48. ->retry(2, 500)
  49. ->get('https://versions.pixelfed.org/versions.json');
  50. } catch (RequestException $e) {
  51. return;
  52. } catch (ConnectionException $e) {
  53. return;
  54. } catch (Exception $e) {
  55. return;
  56. }
  57. if(!$res->ok()) {
  58. return;
  59. }
  60. return $res->json();
  61. }
  62. }