Exemplo n.º 1
0
    /**
     * @abstract
     * @param \Sonata\MediaBundle\Model\MediaInterface $media
     * @return string
     */
    public function generatePath(MediaInterface $media)
    {
        $rep_first_level = (int) ($media->getId() / $this->firstLevel);
        $rep_second_level = (int) (($media->getId() - ($rep_first_level * $this->firstLevel)) / $this->secondLevel);

        return sprintf('%s/%04s/%02s', $media->getContext(), $rep_first_level + 1, $rep_second_level + 1);
    }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function getBox(MediaInterface $media, array $settings)
 {
     $size = $media->getBox();
     $skipBox = false;
     $constraint = array_key_exists('constraint', $settings) && $settings['constraint'] === false;
     if ($constraint && $size->getHeight() > $size->getWidth()) {
         $settings['width'] = $settings['height'];
         $this->mode = ImageInterface::THUMBNAIL_INSET;
         $skipBox = true;
     }
     $hasWidth = array_key_exists('width', $settings) && $settings['width'] > 0;
     $hasHeight = array_key_exists('height', $settings) && $settings['height'] > 0;
     if (!$hasWidth && !$hasHeight) {
         throw new \RuntimeException(sprintf('Width/Height parameter is missing in context "%s" for provider "%s". Please add at least one parameter.', $media->getContext(), $media->getProviderName()));
     }
     if ($hasWidth && $hasHeight && !$skipBox) {
         return new Box($settings['width'], $settings['height']);
     }
     if (!$hasHeight) {
         $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
     }
     if (!$hasWidth) {
         $settings['width'] = (int) ($settings['height'] * $size->getWidth() / $size->getHeight());
     }
     return $this->computeBox($size, $settings);
 }
Exemplo n.º 3
0
 /**
  * Updates a media
  *
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  * @param string $context
  * @param string $providerName
  * @return void
  */
 public function save(MediaInterface $media, $context = null, $providerName = null)
 {
     if ($context) {
         $media->setContext($context);
     }
     if ($providerName) {
         $media->setProviderName($providerName);
     }
     $isNew = $media->getId() != null;
     if ($isNew) {
         $this->pool->getProvider($media->getProviderName())->prePersist($media);
     } else {
         $this->pool->getProvider($media->getProviderName())->preUpdate($media);
     }
     $this->dm->persist($media);
     $this->dm->flush();
     if ($isNew) {
         $this->pool->getProvider($media->getProviderName())->postPersist($media);
     } else {
         $this->pool->getProvider($media->getProviderName())->postUpdate($media);
     }
     // just in case the pool alter the media
     $this->dm->persist($media);
     $this->dm->flush();
 }
Exemplo n.º 4
0
 /**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  *
  * @return string the file extension for the $media, or the $defaultExtension if not available
  */
 protected function getExtension(MediaInterface $media)
 {
     $ext = $media->getExtension();
     if (!is_string($ext) || strlen($ext) < 3) {
         $ext = $this->defaultFormat;
     }
     return $ext;
 }
 /**
  * @param MediaInterface $media
  */
 protected function fixBinaryContent(MediaInterface $media)
 {
     if (!$media->getBinaryContent()) {
         return;
     }
     if (preg_match("/www.dailymotion.com\\/video\\/([0-9a-zA-Z]*)_/", $media->getBinaryContent(), $matches)) {
         $media->setBinaryContent($matches[1]);
     }
 }
 /**
  * @param MediaInterface $media
  */
 protected function fixBinaryContent(MediaInterface $media)
 {
     if (!$media->getBinaryContent()) {
         return;
     }
     if (preg_match("/vimeo\\.com\\/(video\\/|)(\\d+)/", $media->getBinaryContent(), $matches)) {
         $media->setBinaryContent($matches[2]);
     }
 }
 /**
  * Replace 4/3 by 16/9 format
  * Possible Formats :
  * default
  * mqdefault -> medium quality
  * hqdefault -> high quality
  * sddefault
  * maxresdefault
  *
  * {@inheritdoc}
  */
 public function getReferenceImage(MediaInterface $media)
 {
     $url = $media->getMetadataValue('thumbnail_url');
     $pattern = '/\\bhqdefault\\b/i';
     $sddefault_url = preg_replace($pattern, 'sddefault', $url);
     //var_dump($sddefault_url);
     //exit();
     return $sddefault_url;
 }
Exemplo n.º 8
0
    public function getHelperProperties(MediaInterface $media, $format, $options = array())
    {
        $format_configuration = $this->getFormat($format);

        return array_merge(array(
          'title'    => $media->getName(),
          'src'      => $this->generatePublicUrl($media, $format),
          'width'    => $format_configuration['width'],
        ), $options);
    }
 /**
  * @param MediaInterface $media
  *
  * @return mixed
  */
 protected function getRootCategory(MediaInterface $media)
 {
     if (!$this->rootCategories) {
         $this->rootCategories = $this->container->get('sonata.classification.manager.category')->getRootCategories(false);
     }
     if (!array_key_exists($media->getContext(), $this->rootCategories)) {
         throw new \RuntimeException(sprintf('There is no main category related to context: %s', $media->getContext()));
     }
     return $this->rootCategories[$media->getContext()];
 }
Exemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function getHelperProperties(MediaInterface $media, $format, $options = array())
 {
     /*$getId3 = new GetId3();
       $audio = $getId3
           ->setOptionMD5Data(true)
           ->setOptionMD5DataSource(true)
           ->setEncoding('UTF-8')
           ->analyze($this->root_dir . '/../web' .$this->generatePublicUrl($media, $format))
       ;*/
     return array_merge(array('name' => $media->getName(), 'src' => $this->generatePublicUrl($media, $format), 'filesize' => $media->getMetadataValue('filesize'), 'fileformat' => $media->getMetadataValue('fileformat'), 'encoding' => $media->getMetadataValue('encoding'), 'mime_type' => $media->getMetadataValue('mime_type'), 'playtime_seconds' => $media->getMetadataValue('playtime_seconds'), 'playtime_string' => $media->getMetadataValue('playtime_string'), 'bitrate' => $media->getMetadataValue('bitrate'), 'audio_dataformat' => $media->getMetadataValue('audio_dataformat'), 'audio_codec' => $media->getMetadataValue('audio_codec'), 'audio_sample_rate' => $media->getMetadataValue('audio_sample_rate'), 'audio_channels' => $media->getMetadataValue('audio_channels'), 'audio_bits_per_sample' => $media->getMetadataValue('audio_bits_per_sample'), 'audio_lossless' => $media->getMetadataValue('audio_lossless'), 'audio_channelmode' => $media->getMetadataValue('audio_channelmode'), 'video_dataformat' => $media->getMetadataValue('video_dataformat'), 'video_resolution_x' => $media->getMetadataValue('video_resolution_x'), 'video_resolution_y' => $media->getMetadataValue('video_resolution_y'), 'video_fourcc' => $media->getMetadataValue('video_fourcc'), 'video_frame_rate' => $media->getMetadataValue('video_frame_rate'), 'video_codec' => $media->getMetadataValue('video_codec')), $options);
 }
 public function getFormatName(MediaInterface $media, $format)
 {
     if (in_array($format, array('admin', 'reference', 'orangegate'))) {
         return $format;
     }
     $baseName = $media->getContext() . '_';
     if (substr($format, 0, strlen($baseName)) == $baseName) {
         return $format;
     }
     return $baseName . str_replace('-', '_', $format);
 }
 protected function createQrCode(MediaInterface $media)
 {
     $path = tempnam(sys_get_temp_dir(), 'sonata_media_qrcode_reference') . '.' . $this->config['extension'];
     $qrCode = new QrCode();
     $qrCode->setText($media->getBinaryContent())->setSize($this->config['size'])->setPadding($this->config['padding'])->setErrorCorrection($this->config['error_correction'])->setForegroundColor($this->config['foreground'])->setBackgroundColor($this->config['background'])->setLabel($this->config['label'])->setLabelFontSize($this->config['label_size'])->setImageType($this->config['extension']);
     if ($this->config['logo'] && is_file($this->config['logo'])) {
         $qrCode->setLogo($this->config['logo']);
     }
     $qrCode->save($path);
     return $path;
 }
Exemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function save(MediaInterface $media, $context = null, $providerName = null)
 {
     if ($context) {
         $media->setContext($context);
     }
     if ($providerName) {
         $media->setProviderName($providerName);
     }
     // just in case the pool alter the media
     $this->em->persist($media);
     $this->em->flush();
 }
Exemplo n.º 14
0
 /**
  * {@inheritdoc}
  */
 protected function fixBinaryContent(MediaInterface $media)
 {
     if (!$media->getBinaryContent()) {
         return;
     }
     if (strlen($media->getBinaryContent()) === 11) {
         return;
     }
     if (preg_match("/^(?:http(?:s)?:\\/\\/)?(?:www\\.)?(?:youtu\\.be\\/|youtube\\.com\\/(?:(?:watch)?\\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\\/))([^\\#\\?&\"'>]+)/", $media->getBinaryContent(), $matches)) {
         $media->setBinaryContent($matches[1]);
     }
 }
Exemplo n.º 15
0
 /**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  *
  * @return string
  */
 public function generatePath(MediaInterface $media)
 {
     $segments = preg_split('#/#', $media->getId(), null, PREG_SPLIT_NO_EMPTY);
     if (count($segments) > 1) {
         // remove last part from id
         array_pop($segments);
         $path = join($segments, '/');
     } else {
         $path = '';
     }
     return $path ? sprintf('%s/%s', $media->getContext(), $path) : $media->getContext();
 }
