src/Controller/Api/CsvDownload/CsvDownloadController.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\CsvDownload;
  3. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  4. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class CsvDownloadController
  8. {
  9.     private string $baseDir;
  10.     public function __construct(string $dataOperatorDirectory)
  11.     {
  12.         $this->baseDir realpath($dataOperatorDirectory);
  13.     }
  14.     public function __invoke(Request $requeststring $entityint $cpv): BinaryFileResponse
  15.     {
  16.         $codeCluster $request->query->get('codeCluster'null);
  17.         $codeInsee $request->query->get('codeInsee'null);
  18.         $codeIris $request->query->get('codeIris'null);
  19.         $subDir '';
  20.         if (strtoupper($entity) === 'PRODUCTION' || strtoupper($entity) === 'PRISES_PARC' || strtoupper($entity) === 'MOBILE') {
  21.             $subDir "dataSFR/processed/" strtoupper($entity) . "/csv_by_cpv";
  22.             if ($codeIris) {
  23.                 $subDir "dataSFR/processed/" strtoupper($entity) . "/csv_by_cod_iris";
  24.             }
  25.         } else {
  26.             throw new NotFoundHttpException('Type d\'entité non reconnu.');
  27.         }
  28.         $fullBasePath $this->baseDir DIRECTORY_SEPARATOR $subDir;
  29.         $fullBasePath realpath($fullBasePath);
  30.         if (!$fullBasePath || !str_starts_with($fullBasePath$this->baseDir)) {
  31.             throw new NotFoundHttpException('Répertoire inaccessible ou invalide.');
  32.         }
  33.         // Lister les sous-dossiers
  34.         $folders array_filter(glob($fullBasePath '/*'), 'is_dir');
  35.         // Trier par date décroissante
  36.         usort($folders, fn($a$b) => filemtime($b) <=> filemtime($a));
  37.         foreach ($folders as $folder) {
  38.             $files glob("$folder/{$cpv}_*.csv");
  39.             if (strtoupper($entity) === 'PRISES_PARC') {
  40.                 $files glob("$folder/{$cpv}/{$cpv}_*.csv");
  41.             }
  42.             if ($codeCluster) {
  43.                 $files glob("$folder/{$cpv}/{$codeCluster}/{$codeCluster}*.csv");
  44.                 if ($codeInsee) {
  45.                     $files glob("$folder/{$cpv}/{$codeCluster}/{$codeInsee}*.csv");
  46.                 }
  47.             }
  48.             if ($codeIris) {
  49.                 $files glob("$folder/{$codeIris}/{$codeIris}*.csv");
  50.             }
  51.             if (!empty($files)) {
  52.                 $filePath realpath($files[0]);
  53.                 // Vérification finale
  54.                 if ($filePath && str_starts_with($filePath$this->baseDir) && is_file($filePath)) {
  55.                     $response = new BinaryFileResponse($filePath);
  56.                     $response->setContentDisposition(
  57.                         ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  58.                         basename($filePath)
  59.                     );
  60.                     return $response;
  61.                 }
  62.             }
  63.         }
  64.         // Aucun fichier trouvé
  65.         throw new NotFoundHttpException('Fichier correspondant non trouvé.');
  66.     }
  67. }