Commit 9df09d0f authored by François Agneray's avatar François Agneray
Browse files

Add env data_path and admin file explorer

parent 988f9eef
......@@ -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'));
});
......
......@@ -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('/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'),
......
......@@ -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"
......
<?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;
}
}
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