mirror of
https://github.com/nextcloud/server.git
synced 2026-06-08 16:26:59 -04:00
fix(OCM): Make the OCM provider stateful so apps can add resources
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
4dbe0677ad
commit
b246d51cbc
4 changed files with 21 additions and 18 deletions
|
|
@ -12,8 +12,8 @@
|
|||
<types>
|
||||
<filesystem/>
|
||||
</types>
|
||||
<category>files</category>
|
||||
<bugs>https://github.com/nextcloud/cloud_federation/issues</bugs>
|
||||
<category>integration</category>
|
||||
<bugs>https://github.com/nextcloud/server/issues</bugs>
|
||||
<dependencies>
|
||||
<nextcloud min-version="28" max-version="28"/>
|
||||
</dependencies>
|
||||
|
|
|
|||
|
|
@ -28,11 +28,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace OCA\CloudFederationAPI;
|
||||
|
||||
use OC\OCM\Model\OCMProvider;
|
||||
use OC\OCM\Model\OCMResource;
|
||||
use OCP\Capabilities\ICapability;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\OCM\Exceptions\OCMArgumentException;
|
||||
use OCP\OCM\IOCMProvider;
|
||||
|
||||
class Capabilities implements ICapability {
|
||||
|
||||
|
|
@ -40,6 +40,7 @@ class Capabilities implements ICapability {
|
|||
|
||||
public function __construct(
|
||||
private IURLGenerator $urlGenerator,
|
||||
private IOCMProvider $provider,
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -63,24 +64,23 @@ class Capabilities implements ICapability {
|
|||
public function getCapabilities() {
|
||||
$url = $this->urlGenerator->linkToRouteAbsolute('cloud_federation_api.requesthandlercontroller.addShare');
|
||||
|
||||
$provider = new OCMProvider();
|
||||
$provider->setEnabled(true);
|
||||
$provider->setApiVersion(self::API_VERSION);
|
||||
$this->provider->setEnabled(true);
|
||||
$this->provider->setApiVersion(self::API_VERSION);
|
||||
|
||||
$pos = strrpos($url, '/');
|
||||
if (false === $pos) {
|
||||
throw new OCMArgumentException('generated route should contains a slash character');
|
||||
}
|
||||
|
||||
$provider->setEndPoint(substr($url, 0, $pos));
|
||||
$this->provider->setEndPoint(substr($url, 0, $pos));
|
||||
|
||||
$resource = new OCMResource();
|
||||
$resource->setName('file')
|
||||
->setShareTypes(['user', 'group'])
|
||||
->setProtocols(['webdav' => '/public.php/webdav/']);
|
||||
|
||||
$provider->setResourceTypes([$resource]);
|
||||
$this->provider->setResourceTypes([$resource]);
|
||||
|
||||
return ['ocm' => $provider->jsonSerialize()];
|
||||
return ['ocm' => $this->provider->jsonSerialize()];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ declare(strict_types=1);
|
|||
namespace OC\OCM;
|
||||
|
||||
use JsonException;
|
||||
use OC\OCM\Model\OCMProvider;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\Http\Client\IClientService;
|
||||
use OCP\ICache;
|
||||
|
|
@ -54,7 +53,8 @@ class OCMDiscoveryService implements IOCMDiscoveryService {
|
|||
ICacheFactory $cacheFactory,
|
||||
private IClientService $clientService,
|
||||
private IConfig $config,
|
||||
private LoggerInterface $logger
|
||||
private IOCMProvider $provider,
|
||||
private LoggerInterface $logger,
|
||||
) {
|
||||
$this->cache = $cacheFactory->createDistributed('ocm-discovery');
|
||||
}
|
||||
|
|
@ -69,13 +69,12 @@ class OCMDiscoveryService implements IOCMDiscoveryService {
|
|||
*/
|
||||
public function discover(string $remote, bool $skipCache = false): IOCMProvider {
|
||||
$remote = rtrim($remote, '/');
|
||||
$provider = new OCMProvider();
|
||||
|
||||
if (!$skipCache) {
|
||||
try {
|
||||
$provider->import(json_decode($this->cache->get($remote) ?? '', true, 8, JSON_THROW_ON_ERROR) ?? []);
|
||||
if ($this->supportedAPIVersion($provider->getApiVersion())) {
|
||||
return $provider; // if cache looks valid, we use it
|
||||
$this->provider->import(json_decode($this->cache->get($remote) ?? '', true, 8, JSON_THROW_ON_ERROR) ?? []);
|
||||
if ($this->supportedAPIVersion($this->provider->getApiVersion())) {
|
||||
return $this->provider; // if cache looks valid, we use it
|
||||
}
|
||||
} catch (JsonException|OCMProviderException $e) {
|
||||
// we ignore cache on issues
|
||||
|
|
@ -96,7 +95,7 @@ class OCMDiscoveryService implements IOCMDiscoveryService {
|
|||
if ($response->getStatusCode() === Http::STATUS_OK) {
|
||||
$body = $response->getBody();
|
||||
// update provider with data returned by the request
|
||||
$provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
|
||||
$this->provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
|
||||
$this->cache->set($remote, $body, 60 * 60 * 24);
|
||||
}
|
||||
} catch (JsonException|OCMProviderException $e) {
|
||||
|
|
@ -109,11 +108,11 @@ class OCMDiscoveryService implements IOCMDiscoveryService {
|
|||
throw new OCMProviderException('error while requesting remote ocm provider');
|
||||
}
|
||||
|
||||
if (!$this->supportedAPIVersion($provider->getApiVersion())) {
|
||||
if (!$this->supportedAPIVersion($this->provider->getApiVersion())) {
|
||||
throw new OCMProviderException('API version not supported');
|
||||
}
|
||||
|
||||
return $provider;
|
||||
return $this->provider;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -124,6 +124,7 @@ use OC\Metadata\Capabilities as MetadataCapabilities;
|
|||
use OC\Metadata\IMetadataManager;
|
||||
use OC\Metadata\MetadataManager;
|
||||
use OC\Notification\Manager;
|
||||
use OC\OCM\Model\OCMProvider;
|
||||
use OC\OCM\OCMDiscoveryService;
|
||||
use OC\OCS\DiscoveryService;
|
||||
use OC\Preview\GeneratorHelper;
|
||||
|
|
@ -232,6 +233,7 @@ use OCP\Lockdown\ILockdownManager;
|
|||
use OCP\Log\ILogFactory;
|
||||
use OCP\Mail\IMailer;
|
||||
use OCP\OCM\IOCMDiscoveryService;
|
||||
use OCP\OCM\IOCMProvider;
|
||||
use OCP\Remote\Api\IApiFactory;
|
||||
use OCP\Remote\IInstanceFactory;
|
||||
use OCP\RichObjectStrings\IValidator;
|
||||
|
|
@ -1426,6 +1428,8 @@ class Server extends ServerContainer implements IServerContainer {
|
|||
|
||||
$this->registerAlias(IPhoneNumberUtil::class, PhoneNumberUtil::class);
|
||||
|
||||
$this->registerAlias(IOCMProvider::class, OCMProvider::class);
|
||||
|
||||
$this->connectDispatcher();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue