AppFetcher.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\App\AppStore\Fetcher;
  27. use OC\App\AppStore\Version\VersionParser;
  28. use OC\App\CompareVersion;
  29. use OC\Files\AppData\Factory;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\Http\Client\IClientService;
  32. use OCP\IConfig;
  33. use OCP\ILogger;
  34. use OCP\Util;
  35. class AppFetcher extends Fetcher {
  36. /** @var CompareVersion */
  37. private $compareVersion;
  38. /**
  39. * @param Factory $appDataFactory
  40. * @param IClientService $clientService
  41. * @param ITimeFactory $timeFactory
  42. * @param IConfig $config
  43. * @param CompareVersion $compareVersion
  44. * @param ILogger $logger
  45. */
  46. public function __construct(Factory $appDataFactory,
  47. IClientService $clientService,
  48. ITimeFactory $timeFactory,
  49. IConfig $config,
  50. CompareVersion $compareVersion,
  51. ILogger $logger) {
  52. parent::__construct(
  53. $appDataFactory,
  54. $clientService,
  55. $timeFactory,
  56. $config,
  57. $logger
  58. );
  59. $this->fileName = 'apps.json';
  60. $this->setEndpoint();
  61. $this->compareVersion = $compareVersion;
  62. }
  63. /**
  64. * Only returns the latest compatible app release in the releases array
  65. *
  66. * @param string $ETag
  67. * @param string $content
  68. *
  69. * @return array
  70. */
  71. protected function fetch($ETag, $content) {
  72. /** @var mixed[] $response */
  73. $response = parent::fetch($ETag, $content);
  74. foreach($response['data'] as $dataKey => $app) {
  75. $releases = [];
  76. // Filter all compatible releases
  77. foreach($app['releases'] as $release) {
  78. // Exclude all nightly and pre-releases
  79. if($release['isNightly'] === false
  80. && strpos($release['version'], '-') === false) {
  81. // Exclude all versions not compatible with the current version
  82. try {
  83. $versionParser = new VersionParser();
  84. $version = $versionParser->getVersion($release['rawPlatformVersionSpec']);
  85. $ncVersion = $this->getVersion();
  86. $min = $version->getMinimumVersion();
  87. $max = $version->getMaximumVersion();
  88. $minFulfilled = $this->compareVersion->isCompatible($ncVersion, $min, '>=');
  89. $maxFulfilled = $max !== '' &&
  90. $this->compareVersion->isCompatible($ncVersion, $max, '<=');
  91. if ($minFulfilled && $maxFulfilled) {
  92. $releases[] = $release;
  93. }
  94. } catch (\InvalidArgumentException $e) {
  95. $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]);
  96. }
  97. }
  98. }
  99. // Get the highest version
  100. $versions = [];
  101. foreach($releases as $release) {
  102. $versions[] = $release['version'];
  103. }
  104. usort($versions, 'version_compare');
  105. $versions = array_reverse($versions);
  106. $compatible = false;
  107. if(isset($versions[0])) {
  108. $highestVersion = $versions[0];
  109. foreach ($releases as $release) {
  110. if ((string)$release['version'] === (string)$highestVersion) {
  111. $compatible = true;
  112. $response['data'][$dataKey]['releases'] = [$release];
  113. break;
  114. }
  115. }
  116. }
  117. if(!$compatible) {
  118. unset($response['data'][$dataKey]);
  119. }
  120. }
  121. $response['data'] = array_values($response['data']);
  122. return $response;
  123. }
  124. private function setEndpoint() {
  125. $versionArray = explode('.', $this->getVersion());
  126. $this->endpointUrl = sprintf(
  127. 'https://apps.nextcloud.com/api/v1/platform/%d.%d.%d/apps.json',
  128. $versionArray[0],
  129. $versionArray[1],
  130. $versionArray[2]
  131. );
  132. }
  133. /**
  134. * @param string $version
  135. * @param string $fileName
  136. */
  137. public function setVersion(string $version, string $fileName = 'apps.json') {
  138. parent::setVersion($version);
  139. $this->fileName = $fileName;
  140. $this->setEndpoint();
  141. }
  142. }