AppDiscoverFetcher.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
  4. *
  5. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  6. *
  7. * @license AGPL-3.0-or-later
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\App\AppStore\Fetcher;
  24. use DateTimeImmutable;
  25. use OC\App\CompareVersion;
  26. use OC\Files\AppData\Factory;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\Http\Client\IClientService;
  29. use OCP\IConfig;
  30. use OCP\Support\Subscription\IRegistry;
  31. use Psr\Log\LoggerInterface;
  32. class AppDiscoverFetcher extends Fetcher {
  33. public const INVALIDATE_AFTER_SECONDS = 86400;
  34. public function __construct(
  35. Factory $appDataFactory,
  36. IClientService $clientService,
  37. ITimeFactory $timeFactory,
  38. IConfig $config,
  39. LoggerInterface $logger,
  40. IRegistry $registry,
  41. private CompareVersion $compareVersion,
  42. ) {
  43. parent::__construct(
  44. $appDataFactory,
  45. $clientService,
  46. $timeFactory,
  47. $config,
  48. $logger,
  49. $registry
  50. );
  51. $this->fileName = 'discover.json';
  52. $this->endpointName = 'discover.json';
  53. }
  54. /**
  55. * Get the app discover section entries
  56. *
  57. * @param bool $allowUnstable Include also upcoming entries
  58. */
  59. public function get($allowUnstable = false) {
  60. $entries = parent::get(false);
  61. $now = new DateTimeImmutable();
  62. return array_filter($entries, function (array $entry) use ($now, $allowUnstable) {
  63. // Always remove expired entries
  64. if (isset($entry['expiryDate'])) {
  65. try {
  66. $expiryDate = new DateTimeImmutable($entry['expiryDate']);
  67. if ($expiryDate < $now) {
  68. return false;
  69. }
  70. } catch (\Throwable $e) {
  71. // Invalid expiryDate format
  72. return false;
  73. }
  74. }
  75. // If not include upcoming entries, check for upcoming dates and remove those entries
  76. if (!$allowUnstable && isset($entry['date'])) {
  77. try {
  78. $date = new DateTimeImmutable($entry['date']);
  79. if ($date > $now) {
  80. return false;
  81. }
  82. } catch (\Throwable $e) {
  83. // Invalid date format
  84. return false;
  85. }
  86. }
  87. // Otherwise the entry is not time limited and should stay
  88. return true;
  89. });
  90. }
  91. public function getETag(): string|null {
  92. $rootFolder = $this->appData->getFolder('/');
  93. try {
  94. $file = $rootFolder->getFile($this->fileName);
  95. $jsonBlob = json_decode($file->getContent(), true);
  96. if (is_array($jsonBlob) && isset($jsonBlob['ETag'])) {
  97. return (string)$jsonBlob['ETag'];
  98. }
  99. } catch (\Throwable $e) {
  100. // ignore
  101. }
  102. return null;
  103. }
  104. }