diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2019-02-06 23:13:08 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2019-02-06 23:13:08 +0300 |
commit | c37d0a7e5f4f656cd42b7400163b409e8163e328 (patch) | |
tree | b7fa64b21e2cea3db2d890a8015267d8489a1903 /lib | |
parent | afe96a4593fda23b1fc73746c489905704280647 (diff) |
Move to a service to also fetch the capabilities when the settings are updated
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Backgroundjobs/ObtainCapabilities.php | 59 | ||||
-rw-r--r-- | lib/Controller/SettingsController.php | 8 | ||||
-rw-r--r-- | lib/Service/CapabilitiesService.php | 90 |
3 files changed, 103 insertions, 54 deletions
diff --git a/lib/Backgroundjobs/ObtainCapabilities.php b/lib/Backgroundjobs/ObtainCapabilities.php index e22c7d70..2103d632 100644 --- a/lib/Backgroundjobs/ObtainCapabilities.php +++ b/lib/Backgroundjobs/ObtainCapabilities.php @@ -24,6 +24,7 @@ namespace OCA\Richdocuments\Backgroundjobs; use OC\BackgroundJob\TimedJob; +use OCA\Richdocuments\Service\CapabilitiesService; use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFolder; @@ -32,64 +33,16 @@ use OCP\IConfig; class ObtainCapabilities extends TimedJob { - /** @var IConfig */ - private $config; - /** @var IClientService */ - private $clientService; - /** @var ISimpleFolder */ - private $appData; + /** @var CapabilitiesService */ + private $capabilitiesService; - public function __construct(IConfig $config, IClientService $clientService, IAppData $appData) { - $this->config = $config; - $this->clientService = $clientService; - try { - $this->appData = $appData->getFolder('richdocuments'); - } catch (NotFoundException $e) { - $this->appData = $appData->newFolder('richdocuments'); - } + public function __construct(CapabilitiesService $capabilitiesService) { + $this->capabilitiesService = $capabilitiesService; $this->setInterval(60*60); } protected function run($argument) { - try { - $file = $this->appData->getFile('capabilities.json'); - } catch (NotFoundException $e) { - $file = $this->appData->newFile('capabilities.json'); - } - - $capabilties = $this->renewCapabilities(); - $file->putContent(json_encode($capabilties)); + $this->capabilitiesService->refretch(); } - - private function renewCapabilities() { - $remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url'); - if ($remoteHost === '') { - return []; - } - $capabilitiesEndpoint = $remoteHost . '/hosting/capabilities'; - - $client = $this->clientService->newClient(); - try { - $response = $client->get( - $capabilitiesEndpoint, - [ - 'timeout' => 5, - ] - ); - } catch (\Exception $e) { - return []; - } - - $responseBody = $response->getBody(); - $ret = \json_decode($responseBody, true); - - if (!is_array($ret)) { - return []; - } - - return $ret; - } - - } diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index 122cf116..8c56f9d1 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -11,6 +11,7 @@ namespace OCA\Richdocuments\Controller; +use OCA\Richdocuments\Service\CapabilitiesService; use OCA\Richdocuments\WOPI\DiscoveryManager; use \OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; @@ -31,6 +32,8 @@ class SettingsController extends Controller{ private $discoveryManager; /** @var string */ private $userId; + /** @var CapabilitiesService */ + private $capabilitiesService; /** * @param string $appName @@ -47,13 +50,15 @@ class SettingsController extends Controller{ AppConfig $appConfig, IConfig $config, DiscoveryManager $discoveryManager, - $userId) { + $userId, + CapabilitiesService $capabilitiesService) { parent::__construct($appName, $request); $this->l10n = $l10n; $this->appConfig = $appConfig; $this->config = $config; $this->discoveryManager = $discoveryManager; $this->userId = $userId; + $this->capabilitiesService = $capabilitiesService; } /** @@ -117,6 +122,7 @@ class SettingsController extends Controller{ } $this->discoveryManager->refretch(); + $this->capabilitiesService->refretch(); $response = [ 'status' => 'success', diff --git a/lib/Service/CapabilitiesService.php b/lib/Service/CapabilitiesService.php new file mode 100644 index 00000000..cd1549e2 --- /dev/null +++ b/lib/Service/CapabilitiesService.php @@ -0,0 +1,90 @@ +<?php +/** + * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Richdocuments\Service; + +use OCP\Files\IAppData; +use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFolder; +use OCP\Http\Client\IClientService; +use OCP\IConfig; + +class CapabilitiesService { + + /** @var IConfig */ + private $config; + /** @var IClientService */ + private $clientService; + /** @var ISimpleFolder */ + private $appData; + + public function __construct(IConfig $config, IClientService $clientService, IAppData $appData) { + $this->config = $config; + $this->clientService = $clientService; + try { + $this->appData = $appData->getFolder('richdocuments'); + } catch (NotFoundException $e) { + $this->appData = $appData->newFolder('richdocuments'); + } + } + + public function refretch() { + try { + $file = $this->appData->getFile('capabilities.json'); + } catch (NotFoundException $e) { + $file = $this->appData->newFile('capabilities.json'); + } + + $capabilties = $this->renewCapabilities(); + $file->putContent(json_encode($capabilties)); + } + + private function renewCapabilities() { + $remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url'); + if ($remoteHost === '') { + return []; + } + $capabilitiesEndpoint = $remoteHost . '/hosting/capabilities'; + + $client = $this->clientService->newClient(); + try { + $response = $client->get( + $capabilitiesEndpoint, + [ + 'timeout' => 5, + ] + ); + } catch (\Exception $e) { + return []; + } + + $responseBody = $response->getBody(); + $ret = \json_decode($responseBody, true); + + if (!is_array($ret)) { + return []; + } + + return $ret; + } +} |