src/Controller/DownloadController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Vetoadom\Document;
  4. use App\Service\CustomJsonResponse;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\File\File;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class DownloadController extends AbstractController
  16. {
  17.     private $urlGenerator;
  18.     private $vetoadomEntityManager;
  19.     private $customJsonResponse;
  20.     public function __construct(
  21.         UrlGeneratorInterface $urlGenerator,
  22.         EntityManagerInterface $vetoadomEntityManager,
  23.         CustomJsonResponse $customJsonResponse
  24.     )
  25.     {
  26.         $this->urlGenerator $urlGenerator;
  27.         $this->vetoadomEntityManager $vetoadomEntityManager;
  28.         $this->customJsonResponse $customJsonResponse;
  29.     }
  30.     /**
  31.      * @Route("/download/{documentId}/{action}/{filename}", requirements={"action": "save|display"}, name="app_download", methods={"GET"})
  32.      * @param int $documentId
  33.      * @param string $action
  34.      * @param string $filename
  35.      * @return Response
  36.      */
  37.     public function download(int $documentId 0string $action "display"string $filename ''): Response
  38.     {
  39.         $filter = [
  40.             'id' => $documentId,
  41.             'active' => 1
  42.         ];
  43.         $document $this->vetoadomEntityManager->getRepository(Document::class)->findOneBy($filter);
  44.         if (
  45.             !$document
  46.         ){
  47.             throw $this->createNotFoundException();
  48.         }
  49.         if (($file $this->documentParams($document)) === false){
  50.             throw $this->createNotFoundException();
  51.         }
  52.         // save
  53.         if ($action == "save") {
  54.             return $this->file($file$document->getNom());
  55.         }
  56.         // display the file contents in the browser instead of downloading it
  57.         if ($filename === '') {
  58.             return new RedirectResponse($this->urlGenerator->generate('app_download', ['documentId' => $document->getId(), 'action' => 'display''filename' => $document->getNom()]));
  59.         }
  60.         return $this->file($file$document->getNom(), ResponseHeaderBag::DISPOSITION_INLINE);
  61.     }
  62.     /**
  63.      * @Route("/download/{document_id}", name="app_download_delete", methods={"DELETE"})
  64.      * @param int $document_id
  65.      * @return JsonResponse
  66.      */
  67.     public function downloadDelete(int $document_id 0): JsonResponse
  68.     {
  69.         $document $this->vetoadomEntityManager->getRepository(Document::class)->findOneBy(['id' => $document_id'active' => 1]);
  70.         if (
  71.             !$document
  72.         ){
  73.             return $this->customJsonResponse->sendResponse();
  74.         }
  75.         $document->setActive(false);
  76.         $this->vetoadomEntityManager->flush();
  77.         return $this->customJsonResponse->sendResponse();
  78.     }
  79.     /**
  80.      * @Route("/downloads/{folder}/{file}", name="app_downloads", methods={"GET"})
  81.      * @param Request $request
  82.      * @param string $folder
  83.      * @param string $file
  84.      * @return Response
  85.      */
  86.     public function downloads(Request $requeststring $folder ''string $file ''): Response
  87.     {
  88.         $folder preg_replace("/[^\p{L}0-9 _-]/u",'',rawurldecode($folder));
  89.         $path_parts pathinfo(rawurldecode($file));
  90.         $filename preg_replace("/[^\p{L}0-9 _-]/u",'',$path_parts['filename']);
  91.         $extension preg_replace("/[^\p{L} _-]/u",'',$path_parts['extension']);
  92.         $document $this->getParameter('downloads_path') . '/';
  93.         if ($folder != '') {
  94.             $document .= $folder '/';
  95.         }
  96.         $document .= $filename '.' $extension;
  97.         if (!file_exists($document)){
  98.             throw $this->createNotFoundException();
  99.         }
  100.         // save
  101.         if ($request->get('action') == "save") {
  102.             return $this->file($document);
  103.         }
  104.         return $this->file($documentnullResponseHeaderBag::DISPOSITION_INLINE);
  105.     }
  106.     public function documentParams(Document $document){
  107.         $path sprintf("%s/%s",
  108.             $this->getParameter("documents_directory"),
  109.             $document->getId()
  110.         );
  111.         if (!file_exists($path)){
  112.             return false;
  113.         }
  114.         // load the file from the filesystem
  115.         return new File($path);
  116.     }
  117.     /**
  118.      * @param Document $document
  119.      * @param string $for
  120.      * @return Response|string
  121.      */
  122.     public function downloadLogo(Document $documentstring $for 'html'){
  123.         $file $this->documentParams($document);
  124.         $logo sprintf("data:%s;base64,%s",
  125.             $file->getMimeType(),
  126.             base64_encode(file_get_contents($file->getPathname()))
  127.         );
  128.         if ($for === 'html')
  129.             return new Response($logo,Response::HTTP_OK);
  130.         return $logo;
  131.     }
  132. }