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