src/Hitso/Bundle/FileManagerBundle/Controller/Front/DownloadController.php line 82

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\FileManagerBundle\Controller\Front;
  4. use Hitso\Bundle\CommonBundle\Controller\Controller;
  5. use Hitso\Bundle\FileManagerBundle\Entity\DisposableLink;
  6. use Hitso\Bundle\FileManagerBundle\Entity\Download;
  7. use Hitso\Bundle\FileManagerBundle\Entity\File;
  8. use Hitso\Bundle\FileManagerBundle\Manager\DisposableLinkManager;
  9. use Hitso\Bundle\FileManagerBundle\Manager\FileManager;
  10. use Hitso\Bundle\FileManagerBundle\Repository\DownloadRepository;
  11. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  14. use Symfony\Component\HttpFoundation\StreamedResponse;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class DownloadController extends Controller
  17. {
  18.     /**
  19.      * @var FileManager
  20.      */
  21.     protected $fileManager;
  22.     /**
  23.      * @var DisposableLinkManager
  24.      */
  25.     protected $disposableLinkManager;
  26.     public function __construct(FileManager $fileManagerDisposableLinkManager $disposableLinkManager)
  27.     {
  28.         $this->fileManager           $fileManager;
  29.         $this->disposableLinkManager $disposableLinkManager;
  30.     }
  31.     /**
  32.      * @Route("/share/{token}", name="fm_share", defaults={"token"=null})
  33.      */
  34.     public function shareAction(string $token)
  35.     {
  36.         $disposableLink $this->disposableLinkManager->getRepository()->findOneBy(['token' => $token]);
  37.         if ($disposableLink instanceof DisposableLink && $disposableLink->isValid()) {
  38.             $file     $disposableLink->getFile();
  39.             $fileName $this->fileManager->getFs()->getFullPathname($file);
  40.             $response = new StreamedResponse(
  41.                 function () use ($fileName) {
  42.                     readfile($fileName);
  43.                 }, 200, [
  44.                     'Content-Type'        => 'application/octet-stream',
  45.                     'Content-Disposition' => 'attachment;filename="' $file->getName() . '"',
  46.                 ]
  47.             );
  48.             $this->disposableLinkManager->removeResource($disposableLink);
  49.             return $response;
  50.         }
  51.         throw $this->createNotFoundException('Podany plik nie istnieje lub link utracił ważność.');
  52.     }
  53.     /**
  54.      * @Route("/file/stream/{id}/{name}", name="fm_file", defaults={"id"=null,"name"=null})
  55.      */
  56.     public function streamAction(File $file): BinaryFileResponse
  57.     {
  58.         if ($file->isDisposable()) {
  59.             return $this->redirectToRoute('homepage');
  60.         }
  61.         $path $this->fileManager->getFs()->getFullPathname($file);
  62.         return $this->file($path$file->getName(), ResponseHeaderBag::DISPOSITION_INLINE);
  63.     }
  64.     /**
  65.      * @Route("/file/download/{id}", name="file_download", defaults={"id"=null})
  66.      */
  67.     public function downloadAction(File $file): StreamedResponse
  68.     {
  69.         if ($file->isDisposable()) {
  70.             return $this->redirectToRoute('homepage');
  71.         }
  72.         $fileName $this->fileManager->getFs()->getFullPathname($file);
  73.         $response = new StreamedResponse(
  74.             function () use ($fileName) {
  75.                 readfile($fileName);
  76.             }, 200, [
  77.                 'Content-Type'        => 'application/octet-stream',
  78.                 'Content-Disposition' => 'attachment;filename="' $file->getName() . '"',
  79.             ]
  80.         );
  81.         return $response;
  82.     }
  83. }