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

#51 => done

parent b1e08e9c
Pipeline #2338 passed with stages
in 4 minutes and 43 seconds
......@@ -8,7 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- #42: The GET method of the InstanceListAction returns the list of available instances with **the number of dataset families** and the **number of available datasets** for each instance
- #45: New route which allows to return the distinct values ​​of an attribute for one dataset used to generate the criterion option list => **/dataset/{name}/attribute/{id}/distinct**
- #46: Adding param "f" to export result in different format (json, csv, ascii) for the SearchAction
- #50: Middleware to log metamodel sql requests added (only for the dev mode)
- #51: Adding an action to download files associated to a dataset
### Changed
- #44: Adding the doctrine production mode (see readme)
......
......@@ -160,6 +160,10 @@ $container->set('App\Action\SearchAction', function (ContainerInterface $c) {
);
});
$container->set('App\Action\DownloadFileAction', function (ContainerInterface $c) {
return new App\Action\DownloadFileAction($c->get('em'));
});
// Middlewares
$container->set('App\Middleware\AuthorizationMiddleware', function (ContainerInterface $c) {
return new App\Middleware\AuthorizationMiddleware($c->get('em'), $c->get(SETTINGS)['token_options']);
......
......@@ -36,3 +36,4 @@ $app->map([OPTIONS, GET], '/dataset/{name}/attribute', App\Action\AttributeListA
$app->map([OPTIONS, GET, PUT], '/dataset/{name}/attribute/{id}', App\Action\AttributeAction::class);
$app->map([OPTIONS, GET, PUT], '/dataset/{name}/attribute/{id}/distinct', App\Action\AttributeDistinctAction::class);
$app->get('/search/{dname}', App\Action\SearchAction::class);
$app->get('/download-file/{dname}/[{fpath:.*}]', App\Action\DownloadFileAction::class);
......@@ -23,6 +23,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
db:
image: postgres
......
<?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 Slim\Exception\HttpNotFoundException;
use Nyholm\Psr7\Factory\Psr17Factory;
final class DownloadFileAction extends AbstractAction
{
/**
* `GET` Returns the file found
*
* @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['name'] . ' is not found'
);
}
// Search the file
$filePath = realpath($dataset->getDataPath()) . DIRECTORY_SEPARATOR . $args['fpath'];
// If the file not found 404
if (!file_exists($filePath)) {
throw new HttpNotFoundException(
$request,
'File with path ' . $args['fpath'] . ' is not found for the dataset ' . $args['dname']
);
}
// If the file found so stream it
$psr17Factory = new Psr17Factory();
$stream = $psr17Factory->createStreamFromFile($filePath, 'r');
return $response->withBody($stream)
->withHeader('Content-Disposition', 'attachment; filename=' . basename($filePath) . ';')
->withHeader('Content-Type', mime_content_type($filePath))
->withHeader('Content-Length', filesize($filePath));
}
}
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