Move to a service to also fetch the capabilities when the settings are updated

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2019-02-06 21:13:08 +01:00
Родитель afe96a4593
Коммит c37d0a7e5f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: F941078878347C0C
3 изменённых файлов: 103 добавлений и 54 удалений

Просмотреть файл

@ -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;
}
}

Просмотреть файл

@ -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',

Просмотреть файл

@ -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;
}
}