Example #1
0
 /**
  * {@inheritdoc}
  */
 public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = array())
 {
     // build the default headers
     $headers = array_merge(array('Content-Type' => $media->getContentType()), $headers);
     if (!in_array($mode, array('http', 'X-Sendfile', 'X-Accel-Redirect'))) {
         throw new \RuntimeException('Invalid mode provided');
     }
     if ($mode == 'http') {
         $provider = $this;
         return new StreamedResponse(function () use($provider, $media, $format) {
             if ($format == 'reference') {
                 echo $provider->getReferenceFile($media)->getContent();
             } else {
                 echo $provider->getFilesystem()->get($provider->generatePrivateUrl($media, $format), true)->getContent();
             }
         }, 200, $headers);
     }
     if (!$this->getFilesystem()->getAdapter() instanceof \Zym\Bundle\MediaBundle\Filesystem\Local) {
         throw new \RuntimeException('Cannot use X-Sendfile or X-Accel-Redirect with non \\Zym\\Bundle\\MediaBundle\\Filesystem\\Local');
     }
     $headers[$mode] = sprintf('%s/%s', $this->getFilesystem()->getAdapter()->getDirectory(), $this->generatePrivateUrl($media, $format));
     return new Response('', 200, $headers);
 }