Skip to content

Commit

Permalink
Merge pull request #91 from ConductionNL/feature/AXCVDWOF-12/public-p…
Browse files Browse the repository at this point in the history
…ublication-attachments

Added a public endpoint for getting publication attachments & Let's make sure MetaData Source always has a default value
  • Loading branch information
WilcoLouwerse authored Oct 14, 2024
2 parents ad01888 + 13421f1 commit 34f5e4d
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 23 deletions.
6 changes: 3 additions & 3 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Create a [feature request](https://github.com/OpenCatalogi/.github/issues/new/ch
<version>0.6.5</version>
<licence>agpl</licence>
<author mail="[email protected]" homepage="https://www.conduction.nl/">Conduction</author>
<author mail="[email protected]" homepage="https://acato.nl/">Acato</author>
<author mail="[email protected]" homepage="https://acato.nl/">Acato</author>
<namespace>OpenCatalogi</namespace>
<documentation>
<user>https://conduction.gitbook.io/opencatalogi-nextcloud/gebruikers</user>
Expand All @@ -40,13 +40,13 @@ Create a [feature request](https://github.com/OpenCatalogi/.github/issues/new/ch
<screenshot>https://raw.githubusercontent.com/ConductionNL/opencatalogi/master/docs/assets/logo.png</screenshot>
<screenshot>https://raw.githubusercontent.com/ConductionNL/opencatalogi/master/docs/screenshots/1.png</screenshot>
<screenshot>https://raw.githubusercontent.com/ConductionNL/opencatalogi/master/docs/screenshots/2.png</screenshot>

<dependencies>
<php min-version="8.0" min-int-size="64"/>
<database min-version="10">pgsql</database>
<database>sqlite</database>
<database min-version="8.0">mysql</database>

<lib>curl</lib>
<lib>zip</lib>

Expand Down
11 changes: 6 additions & 5 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
['name' => 'metadata#page', 'url' => '/metadata', 'verb' => 'GET'],
['name' => 'publications#page', 'url' => '/publications', 'verb' => 'GET'],
['name' => 'publications#attachments', 'url' => '/api/publications/{id}/attachments', 'verb' => 'GET', 'requirements' => ['id' => '.+']],
['name' => 'publications#attachmentsInternal', 'url' => '/api/publications/{id}/attachments/internal', 'verb' => 'GET', 'requirements' => ['id' => '.+']],
['name' => 'publications#download', 'url' => '/api/publications/{id}/download', 'verb' => 'GET', 'requirements' => ['id' => '.+']],
['name' => 'catalogi#page', 'url' => '/catalogi', 'verb' => 'GET'],
['name' => 'search#index', 'url' => '/search', 'verb' => 'GET'],
['name' => 'search#index', 'url' => '/api/search', 'verb' => 'GET'],
['name' => 'search#indexInternal', 'url' => '/api/search/internal', 'verb' => 'GET'],
['name' => 'search#show', 'url' => '/api/search/{id}', 'verb' => 'GET'],
['name' => 'search#showInternal', 'url' => '/api/search/internal/{id}', 'verb' => 'GET'],
['name' => 'search#show', 'url' => '/api/search/{id}', 'verb' => 'GET', 'requirements' => ['id' => '.+']],
['name' => 'search#showInternal', 'url' => '/api/search/internal/{id}', 'verb' => 'GET', 'requirements' => ['id' => '.+']],
['name' => 'search#preflighted_cors', 'url' => '/api/{path}', 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']],
['name' => 'themes#index', 'url' => '/search/themes', 'verb' => 'GET'],
['name' => 'themes#index', 'url' => '/api/search/themes', 'verb' => 'GET'],
['name' => 'themes#indexInternal', 'url' => '/api/themes', 'verb' => 'GET'],
['name' => 'themes#show', 'url' => '/api/search/themes/{id}', 'verb' => 'GET'],
['name' => 'themes#showInternal', 'url' => '/api/themes/{id}', 'verb' => 'GET'],
['name' => 'themes#show', 'url' => '/api/search/themes/{id}', 'verb' => 'GET', 'requirements' => ['id' => '.+']],
['name' => 'themes#showInternal', 'url' => '/api/themes/{id}', 'verb' => 'GET', 'requirements' => ['id' => '.+']],
['name' => 'directory#page', 'url' => '/directory', 'verb' => 'GET'],
['name' => 'directory#synchronise', 'url' => '/api/directory/{id}/sync', 'verb' => 'GET'],
['name' => 'directory#synchronise', 'url' => '/api/directory/{id}/sync', 'verb' => 'GET', 'requirements' => ['id' => '.+']],
['name' => 'configuration#index', 'url' => '/configuration', 'verb' => 'GET'],
['name' => 'configuration#create', 'url' => '/configuration', 'verb' => 'POST']
],
Expand Down
20 changes: 13 additions & 7 deletions lib/Controller/MetaDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function page(?string $getParameter)
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index(ObjectService $objectService, SearchService $searchService): JSONResponse
public function index(ObjectService $objectService, SearchService $searchService, IURLGenerator $urlGenerator): JSONResponse
{
$filters = $this->request->getParams();
unset($filters['_route']);
Expand Down Expand Up @@ -76,6 +76,11 @@ public function index(ObjectService $objectService, SearchService $searchService
$result = $objectService->findObjects(filters: $filters, config: $dbConfig);

$results = ["results" => $result['documents']];
foreach ($results['results'] as &$result) {
if (empty($result['source']) === true) {
$result['source'] = $urlGenerator->getAbsoluteURL($urlGenerator->linkToRoute(routeName:"opencatalogi.metadata.show", arguments: ['id' => $result['_id']]));
}
}
return new JSONResponse($results);
}

Expand All @@ -84,7 +89,7 @@ public function index(ObjectService $objectService, SearchService $searchService
* @NoAdminRequired
* @NoCSRFRequired
*/
public function show(string|int $id, ObjectService $objectService): JSONResponse
public function show(string|int $id, ObjectService $objectService, IURLGenerator $urlGenerator): JSONResponse
{
if ($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
Expand All @@ -103,6 +108,10 @@ public function show(string|int $id, ObjectService $objectService): JSONResponse

$result = $objectService->findObject(filters: $filters, config: $dbConfig);

if (empty($result['source']) === true) {
$result['source'] = $urlGenerator->getAbsoluteURL($urlGenerator->linkToRoute(routeName:"opencatalogi.metadata.show", arguments: ['id' => $id]));
}

return new JSONResponse($result);
}

Expand All @@ -113,13 +122,11 @@ public function show(string|int $id, ObjectService $objectService): JSONResponse
*/
public function create(ObjectService $objectService, IURLGenerator $urlGenerator): JSONResponse
{

$data = $this->request->getParams();

// Remove fields we should never post
unset($data['id']);


foreach ($data as $key => $value) {
if (str_starts_with($key, '_')) {
unset($data[$key]);
Expand All @@ -139,7 +146,6 @@ public function create(ObjectService $objectService, IURLGenerator $urlGenerator
$this->metaDataMapper->update($object);
}


return new JSONResponse($object);
}
$dbConfig['base_uri'] = $this->config->getValueString(app: $this->appName, key: 'mongodbLocation');
Expand All @@ -154,9 +160,9 @@ public function create(ObjectService $objectService, IURLGenerator $urlGenerator
);

if (isset($data['source']) === false || $data['source'] === null) {
$returnData['source'] = $urlGenerator->getAbsoluteURL($urlGenerator->linkToRoute(routeName:"opencatalogi.metadata.show", arguments: ['id' => $returnData['id']]));
$data['source'] = $urlGenerator->getAbsoluteURL($urlGenerator->linkToRoute(routeName:"opencatalogi.metadata.show", arguments: ['id' => $returnData['id']]));
$returnData = $objectService->saveObject(
data: $returnData,
data: $data,
config: $dbConfig
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/OrganisationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function page(): TemplateResponse
}

/**
* Return (and serach) all objects
* Return (and search) all objects
*
* @NoAdminRequired
* @NoCSRFRequired
Expand Down
52 changes: 48 additions & 4 deletions lib/Controller/PublicationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,16 @@ public function index(ObjectService $objectService, SearchService $searchService
}

/**
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
* Return all attachments for given publication.
*
* @param string|int $id The id.
* @param ObjectService $objectService The Object Service.
* @param array|null $publication A publication array.
*
* @return JSONResponse The Response.
* @throws GuzzleException
*/
public function attachments(string|int $id, ObjectService $objectService, ?array $publication = null): JSONResponse
private function publicationsAttachments(string|int $id, ObjectService $objectService, ?array $publication = null): JSONResponse
{
if ($publication === null) {
$publication = $this->getPublicationData(id: $id, objectService: $objectService);
Expand Down Expand Up @@ -183,6 +188,45 @@ public function attachments(string|int $id, ObjectService $objectService, ?array
return new JSONResponse(['results' => $result['documents']]);
}

/**
* Return all attachments for given publication.
*
* @CORS
* @PublicPage
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string|int $id The id.
* @param ObjectService $objectService The Object Service.
* @param array|null $publication A publication array.
*
* @return JSONResponse The Response.
* @throws GuzzleException
*/
public function attachments(string|int $id, ObjectService $objectService, ?array $publication = null): JSONResponse
{
return $this->publicationsAttachments($id, $objectService, $publication);
}

/**
* Return all attachments for given publication.
*
* @PublicPage
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string|int $id The id.
* @param ObjectService $objectService The Object Service.
* @param array|null $publication A publication array.
*
* @return JSONResponse The Response.
* @throws GuzzleException
*/
public function attachmentsInternal(string|int $id, ObjectService $objectService, ?array $publication = null): JSONResponse
{
return $this->publicationsAttachments($id, $objectService, $publication);
}


/**
* Gets a publication for the given id (if it exists) and returns its data as an array.
Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/ThemesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private function themesIndex(ObjectService $objectService, SearchService $search
}

/**
* Return (and serach) all objects
* Return (and search) all objects
*
* @CORS
* @PublicPage
Expand All @@ -105,7 +105,7 @@ public function index(ObjectService $objectService, SearchService $searchService
}

/**
* Return (and serach) all objects
* Return (and search) all objects
*
* @PublicPage
* @NoAdminRequired
Expand Down
57 changes: 56 additions & 1 deletion lib/Db/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,57 @@ public function __construct() {
$this->addType(fieldName: 'required', type: 'json');
$this->addType(fieldName: 'properties', type: 'json');
$this->addType(fieldName: 'source', type: 'string');

// Set the source URL using the id if available
$this->setSourceUrl();
}

// Override setId method to update the source URL when id is set
public function setId(int $id): void {
parent::setId($id);
$this->setSourceUrl();
}

// Method to set the source URL dynamically based on the entity id and Nextcloud's domain
public function setSourceUrl(): void {
// Get the current URL of the Nextcloud installation
$baseUrl = $this->getBaseUrl();

// Set the source dynamically if the id is available
if ($this->id !== null) {
$this->setSource($baseUrl . '/index.php/apps/opencatalogi/api/metadata/' . $this->id);
}
}

// Method to get the base URL of the Nextcloud installation
private function getBaseUrl(): string {
// Determine the scheme (http or https)
$scheme = $this->isHttps() ? 'https' : 'http';
// Get the server host and port using $_SERVER
$host = $_SERVER['SERVER_NAME'] ?? 'localhost';
$port = $_SERVER['SERVER_PORT'] ?? 80;

// Construct the base URL (including port if it's not default HTTP/HTTPS ports)
$baseUrl = $scheme . '://' . $host;
if (($scheme === 'http' && $port !== 80) || ($scheme === 'https' && $port !== 443)) {
$baseUrl .= ':' . $port;
}

return $baseUrl;
}

// Check if the current connection is HTTPS
private function isHttps(): bool {
return (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off');
}

public function setSource(?string $source): self {
if ($source === null) {
$this->setSourceUrl();
}
$this->source = $source;

return $this;
}

public function getJsonFields(): array
Expand All @@ -47,7 +98,7 @@ public function hydrate(array $object): self
$value = null;
}

$method = 'set'.ucfirst($key);
$method = 'set' . ucfirst($key);

try {
$this->$method($value);
Expand Down Expand Up @@ -85,6 +136,10 @@ public function jsonSerialize(): array
}
}

if (empty($this->source) === true) {
$this->setSourceUrl();
}

$array = [
'id' => $this->id,
'title' => $this->title,
Expand Down

0 comments on commit 34f5e4d

Please sign in to comment.