Exemplo n.º 16
0
 /**
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  * @param \Gaufrette\File $in
  * @param \Gaufrette\File $out
  * @param string $format
  * @param array $settings
  * @return void
  */
 public function resize(MediaInterface $media, File $in, File $out, $format, $settings)
 {
     if (!isset($settings['width'])) {
         throw new \RuntimeException(sprintf('Width parameter is missing in context "%s" for provider "%s"', $media->getContext(), $media->getProviderClass()));
     }
     $image = $this->getAdapter()->load($in->getContent());
     if ($settings['height'] == null) {
         $size = $image->getSize();
         $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
     }
     $content = $image->thumbnail(new Box($settings['width'], $settings['height']), $this->getMode())->get($format);
     $out->setContent($content);
 }
 /**
  * @throws InvalidArgumentException
  *
  * @param MediaInterface $media
  * @param array          $settings
  *
  * @return Box
  */
 protected function computeBox(MediaInterface $media, array $settings)
 {
     if ($this->mode !== ImageInterface::THUMBNAIL_INSET && $this->mode !== ImageInterface::THUMBNAIL_OUTBOUND) {
         throw new InvalidArgumentException('Invalid mode specified');
     }
     $size = $media->getBox();
     $ratios = array($settings['width'] / $size->getWidth(), $settings['height'] / $size->getHeight());
     if ($this->mode === ImageInterface::THUMBNAIL_INSET) {
         $ratio = min($ratios);
     } else {
         $ratio = max($ratios);
     }
     return $size->scale($ratio);
 }
