diff options
author | Bjoern Schiessle <bjoern@schiessle.org> | 2018-05-04 15:25:02 +0200 |
---|---|---|
committer | Bjoern Schiessle <bjoern@schiessle.org> | 2018-07-02 11:29:27 +0200 |
commit | a3948e8a126d6f84629841c8886fe0819ab04ad5 (patch) | |
tree | 55e8458dfc9cf257f3ec6b5a40c9d0ff03e73d67 /lib/private/Federation | |
parent | 6208f250e88a15794ac5b7eeef6d701aa91e131b (diff) | |
download | nextcloud-server-a3948e8a126d6f84629841c8886fe0819ab04ad5.tar.gz nextcloud-server-a3948e8a126d6f84629841c8886fe0819ab04ad5.zip |
use new API to send a federated share if possible
Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
Diffstat (limited to 'lib/private/Federation')
-rw-r--r-- | lib/private/Federation/CloudFederationProviderManager.php | 73 | ||||
-rw-r--r-- | lib/private/Federation/CloudFederationShare.php | 6 |
2 files changed, 74 insertions, 5 deletions
diff --git a/lib/private/Federation/CloudFederationProviderManager.php b/lib/private/Federation/CloudFederationProviderManager.php index 73e1dd99c61..dcf666ca266 100644 --- a/lib/private/Federation/CloudFederationProviderManager.php +++ b/lib/private/Federation/CloudFederationProviderManager.php @@ -29,6 +29,8 @@ use OCP\Federation\ICloudFederationNotification; use OCP\Federation\ICloudFederationProvider; use OCP\Federation\ICloudFederationProviderManager; use OCP\Federation\ICloudFederationShare; +use OCP\Federation\ICloudIdManager; +use OCP\Http\Client\IClientService; /** * Class Manager @@ -45,9 +47,26 @@ class CloudFederationProviderManager implements ICloudFederationProviderManager /** @var IAppManager */ private $appManager; - public function __construct(IAppManager $appManager) { + /** @var IClientService */ + private $httpClientService; + + /** @var ICloudIdManager */ + private $cloudIdManager; + + /** + * CloudFederationProviderManager constructor. + * + * @param IAppManager $appManager + * @param IClientService $httpClientService + * @param ICloudIdManager $cloudIdManager + */ + public function __construct(IAppManager $appManager, + IClientService $httpClientService, + ICloudIdManager $cloudIdManager) { $this->cloudFederationProvider= []; $this->appManager = $appManager; + $this->httpClientService = $httpClientService; + $this->cloudIdManager = $cloudIdManager; } @@ -103,7 +122,32 @@ class CloudFederationProviderManager implements ICloudFederationProviderManager } public function sendShare(ICloudFederationShare $share) { - // TODO: Implement sendShare() method. + $ocmEndPoint = $this->getOCMEndPoint($share->getShareWith()); + + if (empty($ocmEndPoint)) { + return false; + } + + $client = $this->httpClientService->newClient(); + try { + $response = $client->post($ocmEndPoint . '/shares', [ + 'body' => $share->getShare(), + 'timeout' => 10, + 'connect_timeout' => 10, + ]); + $result['result'] = $response->getBody(); + $result['success'] = true; + } catch (\Exception $e) { + // if flat re-sharing is not supported by the remote server + // we re-throw the exception and fall back to the old behaviour. + // (flat re-shares has been introduced in Nextcloud 9.1) + if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) { + throw $e; + } + } + + return true; + } public function sendNotification(ICloudFederationNotification $notification) { @@ -118,5 +162,30 @@ class CloudFederationProviderManager implements ICloudFederationProviderManager public function isReady() { return $this->appManager->isEnabledForUser('cloud_federation_api', false); } + /** + * check if server supports the new OCM api and ask for the correct end-point + * + * @param string $recipient full federated cloud ID of the recipient of a share + * @return string + */ + protected function getOCMEndPoint($recipient) { + $cloudId = $this->cloudIdManager->resolveCloudId($recipient); + $client = $this->httpClientService->newClient(); + try { + $response = $client->get($cloudId->getRemote() . '/ocm-provider/', ['timeout' => 10, 'connect_timeout' => 10]); + } catch (\Exception $e) { + return ''; + } + + $result = $response->getBody(); + $result = json_decode($result, true); + + if (isset($result['end-point'])) { + return $result['end-point']; + } + + return ''; + } + } diff --git a/lib/private/Federation/CloudFederationShare.php b/lib/private/Federation/CloudFederationShare.php index 4622dc096d7..5bc172ab6b6 100644 --- a/lib/private/Federation/CloudFederationShare.php +++ b/lib/private/Federation/CloudFederationShare.php @@ -203,14 +203,14 @@ class CloudFederationShare implements ICloudFederationShare { } /** - * get JSON encoded share, ready to send out + * get the whole share, ready to send out * - * @return string + * @return array * * @since 14.0.0 */ public function getShare() { - return json_encode($this->share); + return $this->share; } /** |