vendor/nen/kennisbank-platform/src/Nen/Controller/ImagesController.php line 29

Open in your IDE?
  1. <?php
  2. namespace Nen\Bundle\KennisbankPlatformBundle\Controller;
  3. use League\Glide\Server;
  4. use League\Glide\Signatures\SignatureException;
  5. use League\Glide\Signatures\SignatureInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Intervention\Image\Exception\NotReadableException;
  11. use League\Flysystem\FilesystemInterface;
  12. use Symfony\Component\HttpFoundation\StreamedResponse;
  13. class ImagesController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/image/{path}", name="images_server", requirements={"path"=".+"})
  17.      *
  18.      * @param string             $path
  19.      * @param Request            $request
  20.      * @param SignatureInterface $signature
  21.      * @param Server             $server
  22.      * @return Response
  23.      */
  24.     public function server(string $pathRequest $requestSignatureInterface $signatureServer $serverFilesystemInterface $filesystem)
  25.     {
  26.         $options $request->query->all();
  27.         try {
  28.             $signature->validateRequest($path$options);
  29.         } catch (SignatureException $e) {
  30.             throw $this->createNotFoundException();
  31.         }
  32.         // @todo(guido): needs to be a different method to retrieve the prefix.
  33.         if (strpos($path'publisher/') === 0) {
  34.             $path 'publisher://'.$path;
  35.         } elseif (strpos($path'uploads/') === 0) {
  36.             $path substr($path8);
  37.             $path 'uploads://'.$path;
  38.         } else {
  39.             $path 'cms://' $path;
  40.         }
  41.         try {
  42.             return $server->getImageResponse($path$options);
  43.         } catch (NotReadableException $e) {
  44.             $stream $filesystem->readStream($path);
  45.             $response = new StreamedResponse(
  46.                 function() use ($stream) {
  47.                     $outputStream fopen('php://output''wb');
  48.                     $fileStream $stream;
  49.                     stream_copy_to_stream($fileStream$outputStream);
  50.                 });
  51.             $detector = new \League\MimeTypeDetection\FinfoMimeTypeDetector();
  52.             $mimeType $detector->detectMimeTypeFromPath($path);
  53.             $response->headers->set('Content-Type'$mimeType);
  54.             return $response;
  55.         }
  56.     }
  57. }