-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add metadata_endpoint to external storage configuration
- Loading branch information
1 parent
0f2d6b8
commit b93bc84
Showing
10 changed files
with
598 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,36 @@ | |
|
||
class ExternalStorageService { | ||
|
||
public function __construct(private LoggerInterface $logger, private IRootFolder $rootFolder, private GlobalStoragesService $globalStoragesService) {} | ||
public function __construct(private LoggerInterface $logger, private IRootFolder $rootFolder, private GlobalStoragesService $globalStoragesService, private HttpRequestService $httpClient) {} | ||
|
||
/** | ||
* Get the metadata from the external storage metadata endpoint for specific file | ||
* @param IUser $nextcloudUser Nextcloud user associated with the session | ||
* @param int $fileId File ID to search for | ||
* @return array | ||
* @throws Exception | ||
*/ | ||
public function getMetadataForSpecificFile(IUser $nextcloudUser, int $fileId): array { | ||
try { | ||
$externalStorageConfiguration = $this->getExternalStorageConfigurationForSpecificFile($nextcloudUser, $fileId, true); | ||
|
||
if (!isset($externalStorageConfiguration['error'])) { | ||
$requestBody = [ | ||
"param1" => "[email protected]", | ||
"param2" => "/CIDgravity/next.pdf" | ||
]; | ||
|
||
$response = $this->httpClient->post($externalStorageConfiguration['metadata_endpoint'], $requestBody); | ||
return ['metadata' => $response]; | ||
|
||
} else { | ||
return $externalStorageConfiguration; | ||
} | ||
|
||
} catch (Exception $e) { | ||
return ['message' => 'error getting external storage config', 'error' => $e->getMessage()]; | ||
} | ||
} | ||
|
||
/** | ||
* Get the external storage configuration to which a specific fileId belongs to | ||
|
@@ -42,7 +71,7 @@ public function __construct(private LoggerInterface $logger, private IRootFolder | |
* @return array | ||
* @throws Exception | ||
*/ | ||
public function getExternalStorageConfigurationForSpecificFile(IUser $nextcloudUser, int $fileId): array { | ||
public function getExternalStorageConfigurationForSpecificFile(IUser $nextcloudUser, int $fileId, bool $includeAuthSettings): array { | ||
try { | ||
$userFolder = $this->rootFolder->getUserFolder($nextcloudUser->getUID()); | ||
$files = $userFolder->getById($fileId); | ||
|
@@ -59,7 +88,7 @@ public function getExternalStorageConfigurationForSpecificFile(IUser $nextcloudU | |
// loop through each external storage to find the one related to your file | ||
foreach ($externalStorages as $externalStorage) { | ||
if ($this->isFileInExternalStorage($file, $externalStorage)) { | ||
return $this->buildExternalStorageConfiguration($externalStorage); | ||
return $this->buildExternalStorageConfiguration($externalStorage, $includeAuthSettings); | ||
} | ||
} | ||
|
||
|
@@ -75,12 +104,24 @@ public function getExternalStorageConfigurationForSpecificFile(IUser $nextcloudU | |
* @param StorageConfig $externalStorage External storage to build configuration for | ||
* @return array | ||
*/ | ||
private function buildExternalStorageConfiguration(StorageConfig $externalStorage): array { | ||
private function buildExternalStorageConfiguration(StorageConfig $externalStorage, bool $includeAuthSettings): array { | ||
|
||
$this->logger->error("buildExternalStorageConfiguration", [ | ||
'externalStorage' => json_encode($externalStorage), | ||
]); | ||
|
||
$configuration = []; | ||
$configuration['is_cidgravity'] = $externalStorage->getBackend()->getIdentifier() == "cidgravity"; | ||
$configuration['id'] = $externalStorage->getId(); | ||
$configuration['host'] = $externalStorage->getBackendOption('host'); | ||
$configuration['mountpoint'] = $externalStorage->getMountPoint(); | ||
$configuration['metadata_endpoint'] = $externalStorage->getBackendOption('metadata_endpoint'); | ||
|
||
// check if we need to include auth settings (for metadata call only, not exposed to frontend) | ||
if ($includeAuthSettings) { | ||
$configuration['user'] = $externalStorage->getBackendOption('user'); | ||
$configuration['password'] = $externalStorage->getBackendOption('password'); | ||
} | ||
|
||
return $configuration; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (c) 2024, CIDgravity (https://cidgravity.com) | ||
* | ||
* @author Florian RUEN <[email protected]> | ||
* | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* 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, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OCA\CidgravityGateway\Service; | ||
|
||
use Exception; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class HttpRequestService { | ||
|
||
private $ch; | ||
|
||
public function __construct(private LoggerInterface $logger) { | ||
$this->ch = curl_init(); | ||
} | ||
|
||
public function post($url, $data, $headers = ['Content-Type: application/json']) { | ||
$jsonData = json_encode($data); | ||
|
||
// set CURL configuration | ||
curl_setopt($this->ch, CURLOPT_URL, $url); | ||
curl_setopt($this->ch, CURLOPT_POST, true); | ||
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonData); | ||
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers); | ||
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); | ||
|
||
// execute the request | ||
$response = curl_exec($this->ch); | ||
|
||
// check for errors | ||
if ($error = curl_errno($this->ch)) { | ||
$errorMessage = curl_error($this->ch); | ||
throw new Exception("cURL Error: $errorMessage"); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
public function __destruct() { | ||
curl_close($this->ch); | ||
} | ||
} |
Oops, something went wrong.