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