vendor/pimcore/pimcore/models/Asset/Video/Thumbnail/Config.php line 120

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\Asset\Video\Thumbnail;
  15. use Pimcore\Cache\RuntimeCache;
  16. use Pimcore\Model;
  17. /**
  18.  * @method bool isWriteable()
  19.  * @method string getWriteTarget()
  20.  * @method void delete()
  21.  * @method void save()
  22.  */
  23. final class Config extends Model\AbstractModel
  24. {
  25.     use Model\Asset\Thumbnail\ClearTempFilesTrait;
  26.     /**
  27.      * format of array:
  28.      * array(
  29.      array(
  30.      "method" => "myName",
  31.      "arguments" =>
  32.      array(
  33.      "width" => 345,
  34.      "height" => 200
  35.      )
  36.      )
  37.      * )
  38.      *
  39.      * @internal
  40.      *
  41.      * @var array
  42.      */
  43.     protected $items = [];
  44.     /**
  45.      * @internal
  46.      *
  47.      * @var array
  48.      */
  49.     public $medias = [];
  50.     /**
  51.      * @internal
  52.      *
  53.      * @var string
  54.      */
  55.     protected $name '';
  56.     /**
  57.      * @internal
  58.      *
  59.      * @var string
  60.      */
  61.     protected $description '';
  62.     /**
  63.      * @internal
  64.      *
  65.      * @var string
  66.      */
  67.     protected $group '';
  68.     /**
  69.      * @internal
  70.      *
  71.      * @var int|null
  72.      */
  73.     protected $videoBitrate;
  74.     /**
  75.      * @internal
  76.      *
  77.      * @var int|null
  78.      */
  79.     protected $audioBitrate;
  80.     /**
  81.      * @internal
  82.      *
  83.      * @var int|null
  84.      */
  85.     protected $modificationDate;
  86.     /**
  87.      * @internal
  88.      *
  89.      * @var int|null
  90.      */
  91.     protected $creationDate;
  92.     /**
  93.      * @internal
  94.      *
  95.      * @var string|null
  96.      */
  97.     public $filenameSuffix;
  98.     /**
  99.      * @param string $name
  100.      *
  101.      * @return null|Config
  102.      *
  103.      * @throws \Exception
  104.      */
  105.     public static function getByName($name)
  106.     {
  107.         $cacheKey 'videothumb_' crc32($name);
  108.         try {
  109.             $thumbnail RuntimeCache::get($cacheKey);
  110.             if (!$thumbnail) {
  111.                 throw new \Exception('Thumbnail in registry is null');
  112.             }
  113.         } catch (\Exception $e) {
  114.             try {
  115.                 $thumbnail = new self();
  116.                 /** @var Model\Asset\Video\Thumbnail\Config\Dao $dao */
  117.                 $dao $thumbnail->getDao();
  118.                 $dao->getByName($name);
  119.                 RuntimeCache::set($cacheKey$thumbnail);
  120.             } catch (Model\Exception\NotFoundException $e) {
  121.                 return null;
  122.             }
  123.         }
  124.         return $thumbnail;
  125.     }
  126.     /**
  127.      * @internal
  128.      *
  129.      * @return Config
  130.      */
  131.     public static function getPreviewConfig()
  132.     {
  133.         $config = new self();
  134.         $config->setName('pimcore-system-treepreview');
  135.         $config->setAudioBitrate(128);
  136.         $config->setVideoBitrate(700);
  137.         $config->setItems([
  138.             [
  139.                 'method' => 'scaleByWidth',
  140.                 'arguments' =>
  141.                 [
  142.                     'width' => 500,
  143.                 ],
  144.             ],
  145.         ]);
  146.         return $config;
  147.     }
  148.     /**
  149.      * @param string $name
  150.      */
  151.     private function createMediaIfNotExists($name)
  152.     {
  153.         if (!array_key_exists($name$this->medias)) {
  154.             $this->medias[$name] = [];
  155.         }
  156.     }
  157.     /**
  158.      * @internal
  159.      *
  160.      * @param string $name
  161.      * @param array $parameters
  162.      * @param string $media
  163.      *
  164.      * @return bool
  165.      */
  166.     public function addItem($name$parameters$media null)
  167.     {
  168.         $item = [
  169.             'method' => $name,
  170.             'arguments' => $parameters,
  171.         ];
  172.         // default is added to $this->items for compatibility reasons
  173.         if (!$media || $media == 'default') {
  174.             $this->items[] = $item;
  175.         } else {
  176.             $this->createMediaIfNotExists($media);
  177.             $this->medias[$media][] = $item;
  178.         }
  179.         return true;
  180.     }
  181.     /**
  182.      * @internal
  183.      *
  184.      * @param int $position
  185.      * @param string $name
  186.      * @param array $parameters
  187.      *
  188.      * @return bool
  189.      */
  190.     public function addItemAt($position$name$parameters$media null)
  191.     {
  192.         if (!$media || $media == 'default') {
  193.             $itemContainer = &$this->items;
  194.         } else {
  195.             $this->createMediaIfNotExists($media);
  196.             $itemContainer = &$this->medias[$media];
  197.         }
  198.         array_splice($itemContainer$position0, [[
  199.             'method' => $name,
  200.             'arguments' => $parameters,
  201.         ]]);
  202.         return true;
  203.     }
  204.     /**
  205.      * @param string $name
  206.      *
  207.      * @return bool
  208.      */
  209.     public function selectMedia($name)
  210.     {
  211.         if (preg_match('/^[0-9a-f]{8}$/'$name)) {
  212.             $hash $name;
  213.         } else {
  214.             $hash hash('crc32b'$name);
  215.         }
  216.         foreach ($this->medias as $key => $value) {
  217.             $currentHash hash('crc32b'$key);
  218.             if ($key === $name || $currentHash === $hash) {
  219.                 $this->setItems($value);
  220.                 $this->setFilenameSuffix('media--' $currentHash '--query');
  221.                 return true;
  222.             }
  223.         }
  224.         return false;
  225.     }
  226.     /**
  227.      * @internal
  228.      */
  229.     public function resetItems()
  230.     {
  231.         $this->items = [];
  232.         $this->medias = [];
  233.     }
  234.     /**
  235.      * @param string $description
  236.      *
  237.      * @return $this
  238.      */
  239.     public function setDescription($description)
  240.     {
  241.         $this->description $description;
  242.         return $this;
  243.     }
  244.     /**
  245.      * @return string
  246.      */
  247.     public function getDescription()
  248.     {
  249.         return $this->description;
  250.     }
  251.     /**
  252.      * @param array $items
  253.      *
  254.      * @return $this
  255.      */
  256.     public function setItems($items)
  257.     {
  258.         $this->items $items;
  259.         return $this;
  260.     }
  261.     /**
  262.      * @return array
  263.      */
  264.     public function getItems()
  265.     {
  266.         return $this->items;
  267.     }
  268.     /**
  269.      * @param array $medias
  270.      */
  271.     public function setMedias($medias)
  272.     {
  273.         $this->medias $medias;
  274.     }
  275.     /**
  276.      * @return array
  277.      */
  278.     public function getMedias()
  279.     {
  280.         return $this->medias;
  281.     }
  282.     /**
  283.      * @return bool
  284.      */
  285.     public function hasMedias()
  286.     {
  287.         return !empty($this->medias);
  288.     }
  289.     /**
  290.      * @param string $filenameSuffix
  291.      */
  292.     public function setFilenameSuffix($filenameSuffix)
  293.     {
  294.         $this->filenameSuffix $filenameSuffix;
  295.     }
  296.     /**
  297.      * @return string|null
  298.      */
  299.     public function getFilenameSuffix()
  300.     {
  301.         return $this->filenameSuffix;
  302.     }
  303.     /**
  304.      * @param string $name
  305.      *
  306.      * @return $this
  307.      */
  308.     public function setName($name)
  309.     {
  310.         $this->name $name;
  311.         return $this;
  312.     }
  313.     /**
  314.      * @return string
  315.      */
  316.     public function getName()
  317.     {
  318.         return $this->name;
  319.     }
  320.     /**
  321.      * @param int $audioBitrate
  322.      *
  323.      * @return $this
  324.      */
  325.     public function setAudioBitrate($audioBitrate)
  326.     {
  327.         $this->audioBitrate = (int) $audioBitrate;
  328.         return $this;
  329.     }
  330.     /**
  331.      * @return int
  332.      */
  333.     public function getAudioBitrate()
  334.     {
  335.         return $this->audioBitrate;
  336.     }
  337.     /**
  338.      * @param int $videoBitrate
  339.      *
  340.      * @return $this
  341.      */
  342.     public function setVideoBitrate($videoBitrate)
  343.     {
  344.         $this->videoBitrate = (int) $videoBitrate;
  345.         return $this;
  346.     }
  347.     /**
  348.      * @return int
  349.      */
  350.     public function getVideoBitrate()
  351.     {
  352.         return $this->videoBitrate;
  353.     }
  354.     /**
  355.      * @internal
  356.      *
  357.      * @return array
  358.      */
  359.     public function getEstimatedDimensions()
  360.     {
  361.         $dimensions = [];
  362.         $transformations $this->getItems();
  363.         if (is_array($transformations) && count($transformations) > 0) {
  364.             foreach ($transformations as $transformation) {
  365.                 if (!empty($transformation)) {
  366.                     if (is_array($transformation['arguments'])) {
  367.                         foreach ($transformation['arguments'] as $key => $value) {
  368.                             if ($key == 'width' || $key == 'height') {
  369.                                 $dimensions[$key] = $value;
  370.                             }
  371.                         }
  372.                     }
  373.                 }
  374.             }
  375.         }
  376.         return $dimensions;
  377.     }
  378.     /**
  379.      * @return int|null
  380.      */
  381.     public function getModificationDate()
  382.     {
  383.         return $this->modificationDate;
  384.     }
  385.     /**
  386.      * @param int $modificationDate
  387.      */
  388.     public function setModificationDate($modificationDate)
  389.     {
  390.         $this->modificationDate $modificationDate;
  391.     }
  392.     /**
  393.      * @return int|null
  394.      */
  395.     public function getCreationDate()
  396.     {
  397.         return $this->creationDate;
  398.     }
  399.     /**
  400.      * @param int $creationDate
  401.      */
  402.     public function setCreationDate($creationDate)
  403.     {
  404.         $this->creationDate $creationDate;
  405.     }
  406.     /**
  407.      * @return string
  408.      */
  409.     public function getGroup(): string
  410.     {
  411.         return $this->group;
  412.     }
  413.     /**
  414.      * @param string $group
  415.      */
  416.     public function setGroup(string $group): void
  417.     {
  418.         $this->group $group;
  419.     }
  420.     public function __clone()
  421.     {
  422.         if ($this->dao) {
  423.             $this->dao = clone $this->dao;
  424.             $this->dao->setModel($this);
  425.         }
  426.     }
  427. }