src/Controller/ApiController/DownloadController.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ApiController;
  3. use App\Repository\CorpusRepository;
  4. use App\Repository\ImageRepository;
  5. use App\Repository\ProjectRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  9. use Symfony\Component\HttpFoundation\File\File;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class DownloadController extends AbstractController
  13. {
  14.     private $imageRepository;
  15.     private $corpusRepository;
  16.     private $projectRepository;
  17.     public function __construct(ImageRepository $imageRepositoryEntityManagerInterface $managerCorpusRepository $corpusRepositoryProjectRepository $projectRepository)
  18.     {
  19.         $this->corpusRepository $corpusRepository;
  20.         $this->imageRepository $imageRepository;
  21.         $this->projectRepository $projectRepository;
  22.         $this->em $manager;
  23.     }
  24.     /**
  25.      * @Route("/api/image/{quality}/{id}", name="image_download", methods={"GET"})
  26.      */
  27.     public function image_download($quality,$id): BinaryFileResponse
  28.     {
  29.         if($quality != "highres" && $quality != "thumbnails"){
  30.             throw new NotFoundHttpException('Error : quality format invalid');
  31.         }
  32.         $image $this->imageRepository->find($id);
  33.         if($image == null){
  34.             throw new NotFoundHttpException('Error : invalid ID');
  35.         }
  36.         if($quality == "highres"){
  37.             return $this->file(
  38.                 new File($this->getParameter('CorpusPath')."/highres/".$image->getHighres()),
  39.                 $image->getHighres()
  40.             );
  41.         } else {
  42.             return $this->file(
  43.                 new File($this->getParameter('CorpusPath')."/thumbnails/".$image->getThumbnails()),
  44.                 $image->getThumbnails()
  45.             );
  46.         }
  47.     }
  48.     /**
  49.      * @Route("/api/corpus/download/{id}", name="zip_download", methods={"GET"})
  50.      */
  51.     public function zip_download($id): BinaryFileResponse
  52.     {
  53.         if(!is_dir($this->getParameter('CorpusPath').'/export')){
  54.             mkdir($this->getParameter('CorpusPath').'/export');
  55.         }
  56.         $corpus $this->corpusRepository->find($id);
  57.         if($corpus == null){
  58.             throw new NotFoundHttpException('Error : invalid ID');
  59.         }
  60.         $zipPath $this->corpusRepository->createZip($corpus,$this->getParameter('CorpusPath'));
  61.         return $this->file(
  62.             new File($zipPath),
  63.             $corpus->getTitleFr().'.zip'
  64.         );
  65.     }
  66.     /**
  67.      * @Route("/api/project/download/{code}", name="project_download", methods={"GET"})
  68.      */
  69.     public function project_download($code): BinaryFileResponse
  70.     {
  71.         $project $this->projectRepository->findOneBy(['code' => strtoupper($code)]);
  72.         if($project == null){
  73.             throw new NotFoundHttpException('Error : invalid code');
  74.         }
  75.         return $this->file(
  76.             new File($this->getParameter('ProjectPath')."/".$project->getFileName()),
  77.             $project->getTitle().'.bdnf'
  78.         );
  79.     }
  80. }