@@ -39,162 +39,162 @@ |
||
| 39 | 39 | use OCP\Util; |
| 40 | 40 | |
| 41 | 41 | abstract class Fetcher { |
| 42 | - const INVALIDATE_AFTER_SECONDS = 300; |
|
| 43 | - |
|
| 44 | - /** @var IAppData */ |
|
| 45 | - protected $appData; |
|
| 46 | - /** @var IClientService */ |
|
| 47 | - protected $clientService; |
|
| 48 | - /** @var ITimeFactory */ |
|
| 49 | - protected $timeFactory; |
|
| 50 | - /** @var IConfig */ |
|
| 51 | - protected $config; |
|
| 52 | - /** @var Ilogger */ |
|
| 53 | - protected $logger; |
|
| 54 | - /** @var string */ |
|
| 55 | - protected $fileName; |
|
| 56 | - /** @var string */ |
|
| 57 | - protected $endpointUrl; |
|
| 58 | - /** @var string */ |
|
| 59 | - protected $version; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param Factory $appDataFactory |
|
| 63 | - * @param IClientService $clientService |
|
| 64 | - * @param ITimeFactory $timeFactory |
|
| 65 | - * @param IConfig $config |
|
| 66 | - * @param ILogger $logger |
|
| 67 | - */ |
|
| 68 | - public function __construct(Factory $appDataFactory, |
|
| 69 | - IClientService $clientService, |
|
| 70 | - ITimeFactory $timeFactory, |
|
| 71 | - IConfig $config, |
|
| 72 | - ILogger $logger) { |
|
| 73 | - $this->appData = $appDataFactory->get('appstore'); |
|
| 74 | - $this->clientService = $clientService; |
|
| 75 | - $this->timeFactory = $timeFactory; |
|
| 76 | - $this->config = $config; |
|
| 77 | - $this->logger = $logger; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Fetches the response from the server |
|
| 82 | - * |
|
| 83 | - * @param string $ETag |
|
| 84 | - * @param string $content |
|
| 85 | - * |
|
| 86 | - * @return array |
|
| 87 | - */ |
|
| 88 | - protected function fetch($ETag, $content) { |
|
| 89 | - $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
| 90 | - |
|
| 91 | - if (!$appstoreenabled) { |
|
| 92 | - return []; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $options = [ |
|
| 96 | - 'timeout' => 10, |
|
| 97 | - ]; |
|
| 98 | - |
|
| 99 | - if ($ETag !== '') { |
|
| 100 | - $options['headers'] = [ |
|
| 101 | - 'If-None-Match' => $ETag, |
|
| 102 | - ]; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $client = $this->clientService->newClient(); |
|
| 106 | - $response = $client->get($this->endpointUrl, $options); |
|
| 107 | - |
|
| 108 | - $responseJson = []; |
|
| 109 | - if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { |
|
| 110 | - $responseJson['data'] = json_decode($content, true); |
|
| 111 | - } else { |
|
| 112 | - $responseJson['data'] = json_decode($response->getBody(), true); |
|
| 113 | - $ETag = $response->getHeader('ETag'); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $responseJson['timestamp'] = $this->timeFactory->getTime(); |
|
| 117 | - $responseJson['ncversion'] = $this->getVersion(); |
|
| 118 | - if ($ETag !== '') { |
|
| 119 | - $responseJson['ETag'] = $ETag; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $responseJson; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Returns the array with the categories on the appstore server |
|
| 127 | - * |
|
| 128 | - * @return array |
|
| 129 | - */ |
|
| 130 | - public function get() { |
|
| 131 | - $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
| 132 | - $internetavailable = $this->config->getSystemValue('has_internet_connection', true); |
|
| 133 | - |
|
| 134 | - if (!$appstoreenabled || !$internetavailable) { |
|
| 135 | - return []; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - $rootFolder = $this->appData->getFolder('/'); |
|
| 139 | - |
|
| 140 | - $ETag = ''; |
|
| 141 | - $content = ''; |
|
| 142 | - |
|
| 143 | - try { |
|
| 144 | - // File does already exists |
|
| 145 | - $file = $rootFolder->getFile($this->fileName); |
|
| 146 | - $jsonBlob = json_decode($file->getContent(), true); |
|
| 147 | - if (is_array($jsonBlob)) { |
|
| 148 | - |
|
| 149 | - // No caching when the version has been updated |
|
| 150 | - if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { |
|
| 151 | - |
|
| 152 | - // If the timestamp is older than 300 seconds request the files new |
|
| 153 | - if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
|
| 154 | - return $jsonBlob['data']; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if (isset($jsonBlob['ETag'])) { |
|
| 158 | - $ETag = $jsonBlob['ETag']; |
|
| 159 | - $content = json_encode($jsonBlob['data']); |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } catch (NotFoundException $e) { |
|
| 164 | - // File does not already exists |
|
| 165 | - $file = $rootFolder->newFile($this->fileName); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - // Refresh the file content |
|
| 169 | - try { |
|
| 170 | - $responseJson = $this->fetch($ETag, $content); |
|
| 171 | - $file->putContent(json_encode($responseJson)); |
|
| 172 | - return json_decode($file->getContent(), true)['data']; |
|
| 173 | - } catch (ConnectException $e) { |
|
| 174 | - $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => Util::INFO, 'message' => 'Could not connect to appstore']); |
|
| 175 | - return []; |
|
| 176 | - } catch (\Exception $e) { |
|
| 177 | - $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => Util::INFO]); |
|
| 178 | - return []; |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Get the currently Nextcloud version |
|
| 184 | - * @return string |
|
| 185 | - */ |
|
| 186 | - protected function getVersion() { |
|
| 187 | - if ($this->version === null) { |
|
| 188 | - $this->version = $this->config->getSystemValue('version', '0.0.0'); |
|
| 189 | - } |
|
| 190 | - return $this->version; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Set the current Nextcloud version |
|
| 195 | - * @param string $version |
|
| 196 | - */ |
|
| 197 | - public function setVersion($version) { |
|
| 198 | - $this->version = $version; |
|
| 199 | - } |
|
| 42 | + const INVALIDATE_AFTER_SECONDS = 300; |
|
| 43 | + |
|
| 44 | + /** @var IAppData */ |
|
| 45 | + protected $appData; |
|
| 46 | + /** @var IClientService */ |
|
| 47 | + protected $clientService; |
|
| 48 | + /** @var ITimeFactory */ |
|
| 49 | + protected $timeFactory; |
|
| 50 | + /** @var IConfig */ |
|
| 51 | + protected $config; |
|
| 52 | + /** @var Ilogger */ |
|
| 53 | + protected $logger; |
|
| 54 | + /** @var string */ |
|
| 55 | + protected $fileName; |
|
| 56 | + /** @var string */ |
|
| 57 | + protected $endpointUrl; |
|
| 58 | + /** @var string */ |
|
| 59 | + protected $version; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param Factory $appDataFactory |
|
| 63 | + * @param IClientService $clientService |
|
| 64 | + * @param ITimeFactory $timeFactory |
|
| 65 | + * @param IConfig $config |
|
| 66 | + * @param ILogger $logger |
|
| 67 | + */ |
|
| 68 | + public function __construct(Factory $appDataFactory, |
|
| 69 | + IClientService $clientService, |
|
| 70 | + ITimeFactory $timeFactory, |
|
| 71 | + IConfig $config, |
|
| 72 | + ILogger $logger) { |
|
| 73 | + $this->appData = $appDataFactory->get('appstore'); |
|
| 74 | + $this->clientService = $clientService; |
|
| 75 | + $this->timeFactory = $timeFactory; |
|
| 76 | + $this->config = $config; |
|
| 77 | + $this->logger = $logger; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Fetches the response from the server |
|
| 82 | + * |
|
| 83 | + * @param string $ETag |
|
| 84 | + * @param string $content |
|
| 85 | + * |
|
| 86 | + * @return array |
|
| 87 | + */ |
|
| 88 | + protected function fetch($ETag, $content) { |
|
| 89 | + $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
| 90 | + |
|
| 91 | + if (!$appstoreenabled) { |
|
| 92 | + return []; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $options = [ |
|
| 96 | + 'timeout' => 10, |
|
| 97 | + ]; |
|
| 98 | + |
|
| 99 | + if ($ETag !== '') { |
|
| 100 | + $options['headers'] = [ |
|
| 101 | + 'If-None-Match' => $ETag, |
|
| 102 | + ]; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $client = $this->clientService->newClient(); |
|
| 106 | + $response = $client->get($this->endpointUrl, $options); |
|
| 107 | + |
|
| 108 | + $responseJson = []; |
|
| 109 | + if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { |
|
| 110 | + $responseJson['data'] = json_decode($content, true); |
|
| 111 | + } else { |
|
| 112 | + $responseJson['data'] = json_decode($response->getBody(), true); |
|
| 113 | + $ETag = $response->getHeader('ETag'); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $responseJson['timestamp'] = $this->timeFactory->getTime(); |
|
| 117 | + $responseJson['ncversion'] = $this->getVersion(); |
|
| 118 | + if ($ETag !== '') { |
|
| 119 | + $responseJson['ETag'] = $ETag; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $responseJson; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Returns the array with the categories on the appstore server |
|
| 127 | + * |
|
| 128 | + * @return array |
|
| 129 | + */ |
|
| 130 | + public function get() { |
|
| 131 | + $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
| 132 | + $internetavailable = $this->config->getSystemValue('has_internet_connection', true); |
|
| 133 | + |
|
| 134 | + if (!$appstoreenabled || !$internetavailable) { |
|
| 135 | + return []; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + $rootFolder = $this->appData->getFolder('/'); |
|
| 139 | + |
|
| 140 | + $ETag = ''; |
|
| 141 | + $content = ''; |
|
| 142 | + |
|
| 143 | + try { |
|
| 144 | + // File does already exists |
|
| 145 | + $file = $rootFolder->getFile($this->fileName); |
|
| 146 | + $jsonBlob = json_decode($file->getContent(), true); |
|
| 147 | + if (is_array($jsonBlob)) { |
|
| 148 | + |
|
| 149 | + // No caching when the version has been updated |
|
| 150 | + if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { |
|
| 151 | + |
|
| 152 | + // If the timestamp is older than 300 seconds request the files new |
|
| 153 | + if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
|
| 154 | + return $jsonBlob['data']; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if (isset($jsonBlob['ETag'])) { |
|
| 158 | + $ETag = $jsonBlob['ETag']; |
|
| 159 | + $content = json_encode($jsonBlob['data']); |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } catch (NotFoundException $e) { |
|
| 164 | + // File does not already exists |
|
| 165 | + $file = $rootFolder->newFile($this->fileName); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + // Refresh the file content |
|
| 169 | + try { |
|
| 170 | + $responseJson = $this->fetch($ETag, $content); |
|
| 171 | + $file->putContent(json_encode($responseJson)); |
|
| 172 | + return json_decode($file->getContent(), true)['data']; |
|
| 173 | + } catch (ConnectException $e) { |
|
| 174 | + $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => Util::INFO, 'message' => 'Could not connect to appstore']); |
|
| 175 | + return []; |
|
| 176 | + } catch (\Exception $e) { |
|
| 177 | + $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => Util::INFO]); |
|
| 178 | + return []; |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Get the currently Nextcloud version |
|
| 184 | + * @return string |
|
| 185 | + */ |
|
| 186 | + protected function getVersion() { |
|
| 187 | + if ($this->version === null) { |
|
| 188 | + $this->version = $this->config->getSystemValue('version', '0.0.0'); |
|
| 189 | + } |
|
| 190 | + return $this->version; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Set the current Nextcloud version |
|
| 195 | + * @param string $version |
|
| 196 | + */ |
|
| 197 | + public function setVersion($version) { |
|
| 198 | + $this->version = $version; |
|
| 199 | + } |
|
| 200 | 200 | } |