/**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  *
  * @return false|string
  */
 private function getMediaType(MediaInterface $media)
 {
     if ($media->getContentType() == 'video/x-flv') {
         return 'video';
     } elseif (substr($media->getContentType(), 0, 5) == 'image') {
         return 'image';
     }
     return false;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = array())
 {
     // build the default headers
     $headers = array_merge(array('Content-Type' => $media->getContentType(), 'Content-Disposition' => sprintf('attachment; filename="%s"', $media->getMetadataValue('filename'))), $headers);
     if (!in_array($mode, array('http', 'X-Sendfile', 'X-Accel-Redirect'))) {
         throw new \RuntimeException('Invalid mode provided');
     }
     if ($mode == 'http') {
         if ($format == 'reference') {
             $file = $this->getReferenceFile($media);
         } else {
             $file = $this->getFilesystem()->get($this->generatePrivateUrl($media, $format));
         }
         return new StreamedResponse(function () use($file) {
             echo $file->getContent();
         }, 200, $headers);
     }
     if (!$this->getFilesystem()->getAdapter() instanceof \Sonata\MediaBundle\Filesystem\Local) {
         throw new \RuntimeException('Cannot use X-Sendfile or X-Accel-Redirect with non \\Sonata\\MediaBundle\\Filesystem\\Local');
     }
     $filename = sprintf('%s/%s', $this->getFilesystem()->getAdapter()->getDirectory(), $this->generatePrivateUrl($media, $format));
     return new BinaryFileResponse($filename, 200, $headers);
 }
Exemplo n.º 3
0
 /**
  * Set media binary content according to request content.
  *
  * @param MediaInterface $media
  */
 protected function generateBinaryFromRequest(MediaInterface $media)
 {
     if (php_sapi_name() === 'cli') {
         throw new \RuntimeException('The current process cannot be executed in cli environment');
     }
     if (!$media->getContentType()) {
         throw new \RuntimeException('You must provide the content type value for your media before setting the binary content');
     }
     $request = $media->getBinaryContent();
     if (!$request instanceof Request) {
         throw new \RuntimeException('Expected Request in binary content');
     }
     $content = $request->getContent();
     // create unique id for media reference
     $guesser = ExtensionGuesser::getInstance();
     $extension = $guesser->guess($media->getContentType());
     if (!$extension) {
         throw new \RuntimeException(sprintf('Unable to guess extension for content type %s', $media->getContentType()));
     }
     $handle = tmpfile();
     fwrite($handle, $content);
     $file = new ApiMediaFile($handle);
     $file->setExtension($extension);
     $file->setMimetype($media->getContentType());
     $media->setBinaryContent($file);
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = array())
 {
     // build the default headers
     $headers = array_merge(array('Content-Type' => $media->getContentType(), 'Content-Disposition' => sprintf('attachment; filename="%s"', $media->getMetadataValue('filename'))), $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) {
             echo $provider->getReferenceFile($media)->getContent();
         }, 200, $headers);
     }
     $headers[$mode] = $this->generatePrivateUrl($media, $format);
     return new Response('', 200, $headers);
 }