@@ -29,108 +29,108 @@ |
||
29 | 29 | use OCP\ILogger; |
30 | 30 | |
31 | 31 | class AppFetcher extends Fetcher { |
32 | - /** |
|
33 | - * @param Factory $appDataFactory |
|
34 | - * @param IClientService $clientService |
|
35 | - * @param ITimeFactory $timeFactory |
|
36 | - * @param IConfig $config |
|
37 | - * @param ILogger $logger |
|
38 | - */ |
|
39 | - public function __construct(Factory $appDataFactory, |
|
40 | - IClientService $clientService, |
|
41 | - ITimeFactory $timeFactory, |
|
42 | - IConfig $config, |
|
43 | - ILogger $logger) { |
|
44 | - parent::__construct( |
|
45 | - $appDataFactory, |
|
46 | - $clientService, |
|
47 | - $timeFactory, |
|
48 | - $config, |
|
49 | - $logger |
|
50 | - ); |
|
32 | + /** |
|
33 | + * @param Factory $appDataFactory |
|
34 | + * @param IClientService $clientService |
|
35 | + * @param ITimeFactory $timeFactory |
|
36 | + * @param IConfig $config |
|
37 | + * @param ILogger $logger |
|
38 | + */ |
|
39 | + public function __construct(Factory $appDataFactory, |
|
40 | + IClientService $clientService, |
|
41 | + ITimeFactory $timeFactory, |
|
42 | + IConfig $config, |
|
43 | + ILogger $logger) { |
|
44 | + parent::__construct( |
|
45 | + $appDataFactory, |
|
46 | + $clientService, |
|
47 | + $timeFactory, |
|
48 | + $config, |
|
49 | + $logger |
|
50 | + ); |
|
51 | 51 | |
52 | - $this->fileName = 'apps.json'; |
|
53 | - $this->setEndpoint(); |
|
54 | - } |
|
52 | + $this->fileName = 'apps.json'; |
|
53 | + $this->setEndpoint(); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Only returns the latest compatible app release in the releases array |
|
58 | - * |
|
59 | - * @param string $ETag |
|
60 | - * @param string $content |
|
61 | - * |
|
62 | - * @return array |
|
63 | - */ |
|
64 | - protected function fetch($ETag, $content) { |
|
65 | - /** @var mixed[] $response */ |
|
66 | - $response = parent::fetch($ETag, $content); |
|
56 | + /** |
|
57 | + * Only returns the latest compatible app release in the releases array |
|
58 | + * |
|
59 | + * @param string $ETag |
|
60 | + * @param string $content |
|
61 | + * |
|
62 | + * @return array |
|
63 | + */ |
|
64 | + protected function fetch($ETag, $content) { |
|
65 | + /** @var mixed[] $response */ |
|
66 | + $response = parent::fetch($ETag, $content); |
|
67 | 67 | |
68 | - $ncVersion = $this->getVersion(); |
|
69 | - $ncMajorVersion = explode('.', $ncVersion)[0]; |
|
70 | - foreach($response['data'] as $dataKey => $app) { |
|
71 | - $releases = []; |
|
68 | + $ncVersion = $this->getVersion(); |
|
69 | + $ncMajorVersion = explode('.', $ncVersion)[0]; |
|
70 | + foreach($response['data'] as $dataKey => $app) { |
|
71 | + $releases = []; |
|
72 | 72 | |
73 | - // Filter all compatible releases |
|
74 | - foreach($app['releases'] as $release) { |
|
75 | - // Exclude all nightly and pre-releases |
|
76 | - if($release['isNightly'] === false |
|
77 | - && strpos($release['version'], '-') === false) { |
|
78 | - // Exclude all versions not compatible with the current version |
|
79 | - $versionParser = new VersionParser(); |
|
80 | - $version = $versionParser->getVersion($release['rawPlatformVersionSpec']); |
|
81 | - if ( |
|
82 | - // Major version is bigger or equals to the minimum version of the app |
|
83 | - version_compare($ncMajorVersion, $version->getMinimumVersion(), '>=') |
|
84 | - // Major version is smaller or equals to the maximum version of the app |
|
85 | - && version_compare($ncMajorVersion, $version->getMaximumVersion(), '<=') |
|
86 | - ) { |
|
87 | - $releases[] = $release; |
|
88 | - } |
|
89 | - } |
|
90 | - } |
|
73 | + // Filter all compatible releases |
|
74 | + foreach($app['releases'] as $release) { |
|
75 | + // Exclude all nightly and pre-releases |
|
76 | + if($release['isNightly'] === false |
|
77 | + && strpos($release['version'], '-') === false) { |
|
78 | + // Exclude all versions not compatible with the current version |
|
79 | + $versionParser = new VersionParser(); |
|
80 | + $version = $versionParser->getVersion($release['rawPlatformVersionSpec']); |
|
81 | + if ( |
|
82 | + // Major version is bigger or equals to the minimum version of the app |
|
83 | + version_compare($ncMajorVersion, $version->getMinimumVersion(), '>=') |
|
84 | + // Major version is smaller or equals to the maximum version of the app |
|
85 | + && version_compare($ncMajorVersion, $version->getMaximumVersion(), '<=') |
|
86 | + ) { |
|
87 | + $releases[] = $release; |
|
88 | + } |
|
89 | + } |
|
90 | + } |
|
91 | 91 | |
92 | - // Get the highest version |
|
93 | - $versions = []; |
|
94 | - foreach($releases as $release) { |
|
95 | - $versions[] = $release['version']; |
|
96 | - } |
|
97 | - usort($versions, 'version_compare'); |
|
98 | - $versions = array_reverse($versions); |
|
99 | - $compatible = false; |
|
100 | - if(isset($versions[0])) { |
|
101 | - $highestVersion = $versions[0]; |
|
102 | - foreach ($releases as $release) { |
|
103 | - if ((string)$release['version'] === (string)$highestVersion) { |
|
104 | - $compatible = true; |
|
105 | - $response['data'][$dataKey]['releases'] = [$release]; |
|
106 | - break; |
|
107 | - } |
|
108 | - } |
|
109 | - } |
|
110 | - if(!$compatible) { |
|
111 | - unset($response['data'][$dataKey]); |
|
112 | - } |
|
113 | - } |
|
92 | + // Get the highest version |
|
93 | + $versions = []; |
|
94 | + foreach($releases as $release) { |
|
95 | + $versions[] = $release['version']; |
|
96 | + } |
|
97 | + usort($versions, 'version_compare'); |
|
98 | + $versions = array_reverse($versions); |
|
99 | + $compatible = false; |
|
100 | + if(isset($versions[0])) { |
|
101 | + $highestVersion = $versions[0]; |
|
102 | + foreach ($releases as $release) { |
|
103 | + if ((string)$release['version'] === (string)$highestVersion) { |
|
104 | + $compatible = true; |
|
105 | + $response['data'][$dataKey]['releases'] = [$release]; |
|
106 | + break; |
|
107 | + } |
|
108 | + } |
|
109 | + } |
|
110 | + if(!$compatible) { |
|
111 | + unset($response['data'][$dataKey]); |
|
112 | + } |
|
113 | + } |
|
114 | 114 | |
115 | - $response['data'] = array_values($response['data']); |
|
116 | - return $response; |
|
117 | - } |
|
115 | + $response['data'] = array_values($response['data']); |
|
116 | + return $response; |
|
117 | + } |
|
118 | 118 | |
119 | - private function setEndpoint() { |
|
120 | - $versionArray = explode('.', $this->getVersion()); |
|
121 | - $this->endpointUrl = sprintf( |
|
122 | - 'https://apps.nextcloud.com/api/v1/platform/%d.%d.%d/apps.json', |
|
123 | - $versionArray[0], |
|
124 | - $versionArray[1], |
|
125 | - $versionArray[2] |
|
126 | - ); |
|
127 | - } |
|
119 | + private function setEndpoint() { |
|
120 | + $versionArray = explode('.', $this->getVersion()); |
|
121 | + $this->endpointUrl = sprintf( |
|
122 | + 'https://apps.nextcloud.com/api/v1/platform/%d.%d.%d/apps.json', |
|
123 | + $versionArray[0], |
|
124 | + $versionArray[1], |
|
125 | + $versionArray[2] |
|
126 | + ); |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * @param string $version |
|
131 | - */ |
|
132 | - public function setVersion($version) { |
|
133 | - parent::setVersion($version); |
|
134 | - $this->setEndpoint(); |
|
135 | - } |
|
129 | + /** |
|
130 | + * @param string $version |
|
131 | + */ |
|
132 | + public function setVersion($version) { |
|
133 | + parent::setVersion($version); |
|
134 | + $this->setEndpoint(); |
|
135 | + } |
|
136 | 136 | } |
@@ -32,158 +32,158 @@ |
||
32 | 32 | use OCP\ILogger; |
33 | 33 | |
34 | 34 | abstract class Fetcher { |
35 | - const INVALIDATE_AFTER_SECONDS = 300; |
|
36 | - |
|
37 | - /** @var IAppData */ |
|
38 | - protected $appData; |
|
39 | - /** @var IClientService */ |
|
40 | - protected $clientService; |
|
41 | - /** @var ITimeFactory */ |
|
42 | - protected $timeFactory; |
|
43 | - /** @var IConfig */ |
|
44 | - protected $config; |
|
45 | - /** @var Ilogger */ |
|
46 | - protected $logger; |
|
47 | - /** @var string */ |
|
48 | - protected $fileName; |
|
49 | - /** @var string */ |
|
50 | - protected $endpointUrl; |
|
51 | - /** @var string */ |
|
52 | - protected $version; |
|
53 | - |
|
54 | - /** |
|
55 | - * @param Factory $appDataFactory |
|
56 | - * @param IClientService $clientService |
|
57 | - * @param ITimeFactory $timeFactory |
|
58 | - * @param IConfig $config |
|
59 | - * @param ILogger $logger |
|
60 | - */ |
|
61 | - public function __construct(Factory $appDataFactory, |
|
62 | - IClientService $clientService, |
|
63 | - ITimeFactory $timeFactory, |
|
64 | - IConfig $config, |
|
65 | - ILogger $logger) { |
|
66 | - $this->appData = $appDataFactory->get('appstore'); |
|
67 | - $this->clientService = $clientService; |
|
68 | - $this->timeFactory = $timeFactory; |
|
69 | - $this->config = $config; |
|
70 | - $this->logger = $logger; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Fetches the response from the server |
|
75 | - * |
|
76 | - * @param string $ETag |
|
77 | - * @param string $content |
|
78 | - * |
|
79 | - * @return array |
|
80 | - */ |
|
81 | - protected function fetch($ETag, $content) { |
|
82 | - $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
83 | - |
|
84 | - if (!$appstoreenabled) { |
|
85 | - return []; |
|
86 | - } |
|
87 | - |
|
88 | - $options = []; |
|
89 | - |
|
90 | - if ($ETag !== '') { |
|
91 | - $options['headers'] = [ |
|
92 | - 'If-None-Match' => $ETag, |
|
93 | - ]; |
|
94 | - } |
|
95 | - |
|
96 | - $client = $this->clientService->newClient(); |
|
97 | - $response = $client->get($this->endpointUrl, $options); |
|
98 | - |
|
99 | - $responseJson = []; |
|
100 | - if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { |
|
101 | - $responseJson['data'] = json_decode($content, true); |
|
102 | - } else { |
|
103 | - $responseJson['data'] = json_decode($response->getBody(), true); |
|
104 | - $ETag = $response->getHeader('ETag'); |
|
105 | - } |
|
106 | - |
|
107 | - $responseJson['timestamp'] = $this->timeFactory->getTime(); |
|
108 | - $responseJson['ncversion'] = $this->getVersion(); |
|
109 | - if ($ETag !== '') { |
|
110 | - $responseJson['ETag'] = $ETag; |
|
111 | - } |
|
112 | - |
|
113 | - return $responseJson; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Returns the array with the categories on the appstore server |
|
118 | - * |
|
119 | - * @return array |
|
120 | - */ |
|
121 | - public function get() { |
|
122 | - $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
123 | - |
|
124 | - if (!$appstoreenabled) { |
|
125 | - return []; |
|
126 | - } |
|
127 | - |
|
128 | - $rootFolder = $this->appData->getFolder('/'); |
|
129 | - |
|
130 | - $ETag = ''; |
|
131 | - $content = ''; |
|
132 | - |
|
133 | - try { |
|
134 | - // File does already exists |
|
135 | - $file = $rootFolder->getFile($this->fileName); |
|
136 | - $jsonBlob = json_decode($file->getContent(), true); |
|
137 | - if (is_array($jsonBlob)) { |
|
138 | - |
|
139 | - // No caching when the version has been updated |
|
140 | - if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { |
|
141 | - |
|
142 | - // If the timestamp is older than 300 seconds request the files new |
|
143 | - if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
|
144 | - return $jsonBlob['data']; |
|
145 | - } |
|
146 | - |
|
147 | - if (isset($jsonBlob['ETag'])) { |
|
148 | - $ETag = $jsonBlob['ETag']; |
|
149 | - $content = json_encode($jsonBlob['data']); |
|
150 | - } |
|
151 | - } |
|
152 | - } |
|
153 | - } catch (NotFoundException $e) { |
|
154 | - // File does not already exists |
|
155 | - $file = $rootFolder->newFile($this->fileName); |
|
156 | - } |
|
157 | - |
|
158 | - // Refresh the file content |
|
159 | - try { |
|
160 | - $responseJson = $this->fetch($ETag, $content); |
|
161 | - $file->putContent(json_encode($responseJson)); |
|
162 | - return json_decode($file->getContent(), true)['data']; |
|
163 | - } catch (ConnectException $e) { |
|
164 | - $this->logger->logException($e, ['app' => 'appstoreFetcher']); |
|
165 | - return []; |
|
166 | - } catch (\Exception $e) { |
|
167 | - return []; |
|
168 | - } |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Get the currently Nextcloud version |
|
173 | - * @return string |
|
174 | - */ |
|
175 | - protected function getVersion() { |
|
176 | - if ($this->version === null) { |
|
177 | - $this->version = $this->config->getSystemValue('version', '0.0.0'); |
|
178 | - } |
|
179 | - return $this->version; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Set the current Nextcloud version |
|
184 | - * @param string $version |
|
185 | - */ |
|
186 | - public function setVersion($version) { |
|
187 | - $this->version = $version; |
|
188 | - } |
|
35 | + const INVALIDATE_AFTER_SECONDS = 300; |
|
36 | + |
|
37 | + /** @var IAppData */ |
|
38 | + protected $appData; |
|
39 | + /** @var IClientService */ |
|
40 | + protected $clientService; |
|
41 | + /** @var ITimeFactory */ |
|
42 | + protected $timeFactory; |
|
43 | + /** @var IConfig */ |
|
44 | + protected $config; |
|
45 | + /** @var Ilogger */ |
|
46 | + protected $logger; |
|
47 | + /** @var string */ |
|
48 | + protected $fileName; |
|
49 | + /** @var string */ |
|
50 | + protected $endpointUrl; |
|
51 | + /** @var string */ |
|
52 | + protected $version; |
|
53 | + |
|
54 | + /** |
|
55 | + * @param Factory $appDataFactory |
|
56 | + * @param IClientService $clientService |
|
57 | + * @param ITimeFactory $timeFactory |
|
58 | + * @param IConfig $config |
|
59 | + * @param ILogger $logger |
|
60 | + */ |
|
61 | + public function __construct(Factory $appDataFactory, |
|
62 | + IClientService $clientService, |
|
63 | + ITimeFactory $timeFactory, |
|
64 | + IConfig $config, |
|
65 | + ILogger $logger) { |
|
66 | + $this->appData = $appDataFactory->get('appstore'); |
|
67 | + $this->clientService = $clientService; |
|
68 | + $this->timeFactory = $timeFactory; |
|
69 | + $this->config = $config; |
|
70 | + $this->logger = $logger; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Fetches the response from the server |
|
75 | + * |
|
76 | + * @param string $ETag |
|
77 | + * @param string $content |
|
78 | + * |
|
79 | + * @return array |
|
80 | + */ |
|
81 | + protected function fetch($ETag, $content) { |
|
82 | + $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
83 | + |
|
84 | + if (!$appstoreenabled) { |
|
85 | + return []; |
|
86 | + } |
|
87 | + |
|
88 | + $options = []; |
|
89 | + |
|
90 | + if ($ETag !== '') { |
|
91 | + $options['headers'] = [ |
|
92 | + 'If-None-Match' => $ETag, |
|
93 | + ]; |
|
94 | + } |
|
95 | + |
|
96 | + $client = $this->clientService->newClient(); |
|
97 | + $response = $client->get($this->endpointUrl, $options); |
|
98 | + |
|
99 | + $responseJson = []; |
|
100 | + if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { |
|
101 | + $responseJson['data'] = json_decode($content, true); |
|
102 | + } else { |
|
103 | + $responseJson['data'] = json_decode($response->getBody(), true); |
|
104 | + $ETag = $response->getHeader('ETag'); |
|
105 | + } |
|
106 | + |
|
107 | + $responseJson['timestamp'] = $this->timeFactory->getTime(); |
|
108 | + $responseJson['ncversion'] = $this->getVersion(); |
|
109 | + if ($ETag !== '') { |
|
110 | + $responseJson['ETag'] = $ETag; |
|
111 | + } |
|
112 | + |
|
113 | + return $responseJson; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Returns the array with the categories on the appstore server |
|
118 | + * |
|
119 | + * @return array |
|
120 | + */ |
|
121 | + public function get() { |
|
122 | + $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
123 | + |
|
124 | + if (!$appstoreenabled) { |
|
125 | + return []; |
|
126 | + } |
|
127 | + |
|
128 | + $rootFolder = $this->appData->getFolder('/'); |
|
129 | + |
|
130 | + $ETag = ''; |
|
131 | + $content = ''; |
|
132 | + |
|
133 | + try { |
|
134 | + // File does already exists |
|
135 | + $file = $rootFolder->getFile($this->fileName); |
|
136 | + $jsonBlob = json_decode($file->getContent(), true); |
|
137 | + if (is_array($jsonBlob)) { |
|
138 | + |
|
139 | + // No caching when the version has been updated |
|
140 | + if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { |
|
141 | + |
|
142 | + // If the timestamp is older than 300 seconds request the files new |
|
143 | + if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
|
144 | + return $jsonBlob['data']; |
|
145 | + } |
|
146 | + |
|
147 | + if (isset($jsonBlob['ETag'])) { |
|
148 | + $ETag = $jsonBlob['ETag']; |
|
149 | + $content = json_encode($jsonBlob['data']); |
|
150 | + } |
|
151 | + } |
|
152 | + } |
|
153 | + } catch (NotFoundException $e) { |
|
154 | + // File does not already exists |
|
155 | + $file = $rootFolder->newFile($this->fileName); |
|
156 | + } |
|
157 | + |
|
158 | + // Refresh the file content |
|
159 | + try { |
|
160 | + $responseJson = $this->fetch($ETag, $content); |
|
161 | + $file->putContent(json_encode($responseJson)); |
|
162 | + return json_decode($file->getContent(), true)['data']; |
|
163 | + } catch (ConnectException $e) { |
|
164 | + $this->logger->logException($e, ['app' => 'appstoreFetcher']); |
|
165 | + return []; |
|
166 | + } catch (\Exception $e) { |
|
167 | + return []; |
|
168 | + } |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Get the currently Nextcloud version |
|
173 | + * @return string |
|
174 | + */ |
|
175 | + protected function getVersion() { |
|
176 | + if ($this->version === null) { |
|
177 | + $this->version = $this->config->getSystemValue('version', '0.0.0'); |
|
178 | + } |
|
179 | + return $this->version; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Set the current Nextcloud version |
|
184 | + * @param string $version |
|
185 | + */ |
|
186 | + public function setVersion($version) { |
|
187 | + $this->version = $version; |
|
188 | + } |
|
189 | 189 | } |
@@ -28,26 +28,26 @@ |
||
28 | 28 | use OCP\ILogger; |
29 | 29 | |
30 | 30 | class CategoryFetcher extends Fetcher { |
31 | - /** |
|
32 | - * @param Factory $appDataFactory |
|
33 | - * @param IClientService $clientService |
|
34 | - * @param ITimeFactory $timeFactory |
|
35 | - * @param IConfig $config |
|
36 | - * @param ILogger $logger |
|
37 | - */ |
|
38 | - public function __construct(Factory $appDataFactory, |
|
39 | - IClientService $clientService, |
|
40 | - ITimeFactory $timeFactory, |
|
41 | - IConfig $config, |
|
42 | - ILogger $logger) { |
|
43 | - parent::__construct( |
|
44 | - $appDataFactory, |
|
45 | - $clientService, |
|
46 | - $timeFactory, |
|
47 | - $config, |
|
48 | - $logger |
|
49 | - ); |
|
50 | - $this->fileName = 'categories.json'; |
|
51 | - $this->endpointUrl = 'https://apps.nextcloud.com/api/v1/categories.json'; |
|
52 | - } |
|
31 | + /** |
|
32 | + * @param Factory $appDataFactory |
|
33 | + * @param IClientService $clientService |
|
34 | + * @param ITimeFactory $timeFactory |
|
35 | + * @param IConfig $config |
|
36 | + * @param ILogger $logger |
|
37 | + */ |
|
38 | + public function __construct(Factory $appDataFactory, |
|
39 | + IClientService $clientService, |
|
40 | + ITimeFactory $timeFactory, |
|
41 | + IConfig $config, |
|
42 | + ILogger $logger) { |
|
43 | + parent::__construct( |
|
44 | + $appDataFactory, |
|
45 | + $clientService, |
|
46 | + $timeFactory, |
|
47 | + $config, |
|
48 | + $logger |
|
49 | + ); |
|
50 | + $this->fileName = 'categories.json'; |
|
51 | + $this->endpointUrl = 'https://apps.nextcloud.com/api/v1/categories.json'; |
|
52 | + } |
|
53 | 53 | } |