Commit 07ea9148 authored by François Agneray's avatar François Agneray
Browse files

Merge branch '64-gestion-des-donnees' into 'develop'

Resolve "Gestion des données"

Closes #64

See merge request !57
parents 988f9eef 407658df
Pipeline #4854 passed with stages
in 2 minutes and 51 seconds
......@@ -84,6 +84,10 @@ $container->set('App\Action\TableListAction', function (ContainerInterface $c) {
return new App\Action\TableListAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$container->set('App\Action\AdminFileExplorerAction', function (ContainerInterface $c) {
return new App\Action\AdminFileExplorerAction($c->get('settings')['data_path']);
});
$container->set('App\Action\ProjectListAction', function (ContainerInterface $c) {
return new App\Action\ProjectListAction($c->get('em'));
});
......@@ -185,6 +189,10 @@ $container->set('App\Action\SearchAction', function (ContainerInterface $c) {
);
});
$container->set('App\Action\DatasetFileExplorerAction', function (ContainerInterface $c) {
return new App\Action\DatasetFileExplorerAction($c->get('em'), $c->get('settings')['data_path'], $c->get(SETTINGS)['token']);
});
$container->set('App\Action\DownloadFileAction', function (ContainerInterface $c) {
return new App\Action\DownloadFileAction($c->get('em'), $c->get(SETTINGS)['token']);
return new App\Action\DownloadFileAction($c->get('em'), $c->get('settings')['data_path'], $c->get(SETTINGS)['token']);
});
......@@ -22,6 +22,7 @@ $app->group('', function (RouteCollectorProxy $group) {
$group->map([OPTIONS, GET, POST], '/database', App\Action\DatabaseListAction::class);
$group->map([OPTIONS, GET, PUT, DELETE], '/database/{id}', App\Action\DatabaseAction::class);
$group->map([OPTIONS, GET], '/database/{id}/table', App\Action\TableListAction::class);
$group->map([OPTIONS, GET], '/file-explorer/[{fpath:.*}]', App\Action\AdminFileExplorerAction::class);
})->add(new App\Middleware\RouteGuardMiddleware(
boolval($container->get(SETTINGS)['token']['enabled']),
array(GET, POST, PUT, DELETE),
......@@ -65,4 +66,5 @@ $app->group('', function (RouteCollectorProxy $group) {
));
$app->get('/search/{dname}', App\Action\SearchAction::class);
$app->get('/dataset-file-explorer/{dname}/[{fpath:.*}]', App\Action\DatasetFileExplorerAction::class);
$app->get('/download-file/{dname}/[{fpath:.*}]', App\Action\DownloadFileAction::class);
......@@ -25,6 +25,7 @@ return [
'password' => getenv('DATABASE_CO_PASSWORD')
],
],
'data_path' => getenv('DATA_PATH'),
'logger' => [
'name' => getenv('LOGGER_NAME'),
'path' => getenv('LOGGER_PATH'),
......
This diff is collapsed.
......@@ -70,7 +70,7 @@ curl -d '{"name":"colibri","label":"Colibri Project Test","description":"Project
curl -d '{"label":"Default dataset family","display":10}' --header 'Content-Type: application/json' -X POST http://localhost/instance/default/dataset-family
curl -d '{"name":"obs_cat","table_ref":"obs_cat","label":"ObsCat dataset","description":"ObsCat","display":10,"count":10000,"vo":false,"data_path":"/mnt/mount","config":{"cone_search":{"enabled":true,"column_ra":2,"column_dec":3},"selectable_row":true},"public":true,"project_name":"anis_project"}' --header 'Content-Type: application/json' -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"observations","table_ref":"observations_info","label":"Observations dataset","description":"Observations","display":20,"count":177454,"vo":false,"data_path":"/mnt/mount","config":{"selectable_row":false},"public":true,"project_name":"anis_project"}' --header 'Content-Type: application/json' -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"vipers_dr2_w1","table_ref":"aspic_vipers_dr2_w1","label":"VIPERS-W1 (DR2)","description":"VIPERS W1 dataset","display":30,"count":1000,"vo":false,"data_path":"/mnt/mount","config":{"selectable_row":false},"public":true,"project_name":"anis_project"}' --header 'Content-Type: application/json' -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"vipers_dr2_w1","table_ref":"aspic_vipers_dr2_w1","label":"VIPERS-W1 (DR2)","description":"VIPERS W1 dataset","display":30,"count":1000,"vo":false,"data_path":"ASPIC/VIPERS_DR2","config":{"selectable_row":false},"public":true,"project_name":"anis_project"}' --header 'Content-Type: application/json' -X POST http://localhost/dataset-family/1/dataset
curl -d '{"name":"gama_g02_dr3","table_ref":"aspic_gama_g02","label":"GAMA G02 (DR3)","description":"GAMA G02 dataset","display":40,"count":8,"vo":false,"data_path":"/mnt/mount","config":{"selectable_row":false},"public":true,"project_name":"anis_project"}' --header 'Content-Type: application/json' -X POST http://localhost/dataset-family/1/dataset
# Add ANIS obs_cat default criteria family, default output family and default output category
......
......@@ -14,6 +14,7 @@ services:
DATABASE_CO_DBNAME: "anis_metamodel"
DATABASE_CO_USER: "postgres"
DATABASE_CO_PASSWORD: "postgres"
DATA_PATH: "/data"
LOGGER_NAME: "anis-metamodel"
LOGGER_PATH: "php://stderr"
LOGGER_LEVEL: "debug"
......@@ -26,7 +27,7 @@ services:
- .:/project
- ./conf-dev/dev-php.ini:/usr/local/etc/php/conf.d/dev-php.ini
- ./conf-dev/vhost.conf:/etc/apache2/sites-available/000-default.conf
- ./conf-dev/data-dev:/data/ASPIC
- ./conf-dev/data-dev:/data
- ./conf-dev/public_key:/data/public_key
keycloak:
......
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Doctrine\ORM\EntityManagerInterface;
use Slim\Exception\HttpBadRequestException;
use Nyholm\Psr7\Factory\Psr17Factory;
final class AdminFileExplorerAction
{
/**
* Contains anis-server data path
*
* @var string
*/
private $dataPath;
/**
* Create the classe before call __invoke to execute the action
*
* @param string $dataPath Contains anis-server data path
*/
public function __construct(string $dataPath)
{
$this->dataPath = $dataPath;
}
/**
* `GET` Returns the list of files in the directory
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param ResponseInterface $response PSR-7 This object represents the HTTP response
* @param string[] $args This table contains information transmitted in the URL (see routes.php)
*
* @return ResponseInterface
*/
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
}
$path = realpath($this->dataPath);
if (array_key_exists('fpath', $args)) {
$path .= DIRECTORY_SEPARATOR . $args['fpath'];
}
if (is_file($path)) {
throw new HttpBadRequestException(
$request,
'Path ' . $args['fpath'] . ' is a file'
);
}
$files = array();
foreach (scandir($path) as $file) {
$files[] = array(
'name' => $file,
'size' => filesize($path . DIRECTORY_SEPARATOR . $file),
'type' => filetype($path . DIRECTORY_SEPARATOR . $file),
'mimetype' => mime_content_type($path . DIRECTORY_SEPARATOR . $file)
);
}
$response->getBody()->write(json_encode($files));
return $response;
}
}
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Doctrine\ORM\EntityManagerInterface;
use Slim\Exception\HttpNotFoundException;
use Nyholm\Psr7\Factory\Psr17Factory;
final class DatasetFileExplorerAction extends AbstractAction
{
/**
* Contains anis-server data path
*
* @var string
*/
private $dataPath;
/**
* Contains settings to handle Json Web Token
*
* @var array
*/
private $settings;
/**
* Create the classe before call __invoke to execute the action
*
* @param EntityManagerInterface $em Doctrine Entity Manager Interface
* @param string $dataPath Contains anis-server data path
* @param array $settings Settings about token
*/
public function __construct(EntityManagerInterface $em, string $dataPath, array $settings)
{
parent::__construct($em);
$this->dataPath = $dataPath;
$this->settings = $settings;
}
/**
* `GET` Returns the list of files in the directory
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param ResponseInterface $response PSR-7 This object represents the HTTP response
* @param string[] $args This table contains information transmitted in the URL (see routes.php)
*
* @return ResponseInterface
*/
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
}
// Search the correct dataset with primary key
$dataset = $this->em->find('App\Entity\Dataset', $args['dname']);
// If dataset is not found 404
if (is_null($dataset)) {
throw new HttpNotFoundException(
$request,
'Dataset with name ' . $args['dname'] . ' is not found'
);
}
// If dataset is private and authorization enabled
if (!$dataset->getPublic() && boolval($this->settings['enabled'])) {
$this->verifyDatasetAuthorization($request, $dataset->getName(), $this->settings['admin_role']);
}
$path = realpath($this->dataPath) . DIRECTORY_SEPARATOR . $dataset->getDataPath();
if (array_key_exists('fpath', $args)) {
$path .= DIRECTORY_SEPARATOR . $args['fpath'];
}
if (is_file($path)) {
throw new HttpBadRequestException(
$request,
'Path ' . $args['fpath'] . ' is a file'
);
}
$files = array();
foreach (scandir($path) as $file) {
$files[] = array(
'name' => $file,
'size' => filesize($path . DIRECTORY_SEPARATOR . $file),
'type' => filetype($path . DIRECTORY_SEPARATOR . $file),
'mimetype' => mime_content_type($path . DIRECTORY_SEPARATOR . $file)
);
}
$response->getBody()->write(json_encode($files));
return $response;
}
}
......@@ -20,6 +20,13 @@ use Nyholm\Psr7\Factory\Psr17Factory;
final class DownloadFileAction extends AbstractAction
{
/**
* Contains anis-server data path
*
* @var string
*/
private $dataPath;
/**
* Contains settings to handle Json Web Token
*
......@@ -30,12 +37,14 @@ final class DownloadFileAction extends AbstractAction
/**
* Create the classe before call __invoke to execute the action
*
* @param EntityManagerInterface $em Doctrine Entity Manager Interface
* @param array $settings Settings about token
* @param EntityManagerInterface $em Doctrine Entity Manager Interface
* @param string $dataPath Contains anis-server data path
* @param array $settings Settings about token
*/
public function __construct(EntityManagerInterface $em, array $settings)
public function __construct(EntityManagerInterface $em, string $dataPath, array $settings)
{
parent::__construct($em);
$this->dataPath = $dataPath;
$this->settings = $settings;
}
......@@ -71,7 +80,9 @@ final class DownloadFileAction extends AbstractAction
}
// Search the file
$filePath = realpath($dataset->getDataPath()) . DIRECTORY_SEPARATOR . $args['fpath'];
$filePath = $this->dataPath
. DIRECTORY_SEPARATOR . $dataset->getDataPath()
. DIRECTORY_SEPARATOR . $args['fpath'];
// If the file not found 404
if (!file_exists($filePath)) {
......
......@@ -26,7 +26,7 @@ final class DownloadFileActionTest extends TestCase
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\DownloadFileAction($this->entityManager, array());
$this->action = new \App\Action\DownloadFileAction($this->entityManager, '', array());
}
public function testOptionsHttpMethod(): void
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment