vendor/pimcore/pimcore/models/Asset/Image/Thumbnail/Config/Dao.php line 34

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\Image\Thumbnail\Config;
  15. use Pimcore\Config\LocationAwareConfigRepository;
  16. use Pimcore\Messenger\CleanupThumbnailsMessage;
  17. use Pimcore\Model;
  18. /**
  19.  * @internal
  20.  *
  21.  * @property \Pimcore\Model\Asset\Image\Thumbnail\Config $model
  22.  */
  23. class Dao extends Model\Dao\PimcoreLocationAwareConfigDao
  24. {
  25.     private const CONFIG_KEY 'image_thumbnails';
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public function configure()
  30.     {
  31.         $config \Pimcore::getContainer()->getParameter('pimcore.config');
  32.         $storageConfig LocationAwareConfigRepository::getStorageConfigurationCompatibilityLayer(
  33.             $config,
  34.             self::CONFIG_KEY,
  35.             'PIMCORE_CONFIG_STORAGE_DIR_IMAGE_THUMBNAILS',
  36.             'PIMCORE_WRITE_TARGET_IMAGE_THUMBNAILS'
  37.         );
  38.         parent::configure([
  39.             'containerConfig' => $config['assets']['image']['thumbnails']['definitions'],
  40.             'settingsStoreScope' => 'pimcore_image_thumbnails',
  41.             'storageDirectory' => $storageConfig,
  42.             'legacyConfigFile' => 'image-thumbnails.php',
  43.         ]);
  44.     }
  45.     /**
  46.      * @param string|null $id
  47.      *
  48.      * @throws \Exception
  49.      */
  50.     public function getByName($id null)
  51.     {
  52.         if ($id != null) {
  53.             $this->model->setName($id);
  54.         }
  55.         $data $this->getDataByName($this->model->getName());
  56.         if ($data && $id != null) {
  57.             $data['id'] = $id;
  58.         }
  59.         if ($data) {
  60.             $this->assignVariablesToModel($data);
  61.             $this->model->setName($data['id']);
  62.         } else {
  63.             throw new Model\Exception\NotFoundException(sprintf(
  64.                 'Thumbnail with ID "%s" does not exist.',
  65.                 $this->model->getName()
  66.             ));
  67.         }
  68.     }
  69.     /**
  70.      * @param string $name
  71.      *
  72.      * @return bool
  73.      */
  74.     public function exists(string $name): bool
  75.     {
  76.         return (bool) $this->getDataByName($this->model->getName());
  77.     }
  78.     /**
  79.      * @param bool $forceClearTempFiles force removing generated thumbnail files of saved thumbnail config
  80.      *
  81.      * @throws \Exception
  82.      */
  83.     public function save($forceClearTempFiles false)
  84.     {
  85.         $ts time();
  86.         if (!$this->model->getCreationDate()) {
  87.             $this->model->setCreationDate($ts);
  88.         }
  89.         $this->model->setModificationDate($ts);
  90.         $dataRaw $this->model->getObjectVars();
  91.         $data = [];
  92.         $allowedProperties = ['name''description''group''items''medias''format',
  93.             'quality''highResolution''creationDate''modificationDate''preserveColor''preserveMetaData',
  94.             'rasterizeSVG''downloadable''preserveAnimation', ];
  95.         foreach ($dataRaw as $key => $value) {
  96.             if (in_array($key$allowedProperties)) {
  97.                 $data[$key] = $value;
  98.             }
  99.         }
  100.         $this->saveData($this->model->getName(), $data);
  101.         if ($forceClearTempFiles) {
  102.             $this->model->clearTempFiles();
  103.         } elseif ($this->dataSource) {
  104.             // thumbnail already existed
  105.             $this->autoClearTempFiles();
  106.         }
  107.         $this->clearDatabaseCache();
  108.     }
  109.     /**
  110.      * {@inheritdoc}
  111.      */
  112.     protected function prepareDataStructureForYaml(string $id$data)
  113.     {
  114.         return [
  115.             'pimcore' => [
  116.                 'assets' => [
  117.                     'image' => [
  118.                         'thumbnails' => [
  119.                             'definitions' => [
  120.                                 $id => $data,
  121.                             ],
  122.                         ],
  123.                     ],
  124.                 ],
  125.             ],
  126.         ];
  127.     }
  128.     /**
  129.      * Deletes object from database
  130.      *
  131.      * @param bool $forceClearTempFiles force removing generated thumbnail files of saved thumbnail config
  132.      */
  133.     public function delete($forceClearTempFiles false)
  134.     {
  135.         $this->deleteData($this->model->getName());
  136.         if ($forceClearTempFiles) {
  137.             $this->model->clearTempFiles();
  138.         } else {
  139.             $this->autoClearTempFiles();
  140.         }
  141.         $this->clearDatabaseCache();
  142.     }
  143.     private function clearDatabaseCache(): void
  144.     {
  145.         \Pimcore\Db::get()->delete('assets_image_thumbnail_cache', [
  146.             'name' => $this->model->getName(),
  147.         ]);
  148.         Model\Asset\Dao::$thumbnailStatusCache = [];
  149.     }
  150.     protected function autoClearTempFiles()
  151.     {
  152.         $enabled \Pimcore::getContainer()->getParameter('pimcore.config')['assets']['image']['thumbnails']['auto_clear_temp_files'];
  153.         if ($enabled) {
  154.             \Pimcore::getContainer()->get('messenger.bus.pimcore-core')->dispatch(
  155.                 new CleanupThumbnailsMessage('image'$this->model->getName())
  156.             );
  157.         }
  158.     }
  159. }