vendor/nen/kennisbank-platform/src/Nen/Controller/FileController.php line 42

Open in your IDE?
  1. <?php
  2. namespace Nen\Bundle\KennisbankPlatformBundle\Controller;
  3. use League\Flysystem\FilesystemInterface;
  4. use League\Glide\Signatures\SignatureException;
  5. use League\Glide\Signatures\SignatureInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  8. use Symfony\Component\HttpFoundation\HeaderUtils;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\StreamedResponse;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class FileController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/file/{path}", name="file_server", requirements={"path"=".+"})
  17.      *
  18.      * @param string             $path
  19.      * @param Request            $request
  20.      * @return Response
  21.      */
  22.     public function server(string $pathRequest $requestFilesystemInterface $storage)
  23.     {
  24.         // @todo(guido): needs to be a different method to retrieve the prefix.
  25.         if (strpos($path'publisher/') === 0) {
  26.             $path 'publisher://' $path;
  27.         } else {
  28.             $path 'cms://' $path;
  29.         }
  30.         $response = new StreamedResponse(function () use ($path$storage) {
  31.             $outputStream fopen('php://output''wb');
  32.             $fileStream $storage->readStream($pathfalse);
  33.             stream_copy_to_stream($fileStream$outputStream);
  34.         });
  35.         $response->headers->set('Content-Type'$storage->getMimetype($path));
  36.         $disposition HeaderUtils::makeDisposition(
  37.             HeaderUtils::DISPOSITION_ATTACHMENT,
  38.             basename($path)
  39.         );
  40.         $response->headers->set('Content-Disposition'$disposition);
  41.         return $response;
  42.     }
  43. }