AppDiscoverFetcher.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\App\AppStore\Fetcher;
  7. use DateTimeImmutable;
  8. use OC\App\CompareVersion;
  9. use OC\Files\AppData\Factory;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\Http\Client\IClientService;
  12. use OCP\IConfig;
  13. use OCP\Support\Subscription\IRegistry;
  14. use Psr\Log\LoggerInterface;
  15. class AppDiscoverFetcher extends Fetcher {
  16. public const INVALIDATE_AFTER_SECONDS = 86400;
  17. public function __construct(
  18. Factory $appDataFactory,
  19. IClientService $clientService,
  20. ITimeFactory $timeFactory,
  21. IConfig $config,
  22. LoggerInterface $logger,
  23. IRegistry $registry,
  24. private CompareVersion $compareVersion,
  25. ) {
  26. parent::__construct(
  27. $appDataFactory,
  28. $clientService,
  29. $timeFactory,
  30. $config,
  31. $logger,
  32. $registry
  33. );
  34. $this->fileName = 'discover.json';
  35. $this->endpointName = 'discover.json';
  36. }
  37. /**
  38. * Get the app discover section entries
  39. *
  40. * @param bool $allowUnstable Include also upcoming entries
  41. */
  42. public function get($allowUnstable = false) {
  43. $entries = parent::get(false);
  44. $now = new DateTimeImmutable();
  45. return array_filter($entries, function (array $entry) use ($now, $allowUnstable) {
  46. // Always remove expired entries
  47. if (isset($entry['expiryDate'])) {
  48. try {
  49. $expiryDate = new DateTimeImmutable($entry['expiryDate']);
  50. if ($expiryDate < $now) {
  51. return false;
  52. }
  53. } catch (\Throwable $e) {
  54. // Invalid expiryDate format
  55. return false;
  56. }
  57. }
  58. // If not include upcoming entries, check for upcoming dates and remove those entries
  59. if (!$allowUnstable && isset($entry['date'])) {
  60. try {
  61. $date = new DateTimeImmutable($entry['date']);
  62. if ($date > $now) {
  63. return false;
  64. }
  65. } catch (\Throwable $e) {
  66. // Invalid date format
  67. return false;
  68. }
  69. }
  70. // Otherwise the entry is not time limited and should stay
  71. return true;
  72. });
  73. }
  74. public function getETag(): ?string {
  75. $rootFolder = $this->appData->getFolder('/');
  76. try {
  77. $file = $rootFolder->getFile($this->fileName);
  78. $jsonBlob = json_decode($file->getContent(), true);
  79. if (is_array($jsonBlob) && isset($jsonBlob['ETag'])) {
  80. return (string)$jsonBlob['ETag'];
  81. }
  82. } catch (\Throwable $e) {
  83. // ignore
  84. }
  85. return null;
  86. }
  87. }