Exemplo n.º 18
0
 /**
  * {@inheritdoc}
  */
 public function getHelperProperties(MediaInterface $media, $format, $options = array())
 {
     if ($format == 'reference') {
         $box = $media->getBox();
     } else {
         $resizerFormat = $this->getFormat($format);
         if ($resizerFormat === false) {
             throw new \RuntimeException(sprintf('The image format "%s" is not defined.
                     Is the format registered in your ``sonata_media`` configuration?', $format));
         }
         $box = $this->resizer->getBox($media, $resizerFormat);
     }
     return array_merge(array('alt' => $media->getName(), 'title' => $media->getName(), 'src' => $this->generatePublicUrl($media, $format), 'width' => $box->getWidth(), 'height' => $box->getHeight()), $options);
 }
 /**
  * @param MediaInterface $metadata
  * @param string         $filename
  *
  * @return array
  */
 protected function getAmazonBuilder(MediaInterface $media, $filename)
 {
     $adapter = $this->container->get($media->getProviderName())->getFilesystem()->getAdapter();
     //handle special Replicate adapter
     if ($adapter instanceof Replicate) {
         $adapterClassNames = $adapter->getAdapterClassNames();
     } else {
         $adapterClassNames = array(get_class($adapter));
     }
     //for amazon s3
     if (!in_array('Gaufrette\\Adapter\\AmazonS3', $adapterClassNames) || !$this->container->has('sonata.media.metadata.amazon')) {
         return false;
     }
     return $this->container->get('sonata.media.metadata.amazon')->get($media, $filename);
 }
 /**
  * {@inheritdoc}
  */
 public function generate(MediaProviderInterface $provider, MediaInterface $media)
 {
     $backend = $this->backend;
     $id = $this->id;
     $publish = function () use($backend, $media, $id) {
         $backend->createAndPublish('sonata.media.create_thumbnail', array('thumbnailId' => $id, 'mediaId' => $media->getId(), 'providerReference' => $media->getProviderReference()));
     };
     // BC compatibility for missing EventDispatcher
     if (null === $this->dispatcher) {
         trigger_error('Since version 2.3.3, passing an empty parameter in argument 4 for __construct() in ' . __CLASS__ . ' is deprecated and the workaround for it will be removed in 3.0.', E_USER_DEPRECATED);
         $publish();
     } else {
         $this->dispatcher->addListener('kernel.finish_request', $publish);
     }
 }
 /**
  * @throws \RuntimeException
  * @param \Sonata\MediaBundle\Model\MediaInterface $media
  * @return mixed|null|string
  */
 public function getMetadata(MediaInterface $media)
 {
     if (!$media->getBinaryContent()) {
         return null;
     }
     $url = sprintf('http://www.dailymotion.com/services/oembed?url=http://www.dailymotion.com/video/%s&format=json', $media->getBinaryContent());
     $metadata = @file_get_contents($url);
     if (!$metadata) {
         throw new \RuntimeException('Unable to retrieve dailymotion video information for :' . $url);
     }
     $metadata = json_decode($metadata, true);
     if (!$metadata) {
         throw new \RuntimeException('Unable to decode dailymotion video information for :' . $url);
     }
     return $metadata;
 }
 public function getFormatName(MediaInterface $media, $format)
 {
     if ($format == 'admin') {
         return 'admin';
     }
     if ($format == 'reference') {
         return 'reference';
     }
     if ($format == 'orangegate') {
         return 'orangegate';
     }
     $baseName = $media->getContext() . '_';
     if (substr($format, 0, strlen($baseName)) == $baseName) {
         return $format;
     }
     return $baseName . $format;
 }
Exemplo n.º 23
0
 /**
  * @param null|\Sonata\MediaBundle\Model\MediaInterface $media
  *
  * @return array
  */
 protected function getFormatChoices(MediaInterface $media = null)
 {
     $formatChoices = array();
     if (!$media instanceof MediaInterface) {
         return $formatChoices;
     }
     $formats = $this->getMediaPool()->getFormatNamesByContext($media->getContext());
     foreach ($formats as $code => $format) {
         $formatChoices[$code] = $code;
     }
     return $formatChoices;
 }
Exemplo n.º 24
0
 /**
  * {@inheritdoc}
  */
 public function getBox(MediaInterface $media, array $settings)
 {
     $size = $media->getBox();
     if (null != $settings['height']) {
         if ($size->getHeight() > $size->getWidth()) {
             $higher = $size->getHeight();
             $lower = $size->getWidth();
         } else {
             $higher = $size->getWidth();
             $lower = $size->getHeight();
         }
         if ($higher - $lower > 0) {
             return new Box($lower, $lower);
         }
     }
     $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
     if ($settings['height'] < $size->getHeight() && $settings['width'] < $size->getWidth()) {
         return new Box($settings['width'], $settings['height']);
     }
     return $size;
 }
 /**
  * {@inheritdoc}
  */
 public function updateMetadata(MediaInterface $media, $force = true)
 {
     try {
         if (!$media->getBinaryContent() instanceof \SplFileInfo) {
             // this is now optimized at all!!!
             $path = tempnam(sys_get_temp_dir(), 'sonata_update_metadata');
             $fileObject = new \SplFileObject($path, 'w');
             $fileObject->fwrite($this->getReferenceFile($media)->getContent());
         } else {
             $fileObject = $media->getBinaryContent();
         }
         $image = $this->imagineAdapter->open($fileObject->getPathname());
         $size = $image->getSize();
         $media->setSize($fileObject->getSize());
         $media->setWidth($size->getWidth());
         $media->setHeight($size->getHeight());
     } catch (\LogicException $e) {
         $media->setProviderStatus(MediaInterface::STATUS_ERROR);
         $media->setSize(0);
         $media->setWidth(0);
         $media->setHeight(0);
     }
 }
Exemplo n.º 26
0
 /**
  * @param MediaInterface $media
  *
  * @return MediaProviderInterface
  */
 private function getProvider(MediaInterface $media)
 {
     return $this->pool->getProvider($media->getProviderName());
 }
Exemplo n.º 27
0
 /**
  * @param MediaInterface $media
  *
  * @return MediaProviderInterface
  */
 public function getProvider(MediaInterface $media)
 {
     return $this->get('sonata.media.pool')->getProvider($media->getProviderName());
 }
Exemplo n.º 28
0
 /**
  * @param \Sonata\CoreBundle\Validator\ErrorElement $errorElement
  * @param \Sonata\MediaBundle\Model\MediaInterface  $media
  */
 public function validate(ErrorElement $errorElement, MediaInterface $media)
 {
     if (!$media->getProviderName()) {
         return;
     }
     $provider = $this->getProvider($media->getProviderName());
     $provider->validate($errorElement, $media);
 }
Exemplo n.º 29
0
 /**
  * {@inheritdoc}
  */
 public function getHelperProperties(MediaInterface $media, $format, $options = array())
 {
     return array_merge(array('title' => $media->getName(), 'thumbnail' => $this->getReferenceImage($media), 'file' => $this->getReferenceImage($media), 'media' => $media), $options);
 }
Exemplo n.º 30
0
 /**
  * @param MediaInterface         $media
  * @param MediaProviderInterface $provider
  * @param string                 $context
  * @param string                 $format
  *
  * @return bool
  */
 protected function processMedia(MediaInterface $media, MediaProviderInterface $provider, $context, $format)
 {
     $this->log('Deleting thumbs for ' . $media->getName() . ' - ' . $media->getId());
     try {
         if ($format === $context . '_all') {
             $format = null;
         }
         $provider->removeThumbnails($media, $format);
     } catch (\Exception $e) {
         $this->log(sprintf('<error>Unable to remove thumbnails, media: %s - %s </error>', $media->getId(), $e->getMessage()));
         return false;
     }
     return true;
 }