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