Skip to content

Commit

Permalink
feat: add metadata_endpoint to external storage configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRuen committed Jun 19, 2024
1 parent 0f2d6b8 commit b93bc84
Show file tree
Hide file tree
Showing 10 changed files with 598 additions and 22 deletions.
3 changes: 2 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'cidgravitygateway' => ['url' => '/cidgravitygateway']
],
'ocs' => [
['name' => 'externalStorage#getExternalStorageConfigurationForSpecificFile', 'url' => '/get-external-storage-config', 'verb' => 'GET']
['name' => 'externalStorage#getExternalStorageConfigurationForSpecificFile', 'url' => '/get-external-storage-config', 'verb' => 'GET'],
['name' => 'externalStorage#getMetadataForSpecificFile', 'url' => '/get-file-metadata', 'verb' => 'GET']
],
];
33 changes: 32 additions & 1 deletion lib/Controller/ExternalStorageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getExternalStorageConfigurationForSpecificFile(int $fileId): Dat
return new DataResponse(['error' => 'user not logged in'], Http::STATUS_INTERNAL_SERVER_ERROR);
}

$externalStorageConfiguration = $this->externalStorageService->getExternalStorageConfigurationForSpecificFile($user, $fileId);
$externalStorageConfiguration = $this->externalStorageService->getExternalStorageConfigurationForSpecificFile($user, $fileId, false);

if (!isset($externalStorageConfiguration['error'])) {
return new DataResponse(['success' => true, 'configuration' => $externalStorageConfiguration], Http::STATUS_OK);
Expand All @@ -51,4 +51,35 @@ public function getExternalStorageConfigurationForSpecificFile(int $fileId): Dat
return new DataResponse(['success' => false, 'error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}

/**
* @NoAdminRequired
* @NoCSRFRequired
* Return the metadata for specific file from metadata endpoint from external storage configuration
* @return DataResponse
*/
public function getMetadataForSpecificFile(int $fileId): DataResponse {
try {

if (!is_int($fileId)) {
return new DataResponse(['error' => 'invalid param fileId provided'], Http::STATUS_BAD_REQUEST);
}

$user = $this->userSession->getUser();
if (!$user) {
return new DataResponse(['error' => 'user not logged in'], Http::STATUS_INTERNAL_SERVER_ERROR);
}

$fileMetadata = $this->externalStorageService->getMetadataForSpecificFile($user, $fileId);

if (!isset($fileMetadata['error'])) {
return new DataResponse(['success' => true, 'metadata' => $fileMetadata], Http::STATUS_OK);
}

return new DataResponse(['success' => false, 'error' => $fileMetadata['message']], Http::STATUS_INTERNAL_SERVER_ERROR);

} catch (Exception $e) {
return new DataResponse(['success' => false, 'error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
}
2 changes: 1 addition & 1 deletion lib/Service/BackendService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(IL10N $l, Password $legacyAuth) {
->setText($l->t('CIDgravity'))
->addParameters([
new DefinitionParameter('host', $l->t('URL')),
new DefinitionParameter('metadata_url', $l->t('METADATA_URL')),
new DefinitionParameter('metadata_endpoint', $l->t('Metadata endpoint')),
(new DefinitionParameter('root', $l->t('Remote subfolder')))
->setFlag(DefinitionParameter::FLAG_OPTIONAL),
(new DefinitionParameter('secure', $l->t('Secure https://')))
Expand Down
49 changes: 45 additions & 4 deletions lib/Service/ExternalStorageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand All @@ -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;
}
Expand Down
62 changes: 62 additions & 0 deletions lib/Service/HttpRequestService.php
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);
}
}
Loading

0 comments on commit b93bc84

Please sign in to comment.