| @@ -39,118 +39,118 @@ | ||
| 39 | 39 | |
| 40 | 40 |  class AppFetcher extends Fetcher { | 
| 41 | 41 | |
| 42 | - /** @var CompareVersion */ | |
| 43 | - private $compareVersion; | |
| 42 | + /** @var CompareVersion */ | |
| 43 | + private $compareVersion; | |
| 44 | 44 | |
| 45 | - /** @var bool */ | |
| 46 | - private $ignoreMaxVersion; | |
| 45 | + /** @var bool */ | |
| 46 | + private $ignoreMaxVersion; | |
| 47 | 47 | |
| 48 | - /** | |
| 49 | - * @param Factory $appDataFactory | |
| 50 | - * @param IClientService $clientService | |
| 51 | - * @param ITimeFactory $timeFactory | |
| 52 | - * @param IConfig $config | |
| 53 | - * @param CompareVersion $compareVersion | |
| 54 | - * @param ILogger $logger | |
| 55 | - */ | |
| 56 | - public function __construct(Factory $appDataFactory, | |
| 57 | - IClientService $clientService, | |
| 58 | - ITimeFactory $timeFactory, | |
| 59 | - IConfig $config, | |
| 60 | - CompareVersion $compareVersion, | |
| 61 | -								ILogger $logger) { | |
| 62 | - parent::__construct( | |
| 63 | - $appDataFactory, | |
| 64 | - $clientService, | |
| 65 | - $timeFactory, | |
| 66 | - $config, | |
| 67 | - $logger | |
| 68 | - ); | |
| 48 | + /** | |
| 49 | + * @param Factory $appDataFactory | |
| 50 | + * @param IClientService $clientService | |
| 51 | + * @param ITimeFactory $timeFactory | |
| 52 | + * @param IConfig $config | |
| 53 | + * @param CompareVersion $compareVersion | |
| 54 | + * @param ILogger $logger | |
| 55 | + */ | |
| 56 | + public function __construct(Factory $appDataFactory, | |
| 57 | + IClientService $clientService, | |
| 58 | + ITimeFactory $timeFactory, | |
| 59 | + IConfig $config, | |
| 60 | + CompareVersion $compareVersion, | |
| 61 | +                                ILogger $logger) { | |
| 62 | + parent::__construct( | |
| 63 | + $appDataFactory, | |
| 64 | + $clientService, | |
| 65 | + $timeFactory, | |
| 66 | + $config, | |
| 67 | + $logger | |
| 68 | + ); | |
| 69 | 69 | |
| 70 | - $this->fileName = 'apps.json'; | |
| 71 | - $this->endpointName = 'apps.json'; | |
| 72 | - $this->compareVersion = $compareVersion; | |
| 73 | - $this->ignoreMaxVersion = true; | |
| 74 | - } | |
| 70 | + $this->fileName = 'apps.json'; | |
| 71 | + $this->endpointName = 'apps.json'; | |
| 72 | + $this->compareVersion = $compareVersion; | |
| 73 | + $this->ignoreMaxVersion = true; | |
| 74 | + } | |
| 75 | 75 | |
| 76 | - /** | |
| 77 | - * Only returns the latest compatible app release in the releases array | |
| 78 | - * | |
| 79 | - * @param string $ETag | |
| 80 | - * @param string $content | |
| 81 | - * | |
| 82 | - * @return array | |
| 83 | - */ | |
| 84 | -	protected function fetch($ETag, $content) { | |
| 85 | - /** @var mixed[] $response */ | |
| 86 | - $response = parent::fetch($ETag, $content); | |
| 76 | + /** | |
| 77 | + * Only returns the latest compatible app release in the releases array | |
| 78 | + * | |
| 79 | + * @param string $ETag | |
| 80 | + * @param string $content | |
| 81 | + * | |
| 82 | + * @return array | |
| 83 | + */ | |
| 84 | +    protected function fetch($ETag, $content) { | |
| 85 | + /** @var mixed[] $response */ | |
| 86 | + $response = parent::fetch($ETag, $content); | |
| 87 | 87 | |
| 88 | - $allowPreReleases = $this->getChannel() === 'beta' || $this->getChannel() === 'daily'; | |
| 89 | - $allowNightly = $this->getChannel() === 'daily'; | |
| 88 | + $allowPreReleases = $this->getChannel() === 'beta' || $this->getChannel() === 'daily'; | |
| 89 | + $allowNightly = $this->getChannel() === 'daily'; | |
| 90 | 90 | |
| 91 | -		foreach($response['data'] as $dataKey => $app) { | |
| 92 | - $releases = []; | |
| 91 | +        foreach($response['data'] as $dataKey => $app) { | |
| 92 | + $releases = []; | |
| 93 | 93 | |
| 94 | - // Filter all compatible releases | |
| 95 | -			foreach($app['releases'] as $release) { | |
| 96 | - // Exclude all nightly and pre-releases if required | |
| 97 | - if (($allowNightly || $release['isNightly'] === false) | |
| 98 | -					&& ($allowPreReleases || strpos($release['version'], '-') === false)) { | |
| 99 | - // Exclude all versions not compatible with the current version | |
| 100 | -					try { | |
| 101 | - $versionParser = new VersionParser(); | |
| 102 | - $version = $versionParser->getVersion($release['rawPlatformVersionSpec']); | |
| 103 | - $ncVersion = $this->getVersion(); | |
| 104 | - $min = $version->getMinimumVersion(); | |
| 105 | - $max = $version->getMaximumVersion(); | |
| 106 | - $minFulfilled = $this->compareVersion->isCompatible($ncVersion, $min, '>='); | |
| 107 | - $maxFulfilled = $max !== '' && | |
| 108 | - $this->compareVersion->isCompatible($ncVersion, $max, '<='); | |
| 109 | -						if ($minFulfilled && ($this->ignoreMaxVersion || $maxFulfilled)) { | |
| 110 | - $releases[] = $release; | |
| 111 | - } | |
| 112 | -					} catch (\InvalidArgumentException $e) { | |
| 113 | - $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]); | |
| 114 | - } | |
| 115 | - } | |
| 116 | - } | |
| 94 | + // Filter all compatible releases | |
| 95 | +            foreach($app['releases'] as $release) { | |
| 96 | + // Exclude all nightly and pre-releases if required | |
| 97 | + if (($allowNightly || $release['isNightly'] === false) | |
| 98 | +                    && ($allowPreReleases || strpos($release['version'], '-') === false)) { | |
| 99 | + // Exclude all versions not compatible with the current version | |
| 100 | +                    try { | |
| 101 | + $versionParser = new VersionParser(); | |
| 102 | + $version = $versionParser->getVersion($release['rawPlatformVersionSpec']); | |
| 103 | + $ncVersion = $this->getVersion(); | |
| 104 | + $min = $version->getMinimumVersion(); | |
| 105 | + $max = $version->getMaximumVersion(); | |
| 106 | + $minFulfilled = $this->compareVersion->isCompatible($ncVersion, $min, '>='); | |
| 107 | + $maxFulfilled = $max !== '' && | |
| 108 | + $this->compareVersion->isCompatible($ncVersion, $max, '<='); | |
| 109 | +                        if ($minFulfilled && ($this->ignoreMaxVersion || $maxFulfilled)) { | |
| 110 | + $releases[] = $release; | |
| 111 | + } | |
| 112 | +                    } catch (\InvalidArgumentException $e) { | |
| 113 | + $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]); | |
| 114 | + } | |
| 115 | + } | |
| 116 | + } | |
| 117 | 117 | |
| 118 | -			if (empty($releases)) { | |
| 119 | - // Remove apps that don't have a matching release | |
| 120 | - $response['data'][$dataKey] = []; | |
| 121 | - continue; | |
| 122 | - } | |
| 118 | +            if (empty($releases)) { | |
| 119 | + // Remove apps that don't have a matching release | |
| 120 | + $response['data'][$dataKey] = []; | |
| 121 | + continue; | |
| 122 | + } | |
| 123 | 123 | |
| 124 | - // Get the highest version | |
| 125 | - $versions = []; | |
| 126 | -			foreach($releases as $release) { | |
| 127 | - $versions[] = $release['version']; | |
| 128 | - } | |
| 129 | - usort($versions, 'version_compare'); | |
| 130 | - $versions = array_reverse($versions); | |
| 131 | -			if(isset($versions[0])) { | |
| 132 | - $highestVersion = $versions[0]; | |
| 133 | -				foreach ($releases as $release) { | |
| 134 | -					if ((string)$release['version'] === (string)$highestVersion) { | |
| 135 | - $response['data'][$dataKey]['releases'] = [$release]; | |
| 136 | - break; | |
| 137 | - } | |
| 138 | - } | |
| 139 | - } | |
| 140 | - } | |
| 124 | + // Get the highest version | |
| 125 | + $versions = []; | |
| 126 | +            foreach($releases as $release) { | |
| 127 | + $versions[] = $release['version']; | |
| 128 | + } | |
| 129 | + usort($versions, 'version_compare'); | |
| 130 | + $versions = array_reverse($versions); | |
| 131 | +            if(isset($versions[0])) { | |
| 132 | + $highestVersion = $versions[0]; | |
| 133 | +                foreach ($releases as $release) { | |
| 134 | +                    if ((string)$release['version'] === (string)$highestVersion) { | |
| 135 | + $response['data'][$dataKey]['releases'] = [$release]; | |
| 136 | + break; | |
| 137 | + } | |
| 138 | + } | |
| 139 | + } | |
| 140 | + } | |
| 141 | 141 | |
| 142 | - $response['data'] = array_values(array_filter($response['data'])); | |
| 143 | - return $response; | |
| 144 | - } | |
| 142 | + $response['data'] = array_values(array_filter($response['data'])); | |
| 143 | + return $response; | |
| 144 | + } | |
| 145 | 145 | |
| 146 | - /** | |
| 147 | - * @param string $version | |
| 148 | - * @param string $fileName | |
| 149 | - * @param bool $ignoreMaxVersion | |
| 150 | - */ | |
| 151 | -	public function setVersion(string $version, string $fileName = 'apps.json', bool $ignoreMaxVersion = true) { | |
| 152 | - parent::setVersion($version); | |
| 153 | - $this->fileName = $fileName; | |
| 154 | - $this->ignoreMaxVersion = $ignoreMaxVersion; | |
| 155 | - } | |
| 146 | + /** | |
| 147 | + * @param string $version | |
| 148 | + * @param string $fileName | |
| 149 | + * @param bool $ignoreMaxVersion | |
| 150 | + */ | |
| 151 | +    public function setVersion(string $version, string $fileName = 'apps.json', bool $ignoreMaxVersion = true) { | |
| 152 | + parent::setVersion($version); | |
| 153 | + $this->fileName = $fileName; | |
| 154 | + $this->ignoreMaxVersion = $ignoreMaxVersion; | |
| 155 | + } | |
| 156 | 156 | } | 
| @@ -40,187 +40,187 @@ | ||
| 40 | 40 | use OCP\Util; | 
| 41 | 41 | |
| 42 | 42 |  abstract class Fetcher { | 
| 43 | - const INVALIDATE_AFTER_SECONDS = 300; | |
| 44 | - | |
| 45 | - /** @var IAppData */ | |
| 46 | - protected $appData; | |
| 47 | - /** @var IClientService */ | |
| 48 | - protected $clientService; | |
| 49 | - /** @var ITimeFactory */ | |
| 50 | - protected $timeFactory; | |
| 51 | - /** @var IConfig */ | |
| 52 | - protected $config; | |
| 53 | - /** @var Ilogger */ | |
| 54 | - protected $logger; | |
| 55 | - /** @var string */ | |
| 56 | - protected $fileName; | |
| 57 | - /** @var string */ | |
| 58 | - protected $endpointName; | |
| 59 | - /** @var string */ | |
| 60 | - protected $version; | |
| 61 | - /** @var string */ | |
| 62 | - protected $channel; | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * @param Factory $appDataFactory | |
| 66 | - * @param IClientService $clientService | |
| 67 | - * @param ITimeFactory $timeFactory | |
| 68 | - * @param IConfig $config | |
| 69 | - * @param ILogger $logger | |
| 70 | - */ | |
| 71 | - public function __construct(Factory $appDataFactory, | |
| 72 | - IClientService $clientService, | |
| 73 | - ITimeFactory $timeFactory, | |
| 74 | - IConfig $config, | |
| 75 | -								ILogger $logger) { | |
| 76 | -		$this->appData = $appDataFactory->get('appstore'); | |
| 77 | - $this->clientService = $clientService; | |
| 78 | - $this->timeFactory = $timeFactory; | |
| 79 | - $this->config = $config; | |
| 80 | - $this->logger = $logger; | |
| 81 | - } | |
| 82 | - | |
| 83 | - /** | |
| 84 | - * Fetches the response from the server | |
| 85 | - * | |
| 86 | - * @param string $ETag | |
| 87 | - * @param string $content | |
| 88 | - * | |
| 89 | - * @return array | |
| 90 | - */ | |
| 91 | -	protected function fetch($ETag, $content) { | |
| 92 | -		$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); | |
| 93 | - | |
| 94 | -		if (!$appstoreenabled) { | |
| 95 | - return []; | |
| 96 | - } | |
| 97 | - | |
| 98 | - $options = [ | |
| 99 | - 'timeout' => 10, | |
| 100 | - ]; | |
| 101 | - | |
| 102 | -		if ($ETag !== '') { | |
| 103 | - $options['headers'] = [ | |
| 104 | - 'If-None-Match' => $ETag, | |
| 105 | - ]; | |
| 106 | - } | |
| 107 | - | |
| 108 | - $client = $this->clientService->newClient(); | |
| 109 | - $response = $client->get($this->getEndpoint(), $options); | |
| 110 | - | |
| 111 | - $responseJson = []; | |
| 112 | -		if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { | |
| 113 | - $responseJson['data'] = json_decode($content, true); | |
| 114 | -		} else { | |
| 115 | - $responseJson['data'] = json_decode($response->getBody(), true); | |
| 116 | -			$ETag = $response->getHeader('ETag'); | |
| 117 | - } | |
| 118 | - | |
| 119 | - $responseJson['timestamp'] = $this->timeFactory->getTime(); | |
| 120 | - $responseJson['ncversion'] = $this->getVersion(); | |
| 121 | -		if ($ETag !== '') { | |
| 122 | - $responseJson['ETag'] = $ETag; | |
| 123 | - } | |
| 124 | - | |
| 125 | - return $responseJson; | |
| 126 | - } | |
| 127 | - | |
| 128 | - /** | |
| 129 | - * Returns the array with the categories on the appstore server | |
| 130 | - * | |
| 131 | - * @return array | |
| 132 | - */ | |
| 133 | -	public function get() { | |
| 134 | -		$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); | |
| 135 | -		$internetavailable = $this->config->getSystemValue('has_internet_connection', true); | |
| 136 | - | |
| 137 | -		if (!$appstoreenabled || !$internetavailable) { | |
| 138 | - return []; | |
| 139 | - } | |
| 140 | - | |
| 141 | -		$rootFolder = $this->appData->getFolder('/'); | |
| 142 | - | |
| 143 | - $ETag = ''; | |
| 144 | - $content = ''; | |
| 145 | - | |
| 146 | -		try { | |
| 147 | - // File does already exists | |
| 148 | - $file = $rootFolder->getFile($this->fileName); | |
| 149 | - $jsonBlob = json_decode($file->getContent(), true); | |
| 150 | -			if (is_array($jsonBlob)) { | |
| 151 | - | |
| 152 | - // No caching when the version has been updated | |
| 153 | -				if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { | |
| 154 | - | |
| 155 | - // If the timestamp is older than 300 seconds request the files new | |
| 156 | -					if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { | |
| 157 | - return $jsonBlob['data']; | |
| 158 | - } | |
| 159 | - | |
| 160 | -					if (isset($jsonBlob['ETag'])) { | |
| 161 | - $ETag = $jsonBlob['ETag']; | |
| 162 | - $content = json_encode($jsonBlob['data']); | |
| 163 | - } | |
| 164 | - } | |
| 165 | - } | |
| 166 | -		} catch (NotFoundException $e) { | |
| 167 | - // File does not already exists | |
| 168 | - $file = $rootFolder->newFile($this->fileName); | |
| 169 | - } | |
| 170 | - | |
| 171 | - // Refresh the file content | |
| 172 | -		try { | |
| 173 | - $responseJson = $this->fetch($ETag, $content); | |
| 174 | - $file->putContent(json_encode($responseJson)); | |
| 175 | - return json_decode($file->getContent(), true)['data']; | |
| 176 | -		} catch (ConnectException $e) { | |
| 177 | -			$this->logger->warning('Could not connect to appstore: ' . $e->getMessage(), ['app' => 'appstoreFetcher']); | |
| 178 | - return []; | |
| 179 | -		} catch (\Exception $e) { | |
| 180 | - $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]); | |
| 181 | - return []; | |
| 182 | - } | |
| 183 | - } | |
| 184 | - | |
| 185 | - /** | |
| 186 | - * Get the currently Nextcloud version | |
| 187 | - * @return string | |
| 188 | - */ | |
| 189 | -	protected function getVersion() { | |
| 190 | -		if ($this->version === null) { | |
| 191 | -			$this->version = $this->config->getSystemValue('version', '0.0.0'); | |
| 192 | - } | |
| 193 | - return $this->version; | |
| 194 | - } | |
| 195 | - | |
| 196 | - /** | |
| 197 | - * Set the current Nextcloud version | |
| 198 | - * @param string $version | |
| 199 | - */ | |
| 200 | -	public function setVersion(string $version) { | |
| 201 | - $this->version = $version; | |
| 202 | - } | |
| 203 | - | |
| 204 | - /** | |
| 205 | - * Get the currently Nextcloud update channel | |
| 206 | - * @return string | |
| 207 | - */ | |
| 208 | -	protected function getChannel() { | |
| 209 | -		if ($this->channel === null) { | |
| 210 | - $this->channel = \OC_Util::getChannel(); | |
| 211 | - } | |
| 212 | - return $this->channel; | |
| 213 | - } | |
| 214 | - | |
| 215 | - /** | |
| 216 | - * Set the current Nextcloud update channel | |
| 217 | - * @param string $channel | |
| 218 | - */ | |
| 219 | -	public function setChannel(string $channel) { | |
| 220 | - $this->channel = $channel; | |
| 221 | - } | |
| 222 | - | |
| 223 | -	protected function getEndpoint(): string { | |
| 224 | -		return $this->config->getSystemValue('appstoreurl', 'https://apps.nextcloud.com/api/v1') . '/' . $this->endpointName; | |
| 225 | - } | |
| 43 | + const INVALIDATE_AFTER_SECONDS = 300; | |
| 44 | + | |
| 45 | + /** @var IAppData */ | |
| 46 | + protected $appData; | |
| 47 | + /** @var IClientService */ | |
| 48 | + protected $clientService; | |
| 49 | + /** @var ITimeFactory */ | |
| 50 | + protected $timeFactory; | |
| 51 | + /** @var IConfig */ | |
| 52 | + protected $config; | |
| 53 | + /** @var Ilogger */ | |
| 54 | + protected $logger; | |
| 55 | + /** @var string */ | |
| 56 | + protected $fileName; | |
| 57 | + /** @var string */ | |
| 58 | + protected $endpointName; | |
| 59 | + /** @var string */ | |
| 60 | + protected $version; | |
| 61 | + /** @var string */ | |
| 62 | + protected $channel; | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * @param Factory $appDataFactory | |
| 66 | + * @param IClientService $clientService | |
| 67 | + * @param ITimeFactory $timeFactory | |
| 68 | + * @param IConfig $config | |
| 69 | + * @param ILogger $logger | |
| 70 | + */ | |
| 71 | + public function __construct(Factory $appDataFactory, | |
| 72 | + IClientService $clientService, | |
| 73 | + ITimeFactory $timeFactory, | |
| 74 | + IConfig $config, | |
| 75 | +                                ILogger $logger) { | |
| 76 | +        $this->appData = $appDataFactory->get('appstore'); | |
| 77 | + $this->clientService = $clientService; | |
| 78 | + $this->timeFactory = $timeFactory; | |
| 79 | + $this->config = $config; | |
| 80 | + $this->logger = $logger; | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Fetches the response from the server | |
| 85 | + * | |
| 86 | + * @param string $ETag | |
| 87 | + * @param string $content | |
| 88 | + * | |
| 89 | + * @return array | |
| 90 | + */ | |
| 91 | +    protected function fetch($ETag, $content) { | |
| 92 | +        $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); | |
| 93 | + | |
| 94 | +        if (!$appstoreenabled) { | |
| 95 | + return []; | |
| 96 | + } | |
| 97 | + | |
| 98 | + $options = [ | |
| 99 | + 'timeout' => 10, | |
| 100 | + ]; | |
| 101 | + | |
| 102 | +        if ($ETag !== '') { | |
| 103 | + $options['headers'] = [ | |
| 104 | + 'If-None-Match' => $ETag, | |
| 105 | + ]; | |
| 106 | + } | |
| 107 | + | |
| 108 | + $client = $this->clientService->newClient(); | |
| 109 | + $response = $client->get($this->getEndpoint(), $options); | |
| 110 | + | |
| 111 | + $responseJson = []; | |
| 112 | +        if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { | |
| 113 | + $responseJson['data'] = json_decode($content, true); | |
| 114 | +        } else { | |
| 115 | + $responseJson['data'] = json_decode($response->getBody(), true); | |
| 116 | +            $ETag = $response->getHeader('ETag'); | |
| 117 | + } | |
| 118 | + | |
| 119 | + $responseJson['timestamp'] = $this->timeFactory->getTime(); | |
| 120 | + $responseJson['ncversion'] = $this->getVersion(); | |
| 121 | +        if ($ETag !== '') { | |
| 122 | + $responseJson['ETag'] = $ETag; | |
| 123 | + } | |
| 124 | + | |
| 125 | + return $responseJson; | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Returns the array with the categories on the appstore server | |
| 130 | + * | |
| 131 | + * @return array | |
| 132 | + */ | |
| 133 | +    public function get() { | |
| 134 | +        $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); | |
| 135 | +        $internetavailable = $this->config->getSystemValue('has_internet_connection', true); | |
| 136 | + | |
| 137 | +        if (!$appstoreenabled || !$internetavailable) { | |
| 138 | + return []; | |
| 139 | + } | |
| 140 | + | |
| 141 | +        $rootFolder = $this->appData->getFolder('/'); | |
| 142 | + | |
| 143 | + $ETag = ''; | |
| 144 | + $content = ''; | |
| 145 | + | |
| 146 | +        try { | |
| 147 | + // File does already exists | |
| 148 | + $file = $rootFolder->getFile($this->fileName); | |
| 149 | + $jsonBlob = json_decode($file->getContent(), true); | |
| 150 | +            if (is_array($jsonBlob)) { | |
| 151 | + | |
| 152 | + // No caching when the version has been updated | |
| 153 | +                if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { | |
| 154 | + | |
| 155 | + // If the timestamp is older than 300 seconds request the files new | |
| 156 | +                    if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { | |
| 157 | + return $jsonBlob['data']; | |
| 158 | + } | |
| 159 | + | |
| 160 | +                    if (isset($jsonBlob['ETag'])) { | |
| 161 | + $ETag = $jsonBlob['ETag']; | |
| 162 | + $content = json_encode($jsonBlob['data']); | |
| 163 | + } | |
| 164 | + } | |
| 165 | + } | |
| 166 | +        } catch (NotFoundException $e) { | |
| 167 | + // File does not already exists | |
| 168 | + $file = $rootFolder->newFile($this->fileName); | |
| 169 | + } | |
| 170 | + | |
| 171 | + // Refresh the file content | |
| 172 | +        try { | |
| 173 | + $responseJson = $this->fetch($ETag, $content); | |
| 174 | + $file->putContent(json_encode($responseJson)); | |
| 175 | + return json_decode($file->getContent(), true)['data']; | |
| 176 | +        } catch (ConnectException $e) { | |
| 177 | +            $this->logger->warning('Could not connect to appstore: ' . $e->getMessage(), ['app' => 'appstoreFetcher']); | |
| 178 | + return []; | |
| 179 | +        } catch (\Exception $e) { | |
| 180 | + $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]); | |
| 181 | + return []; | |
| 182 | + } | |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * Get the currently Nextcloud version | |
| 187 | + * @return string | |
| 188 | + */ | |
| 189 | +    protected function getVersion() { | |
| 190 | +        if ($this->version === null) { | |
| 191 | +            $this->version = $this->config->getSystemValue('version', '0.0.0'); | |
| 192 | + } | |
| 193 | + return $this->version; | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * Set the current Nextcloud version | |
| 198 | + * @param string $version | |
| 199 | + */ | |
| 200 | +    public function setVersion(string $version) { | |
| 201 | + $this->version = $version; | |
| 202 | + } | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Get the currently Nextcloud update channel | |
| 206 | + * @return string | |
| 207 | + */ | |
| 208 | +    protected function getChannel() { | |
| 209 | +        if ($this->channel === null) { | |
| 210 | + $this->channel = \OC_Util::getChannel(); | |
| 211 | + } | |
| 212 | + return $this->channel; | |
| 213 | + } | |
| 214 | + | |
| 215 | + /** | |
| 216 | + * Set the current Nextcloud update channel | |
| 217 | + * @param string $channel | |
| 218 | + */ | |
| 219 | +    public function setChannel(string $channel) { | |
| 220 | + $this->channel = $channel; | |
| 221 | + } | |
| 222 | + | |
| 223 | +    protected function getEndpoint(): string { | |
| 224 | +        return $this->config->getSystemValue('appstoreurl', 'https://apps.nextcloud.com/api/v1') . '/' . $this->endpointName; | |
| 225 | + } | |
| 226 | 226 | } | 
| @@ -153,7 +153,7 @@ discard block | ||
| 153 | 153 |  				if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { | 
| 154 | 154 | |
| 155 | 155 | // If the timestamp is older than 300 seconds request the files new | 
| 156 | -					if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { | |
| 156 | +					if ((int) $jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { | |
| 157 | 157 | return $jsonBlob['data']; | 
| 158 | 158 | } | 
| 159 | 159 | |
| @@ -174,7 +174,7 @@ discard block | ||
| 174 | 174 | $file->putContent(json_encode($responseJson)); | 
| 175 | 175 | return json_decode($file->getContent(), true)['data']; | 
| 176 | 176 |  		} catch (ConnectException $e) { | 
| 177 | -			$this->logger->warning('Could not connect to appstore: ' . $e->getMessage(), ['app' => 'appstoreFetcher']); | |
| 177 | +			$this->logger->warning('Could not connect to appstore: '.$e->getMessage(), ['app' => 'appstoreFetcher']); | |
| 178 | 178 | return []; | 
| 179 | 179 |  		} catch (\Exception $e) { | 
| 180 | 180 | $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]); | 
| @@ -221,6 +221,6 @@ discard block | ||
| 221 | 221 | } | 
| 222 | 222 | |
| 223 | 223 |  	protected function getEndpoint(): string { | 
| 224 | -		return $this->config->getSystemValue('appstoreurl', 'https://apps.nextcloud.com/api/v1') . '/' . $this->endpointName; | |
| 224 | +		return $this->config->getSystemValue('appstoreurl', 'https://apps.nextcloud.com/api/v1').'/'.$this->endpointName; | |
| 225 | 225 | } | 
| 226 | 226 | } | 
| @@ -33,26 +33,26 @@ | ||
| 33 | 33 | use OCP\ILogger; | 
| 34 | 34 | |
| 35 | 35 |  class CategoryFetcher extends Fetcher { | 
| 36 | - /** | |
| 37 | - * @param Factory $appDataFactory | |
| 38 | - * @param IClientService $clientService | |
| 39 | - * @param ITimeFactory $timeFactory | |
| 40 | - * @param IConfig $config | |
| 41 | - * @param ILogger $logger | |
| 42 | - */ | |
| 43 | - public function __construct(Factory $appDataFactory, | |
| 44 | - IClientService $clientService, | |
| 45 | - ITimeFactory $timeFactory, | |
| 46 | - IConfig $config, | |
| 47 | -								ILogger $logger) { | |
| 48 | - parent::__construct( | |
| 49 | - $appDataFactory, | |
| 50 | - $clientService, | |
| 51 | - $timeFactory, | |
| 52 | - $config, | |
| 53 | - $logger | |
| 54 | - ); | |
| 55 | - $this->fileName = 'categories.json'; | |
| 56 | - $this->endpointName = 'categories.json'; | |
| 57 | - } | |
| 36 | + /** | |
| 37 | + * @param Factory $appDataFactory | |
| 38 | + * @param IClientService $clientService | |
| 39 | + * @param ITimeFactory $timeFactory | |
| 40 | + * @param IConfig $config | |
| 41 | + * @param ILogger $logger | |
| 42 | + */ | |
| 43 | + public function __construct(Factory $appDataFactory, | |
| 44 | + IClientService $clientService, | |
| 45 | + ITimeFactory $timeFactory, | |
| 46 | + IConfig $config, | |
| 47 | +                                ILogger $logger) { | |
| 48 | + parent::__construct( | |
| 49 | + $appDataFactory, | |
| 50 | + $clientService, | |
| 51 | + $timeFactory, | |
| 52 | + $config, | |
| 53 | + $logger | |
| 54 | + ); | |
| 55 | + $this->fileName = 'categories.json'; | |
| 56 | + $this->endpointName = 'categories.json'; | |
| 57 | + } | |
| 58 | 58 | } |