vendor/pimcore/pimcore/bundles/AdminBundle/Controller/Admin/Asset/AssetController.php line 63

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\Bundle\AdminBundle\Controller\Admin\Asset;
  15. use Pimcore\Bundle\AdminBundle\Controller\Admin\ElementControllerBase;
  16. use Pimcore\Bundle\AdminBundle\Controller\Traits\AdminStyleTrait;
  17. use Pimcore\Bundle\AdminBundle\Controller\Traits\ApplySchedulerDataTrait;
  18. use Pimcore\Bundle\AdminBundle\Helper\GridHelperService;
  19. use Pimcore\Bundle\AdminBundle\Security\CsrfProtectionHandler;
  20. use Pimcore\Config;
  21. use Pimcore\Controller\KernelControllerEventInterface;
  22. use Pimcore\Controller\Traits\ElementEditLockHelperTrait;
  23. use Pimcore\Db\Helper;
  24. use Pimcore\Event\Admin\ElementAdminStyleEvent;
  25. use Pimcore\Event\AdminEvents;
  26. use Pimcore\Event\AssetEvents;
  27. use Pimcore\Event\Model\Asset\ResolveUploadTargetEvent;
  28. use Pimcore\File;
  29. use Pimcore\Loader\ImplementationLoader\Exception\UnsupportedException;
  30. use Pimcore\Logger;
  31. use Pimcore\Messenger\AssetPreviewImageMessage;
  32. use Pimcore\Model;
  33. use Pimcore\Model\Asset;
  34. use Pimcore\Model\DataObject\ClassDefinition\Data\ManyToManyRelation;
  35. use Pimcore\Model\DataObject\Concrete;
  36. use Pimcore\Model\Element;
  37. use Pimcore\Model\Element\ValidationException;
  38. use Pimcore\Model\Metadata;
  39. use Pimcore\Model\Schedule\Task;
  40. use Pimcore\Tool;
  41. use Symfony\Component\EventDispatcher\GenericEvent;
  42. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  43. use Symfony\Component\HttpFoundation\JsonResponse;
  44. use Symfony\Component\HttpFoundation\Request;
  45. use Symfony\Component\HttpFoundation\Response;
  46. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  47. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
  48. use Symfony\Component\HttpFoundation\StreamedResponse;
  49. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  50. use Symfony\Component\Mime\MimeTypes;
  51. use Symfony\Component\Process\Process;
  52. use Symfony\Component\Routing\Annotation\Route;
  53. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  54. /**
  55.  * @Route("/asset")
  56.  *
  57.  * @internal
  58.  */
  59. class AssetController extends ElementControllerBase implements KernelControllerEventInterface
  60. {
  61.     use AdminStyleTrait;
  62.     use ElementEditLockHelperTrait;
  63.     use ApplySchedulerDataTrait;
  64.     /**
  65.      * @var Asset\Service
  66.      */
  67.     protected $_assetService;
  68.     /**
  69.      * @Route("/tree-get-root", name="pimcore_admin_asset_treegetroot", methods={"GET"})
  70.      *
  71.      * @param Request $request
  72.      *
  73.      * @return JsonResponse
  74.      */
  75.     public function treeGetRootAction(Request $request)
  76.     {
  77.         return parent::treeGetRootAction($request);
  78.     }
  79.     /**
  80.      * @Route("/delete-info", name="pimcore_admin_asset_deleteinfo", methods={"GET"})
  81.      *
  82.      * @param Request $request
  83.      * @param EventDispatcherInterface $eventDispatcher
  84.      *
  85.      * @return JsonResponse
  86.      */
  87.     public function deleteInfoAction(Request $requestEventDispatcherInterface $eventDispatcher)
  88.     {
  89.         return parent::deleteInfoAction($request$eventDispatcher);
  90.     }
  91.     /**
  92.      * @Route("/get-data-by-id", name="pimcore_admin_asset_getdatabyid", methods={"GET"})
  93.      *
  94.      * @param Request $request
  95.      *
  96.      * @return JsonResponse
  97.      */
  98.     public function getDataByIdAction(Request $requestEventDispatcherInterface $eventDispatcher)
  99.     {
  100.         $assetId = (int)$request->get('id');
  101.         $type = (string)$request->get('type');
  102.         $asset Asset::getById($assetId);
  103.         if (!$asset instanceof Asset) {
  104.             return $this->adminJson(['success' => false'message' => "asset doesn't exist"]);
  105.         }
  106.         // check for lock on non-folder items only.
  107.         if ($type !== 'folder' && ($asset->isAllowed('publish') || $asset->isAllowed('delete'))) {
  108.             if (Element\Editlock::isLocked($assetId'asset')) {
  109.                 return $this->getEditLockResponse($assetId'asset');
  110.             }
  111.             Element\Editlock::lock($request->get('id'), 'asset');
  112.         }
  113.         $asset = clone $asset;
  114.         $asset->setParent(null);
  115.         $asset->setStream(null);
  116.         $data $asset->getObjectVars();
  117.         $data['locked'] = $asset->isLocked();
  118.         if ($asset instanceof Asset\Text) {
  119.             if ($asset->getFileSize() < 2000000) {
  120.                 // it doesn't make sense to show a preview for files bigger than 2MB
  121.                 $data['data'] = \ForceUTF8\Encoding::toUTF8($asset->getData());
  122.             } else {
  123.                 $data['data'] = false;
  124.             }
  125.         } elseif ($asset instanceof Asset\Document) {
  126.             $data['pdfPreviewAvailable'] = (bool)$this->getDocumentPreviewPdf($asset);
  127.         } elseif ($asset instanceof Asset\Video) {
  128.             $videoInfo = [];
  129.             if (\Pimcore\Video::isAvailable()) {
  130.                 $config Asset\Video\Thumbnail\Config::getPreviewConfig();
  131.                 $thumbnail $asset->getThumbnail($config, ['mp4']);
  132.                 if ($thumbnail) {
  133.                     if ($thumbnail['status'] == 'finished') {
  134.                         $videoInfo['previewUrl'] = $thumbnail['formats']['mp4'];
  135.                         $videoInfo['width'] = $asset->getWidth();
  136.                         $videoInfo['height'] = $asset->getHeight();
  137.                         $metaData $asset->getSphericalMetaData();
  138.                         if (isset($metaData['ProjectionType']) && strtolower($metaData['ProjectionType']) == 'equirectangular') {
  139.                             $videoInfo['isVrVideo'] = true;
  140.                         }
  141.                     }
  142.                 }
  143.             }
  144.             $data['videoInfo'] = $videoInfo;
  145.         } elseif ($asset instanceof Asset\Image) {
  146.             $imageInfo = [];
  147.             $previewUrl $this->generateUrl('pimcore_admin_asset_getimagethumbnail', [
  148.                 'id' => $asset->getId(),
  149.                 'treepreview' => true,
  150.                 '_dc' => time(),
  151.             ]);
  152.             if ($asset->isAnimated()) {
  153.                 $previewUrl $this->generateUrl('pimcore_admin_asset_getasset', [
  154.                     'id' => $asset->getId(),
  155.                     '_dc' => time(),
  156.                 ]);
  157.             }
  158.             $imageInfo['previewUrl'] = $previewUrl;
  159.             if ($asset->getWidth() && $asset->getHeight()) {
  160.                 $imageInfo['dimensions'] = [];
  161.                 $imageInfo['dimensions']['width'] = $asset->getWidth();
  162.                 $imageInfo['dimensions']['height'] = $asset->getHeight();
  163.             }
  164.             $imageInfo['exiftoolAvailable'] = (bool)\Pimcore\Tool\Console::getExecutable('exiftool');
  165.             if (!$asset->getEmbeddedMetaData(false)) {
  166.                 $asset->getEmbeddedMetaData(truefalse); // read Exif, IPTC and XPM like in the old days ...
  167.             }
  168.             $data['imageInfo'] = $imageInfo;
  169.         }
  170.         $predefinedMetaData Metadata\Predefined\Listing::getByTargetType('asset', [$asset->getType()]);
  171.         $predefinedMetaDataGroups = [];
  172.         /** @var Metadata\Predefined $item */
  173.         foreach ($predefinedMetaData as $item) {
  174.             if ($item->getGroup()) {
  175.                 $predefinedMetaDataGroups[$item->getGroup()] = true;
  176.             }
  177.         }
  178.         $data['predefinedMetaDataGroups'] = array_keys($predefinedMetaDataGroups);
  179.         $data['properties'] = Element\Service::minimizePropertiesForEditmode($asset->getProperties());
  180.         $data['metadata'] = Asset\Service::expandMetadataForEditmode($asset->getMetadata());
  181.         $data['versionDate'] = $asset->getModificationDate();
  182.         $data['filesizeFormatted'] = $asset->getFileSize(true);
  183.         $data['filesize'] = $asset->getFileSize();
  184.         $data['fileExtension'] = File::getFileExtension($asset->getFilename());
  185.         $data['idPath'] = Element\Service::getIdPath($asset);
  186.         $data['userPermissions'] = $asset->getUserPermissions($this->getAdminUser());
  187.         $frontendPath $asset->getFrontendFullPath();
  188.         $data['url'] = preg_match('/^http(s)?:\\/\\/.+/'$frontendPath) ?
  189.             $frontendPath :
  190.             $request->getSchemeAndHttpHost() . $frontendPath;
  191.         $data['scheduledTasks'] = array_map(
  192.             static function (Task $task) {
  193.                 return $task->getObjectVars();
  194.             },
  195.             $asset->getScheduledTasks()
  196.         );
  197.         $this->addAdminStyle($assetElementAdminStyleEvent::CONTEXT_EDITOR$data);
  198.         $data['php'] = [
  199.             'classes' => array_merge([get_class($asset)], array_values(class_parents($asset))),
  200.             'interfaces' => array_values(class_implements($asset)),
  201.         ];
  202.         $event = new GenericEvent($this, [
  203.             'data' => $data,
  204.             'asset' => $asset,
  205.         ]);
  206.         $eventDispatcher->dispatch($eventAdminEvents::ASSET_GET_PRE_SEND_DATA);
  207.         $data $event->getArgument('data');
  208.         if ($asset->isAllowed('view')) {
  209.             return $this->adminJson($data);
  210.         }
  211.         throw $this->createAccessDeniedHttpException();
  212.     }
  213.     /**
  214.      * @Route("/tree-get-childs-by-id", name="pimcore_admin_asset_treegetchildsbyid", methods={"GET"})
  215.      *
  216.      * @param Request $request
  217.      *
  218.      * @return JsonResponse
  219.      */
  220.     public function treeGetChildsByIdAction(Request $requestEventDispatcherInterface $eventDispatcher)
  221.     {
  222.         $allParams array_merge($request->request->all(), $request->query->all());
  223.         $assets = [];
  224.         $cv false;
  225.         $asset Asset::getById($allParams['node']);
  226.         $filter $request->get('filter');
  227.         $limit = (int)$allParams['limit'];
  228.         if (!is_null($filter)) {
  229.             if (substr($filter, -1) != '*') {
  230.                 $filter .= '*';
  231.             }
  232.             $filter str_replace('*''%'$filter);
  233.             $limit 100;
  234.             $offset 0;
  235.         } elseif (!$allParams['limit']) {
  236.             $limit 100000000;
  237.         }
  238.         $offset = isset($allParams['start']) ? (int)$allParams['start'] : 0;
  239.         $filteredTotalCount 0;
  240.         if ($asset->hasChildren()) {
  241.             if ($allParams['view']) {
  242.                 $cv \Pimcore\Model\Element\Service::getCustomViewById($allParams['view']);
  243.             }
  244.             // get assets
  245.             $childrenList = new Asset\Listing();
  246.             $childrenList->addConditionParam('parentId = ?', [$asset->getId()]);
  247.             $childrenList->filterAccessibleByUser($this->getAdminUser(), $asset);
  248.             if (!is_null($filter)) {
  249.                 $childrenList->addConditionParam('CAST(assets.filename AS CHAR CHARACTER SET utf8) COLLATE utf8_general_ci LIKE ?', [$filter]);
  250.             }
  251.             $childrenList->setLimit($limit);
  252.             $childrenList->setOffset($offset);
  253.             $childrenList->setOrderKey("FIELD(assets.type, 'folder') DESC, CAST(assets.filename AS CHAR CHARACTER SET utf8) COLLATE utf8_general_ci ASC"false);
  254.             \Pimcore\Model\Element\Service::addTreeFilterJoins($cv$childrenList);
  255.             $beforeListLoadEvent = new GenericEvent($this, [
  256.                 'list' => $childrenList,
  257.                 'context' => $allParams,
  258.             ]);
  259.             $eventDispatcher->dispatch($beforeListLoadEventAdminEvents::ASSET_LIST_BEFORE_LIST_LOAD);
  260.             /** @var Asset\Listing $childrenList */
  261.             $childrenList $beforeListLoadEvent->getArgument('list');
  262.             $children $childrenList->load();
  263.             $filteredTotalCount $childrenList->getTotalCount();
  264.             foreach ($children as $childAsset) {
  265.                 $assetTreeNode $this->getTreeNodeConfig($childAsset);
  266.                 if ($assetTreeNode['permissions']['list'] == 1) {
  267.                     $assets[] = $assetTreeNode;
  268.                 }
  269.             }
  270.         }
  271.         //Hook for modifying return value - e.g. for changing permissions based on asset data
  272.         $event = new GenericEvent($this, [
  273.             'assets' => $assets,
  274.         ]);
  275.         $eventDispatcher->dispatch($eventAdminEvents::ASSET_TREE_GET_CHILDREN_BY_ID_PRE_SEND_DATA);
  276.         $assets $event->getArgument('assets');
  277.         if ($allParams['limit']) {
  278.             return $this->adminJson([
  279.                 'offset' => $offset,
  280.                 'limit' => $limit,
  281.                 'total' => $asset->getChildAmount($this->getAdminUser()),
  282.                 'overflow' => !is_null($filter) && ($filteredTotalCount $limit),
  283.                 'nodes' => $assets,
  284.                 'filter' => $request->get('filter') ? $request->get('filter') : '',
  285.                 'inSearch' => (int)$request->get('inSearch'),
  286.             ]);
  287.         } else {
  288.             return $this->adminJson($assets);
  289.         }
  290.     }
  291.     /**
  292.      * @Route("/add-asset", name="pimcore_admin_asset_addasset", methods={"POST"})
  293.      *
  294.      * @param Request $request
  295.      * @param Config $config
  296.      *
  297.      * @return JsonResponse
  298.      */
  299.     public function addAssetAction(Request $requestConfig $config)
  300.     {
  301.         try {
  302.             $res $this->addAsset($request$config);
  303.             $response = [
  304.                 'success' => $res['success'],
  305.             ];
  306.             if ($res['success']) {
  307.                 $response['asset'] = [
  308.                     'id' => $res['asset']->getId(),
  309.                     'path' => $res['asset']->getFullPath(),
  310.                     'type' => $res['asset']->getType(),
  311.                 ];
  312.             }
  313.             return $this->adminJson($response);
  314.         } catch (\Exception $e) {
  315.             return $this->adminJson([
  316.                 'success' => false,
  317.                 'message' => $e->getMessage(),
  318.             ]);
  319.         }
  320.     }
  321.     /**
  322.      * @Route("/add-asset-compatibility", name="pimcore_admin_asset_addassetcompatibility", methods={"POST"})
  323.      *
  324.      * @param Request $request
  325.      * @param Config $config
  326.      *
  327.      * @return JsonResponse
  328.      */
  329.     public function addAssetCompatibilityAction(Request $requestConfig $config)
  330.     {
  331.         try {
  332.             // this is a special action for the compatibility mode upload (without flash)
  333.             $res $this->addAsset($request$config);
  334.             $response $this->adminJson([
  335.                 'success' => $res['success'],
  336.                 'msg' => $res['success'] ? 'Success' 'Error',
  337.                 'id' => $res['asset'] ? $res['asset']->getId() : null,
  338.                 'fullpath' => $res['asset'] ? $res['asset']->getRealFullPath() : null,
  339.                 'type' => $res['asset'] ? $res['asset']->getType() : null,
  340.             ]);
  341.             $response->headers->set('Content-Type''text/html');
  342.             return $response;
  343.         } catch (\Exception $e) {
  344.             return $this->adminJson([
  345.                 'success' => false,
  346.                 'message' => $e->getMessage(),
  347.             ]);
  348.         }
  349.     }
  350.     /**
  351.      * @Route("/exists", name="pimcore_admin_asset_exists", methods={"GET"})
  352.      *
  353.      * @param Request $request
  354.      *
  355.      * @return JsonResponse
  356.      *
  357.      * @throws \Exception
  358.      */
  359.     public function existsAction(Request $request)
  360.     {
  361.         $parentAsset \Pimcore\Model\Asset::getById((int)$request->get('parentId'));
  362.         return new JsonResponse([
  363.             'exists' => Asset\Service::pathExists($parentAsset->getRealFullPath().'/'.$request->get('filename')),
  364.         ]);
  365.     }
  366.     /**
  367.      * @param Request $request
  368.      * @param Config $config
  369.      *
  370.      * @return array
  371.      *
  372.      * @throws \Exception
  373.      */
  374.     protected function addAsset(Request $requestConfig $config)
  375.     {
  376.         $defaultUploadPath $config['assets']['default_upload_path'] ?? '/';
  377.         if (array_key_exists('Filedata'$_FILES)) {
  378.             $filename $_FILES['Filedata']['name'];
  379.             $sourcePath $_FILES['Filedata']['tmp_name'];
  380.         } elseif ($request->get('type') == 'base64') {
  381.             $filename $request->get('filename');
  382.             $sourcePath PIMCORE_SYSTEM_TEMP_DIRECTORY '/upload-base64' uniqid() . '.tmp';
  383.             $data preg_replace('@^data:[^,]+;base64,@'''$request->get('data'));
  384.             File::put($sourcePathbase64_decode($data));
  385.         } else {
  386.             throw new \Exception('The filename of the asset is empty');
  387.         }
  388.         $parentId $request->get('parentId');
  389.         $parentPath $request->get('parentPath');
  390.         if ($request->get('dir') && $request->get('parentId')) {
  391.             // this is for uploading folders with Drag&Drop
  392.             // param "dir" contains the relative path of the file
  393.             $parent Asset::getById((int) $request->get('parentId'));
  394.             $dir $request->get('dir');
  395.             if (strpos($dir'..') !== false) {
  396.                 throw new \Exception('not allowed');
  397.             }
  398.             $newPath $parent->getRealFullPath() . '/' trim($dir'/ ');
  399.             $maxRetries 5;
  400.             $newParent null;
  401.             for ($retries 0$retries $maxRetries$retries++) {
  402.                 try {
  403.                     $newParent Asset\Service::createFolderByPath($newPath);
  404.                     break;
  405.                 } catch (\Exception $e) {
  406.                     if ($retries < ($maxRetries 1)) {
  407.                         $waitTime rand(100000900000); // microseconds
  408.                         usleep($waitTime); // wait specified time until we restart the transaction
  409.                     } else {
  410.                         // if the transaction still fail after $maxRetries retries, we throw out the exception
  411.                         throw $e;
  412.                     }
  413.                 }
  414.             }
  415.             if ($newParent) {
  416.                 $parentId $newParent->getId();
  417.             }
  418.         } elseif (!$request->get('parentId') && $parentPath) {
  419.             $parent Asset::getByPath($parentPath);
  420.             if ($parent instanceof Asset\Folder) {
  421.                 $parentId $parent->getId();
  422.             }
  423.         }
  424.         $filename Element\Service::getValidKey($filename'asset');
  425.         if (empty($filename)) {
  426.             throw new \Exception('The filename of the asset is empty');
  427.         }
  428.         $context $request->get('context');
  429.         if ($context) {
  430.             $context json_decode($contexttrue);
  431.             $context $context ?: [];
  432.             $this->validateManyToManyRelationAssetType($context$filename$sourcePath);
  433.             $event = new ResolveUploadTargetEvent($parentId$filename$context);
  434.             \Pimcore::getEventDispatcher()->dispatch($eventAssetEvents::RESOLVE_UPLOAD_TARGET);
  435.             $filename Element\Service::getValidKey($event->getFilename(), 'asset');
  436.             $parentId $event->getParentId();
  437.         }
  438.         if (!$parentId) {
  439.             $parentId Asset\Service::createFolderByPath($defaultUploadPath)->getId();
  440.         }
  441.         $parentAsset Asset::getById((int)$parentId);
  442.         if (!$request->get('allowOverwrite')) {
  443.             // check for duplicate filename
  444.             $filename $this->getSafeFilename($parentAsset->getRealFullPath(), $filename);
  445.         }
  446.         if (!$parentAsset->isAllowed('create')) {
  447.             throw $this->createAccessDeniedHttpException(
  448.                 'Missing the permission to create new assets in the folder: ' $parentAsset->getRealFullPath()
  449.             );
  450.         }
  451.         if (is_file($sourcePath) && filesize($sourcePath) < 1) {
  452.             throw new \Exception('File is empty!');
  453.         } elseif (!is_file($sourcePath)) {
  454.             throw new \Exception('Something went wrong, please check upload_max_filesize and post_max_size in your php.ini as well as the write permissions of your temporary directories.');
  455.         }
  456.         if ($request->get('allowOverwrite') && Asset\Service::pathExists($parentAsset->getRealFullPath().'/'.$filename)) {
  457.             $asset Asset::getByPath($parentAsset->getRealFullPath().'/'.$filename);
  458.             $asset->setStream(fopen($sourcePath'rb'falseFile::getContext()));
  459.             $asset->save();
  460.         } else {
  461.             $asset Asset::create($parentId, [
  462.                 'filename' => $filename,
  463.                 'sourcePath' => $sourcePath,
  464.                 'userOwner' => $this->getAdminUser()->getId(),
  465.                 'userModification' => $this->getAdminUser()->getId(),
  466.             ]);
  467.         }
  468.         @unlink($sourcePath);
  469.         return [
  470.             'success' => true,
  471.             'asset' => $asset,
  472.         ];
  473.     }
  474.     /**
  475.      * @param string $targetPath
  476.      * @param string $filename
  477.      *
  478.      * @return string
  479.      */
  480.     protected function getSafeFilename($targetPath$filename)
  481.     {
  482.         $pathinfo pathinfo($filename);
  483.         $originalFilename $pathinfo['filename'];
  484.         $originalFileextension = empty($pathinfo['extension']) ? '' '.' $pathinfo['extension'];
  485.         $count 1;
  486.         if ($targetPath == '/') {
  487.             $targetPath '';
  488.         }
  489.         while (true) {
  490.             if (Asset\Service::pathExists($targetPath '/' $filename)) {
  491.                 $filename $originalFilename '_' $count $originalFileextension;
  492.                 $count++;
  493.             } else {
  494.                 return $filename;
  495.             }
  496.         }
  497.     }
  498.     /**
  499.      * @Route("/replace-asset", name="pimcore_admin_asset_replaceasset", methods={"POST", "PUT"})
  500.      *
  501.      * @param Request $request
  502.      *
  503.      * @return JsonResponse
  504.      *
  505.      * @throws \Exception
  506.      */
  507.     public function replaceAssetAction(Request $request)
  508.     {
  509.         $asset Asset::getById((int) $request->get('id'));
  510.         $newFilename Element\Service::getValidKey($_FILES['Filedata']['name'], 'asset');
  511.         $mimetype MimeTypes::getDefault()->guessMimeType($_FILES['Filedata']['tmp_name']);
  512.         $newType Asset::getTypeFromMimeMapping($mimetype$newFilename);
  513.         if ($newType != $asset->getType()) {
  514.             return $this->adminJson([
  515.                 'success' => false,
  516.                 'message' => sprintf($this->trans('asset_type_change_not_allowed', [], 'admin'), $asset->getType(), $newType),
  517.             ]);
  518.         }
  519.         $stream fopen($_FILES['Filedata']['tmp_name'], 'r+');
  520.         $asset->setStream($stream);
  521.         $asset->setCustomSetting('thumbnails'null);
  522.         $asset->setUserModification($this->getAdminUser()->getId());
  523.         $newFileExt File::getFileExtension($newFilename);
  524.         $currentFileExt File::getFileExtension($asset->getFilename());
  525.         if ($newFileExt != $currentFileExt) {
  526.             $newFilename preg_replace('/\.' $currentFileExt '$/i''.' $newFileExt$asset->getFilename());
  527.             $newFilename Element\Service::getSafeCopyName($newFilename$asset->getParent());
  528.             $asset->setFilename($newFilename);
  529.         }
  530.         if ($asset->isAllowed('publish')) {
  531.             $asset->save();
  532.             $response $this->adminJson([
  533.                 'id' => $asset->getId(),
  534.                 'path' => $asset->getRealFullPath(),
  535.                 'success' => true,
  536.             ]);
  537.             // set content-type to text/html, otherwise (when application/json is sent) chrome will complain in
  538.             // Ext.form.Action.Submit and mark the submission as failed
  539.             $response->headers->set('Content-Type''text/html');
  540.             return $response;
  541.         } else {
  542.             throw new \Exception('missing permission');
  543.         }
  544.     }
  545.     /**
  546.      * @Route("/add-folder", name="pimcore_admin_asset_addfolder", methods={"POST"})
  547.      *
  548.      * @param Request $request
  549.      *
  550.      * @return JsonResponse
  551.      */
  552.     public function addFolderAction(Request $request)
  553.     {
  554.         $success false;
  555.         $parentAsset Asset::getById((int)$request->get('parentId'));
  556.         $equalAsset Asset::getByPath($parentAsset->getRealFullPath() . '/' $request->get('name'));
  557.         if ($parentAsset->isAllowed('create')) {
  558.             if (!$equalAsset) {
  559.                 $asset Asset::create($request->get('parentId'), [
  560.                     'filename' => $request->get('name'),
  561.                     'type' => 'folder',
  562.                     'userOwner' => $this->getAdminUser()->getId(),
  563.                     'userModification' => $this->getAdminUser()->getId(),
  564.                 ]);
  565.                 $success true;
  566.             }
  567.         } else {
  568.             Logger::debug('prevented creating asset because of missing permissions');
  569.         }
  570.         return $this->adminJson(['success' => $success]);
  571.     }
  572.     /**
  573.      * @Route("/delete", name="pimcore_admin_asset_delete", methods={"DELETE"})
  574.      *
  575.      * @param Request $request
  576.      *
  577.      * @return JsonResponse
  578.      */
  579.     public function deleteAction(Request $request)
  580.     {
  581.         $type $request->get('type');
  582.         if ($type === 'childs') {
  583.             trigger_deprecation(
  584.                 'pimcore/pimcore',
  585.                 '10.4',
  586.                 'Type childs is deprecated. Use children instead'
  587.             );
  588.             $type 'children';
  589.         }
  590.         if ($type === 'children') {
  591.             $parentAsset Asset::getById((int) $request->get('id'));
  592.             $list = new Asset\Listing();
  593.             $list->setCondition('path LIKE ?', [Helper::escapeLike($parentAsset->getRealFullPath()) . '/%']);
  594.             $list->setLimit((int)$request->get('amount'));
  595.             $list->setOrderKey('LENGTH(path)'false);
  596.             $list->setOrder('DESC');
  597.             $deletedItems = [];
  598.             foreach ($list as $asset) {
  599.                 $deletedItems[$asset->getId()] = $asset->getRealFullPath();
  600.                 if ($asset->isAllowed('delete') && !$asset->isLocked()) {
  601.                     $asset->delete();
  602.                 }
  603.             }
  604.             return $this->adminJson(['success' => true'deleted' => $deletedItems]);
  605.         }
  606.         if ($request->get('id')) {
  607.             $asset Asset::getById((int) $request->get('id'));
  608.             if ($asset && $asset->isAllowed('delete')) {
  609.                 if ($asset->isLocked()) {
  610.                     return $this->adminJson([
  611.                         'success' => false,
  612.                         'message' => 'prevented deleting asset, because it is locked: ID: ' $asset->getId(),
  613.                     ]);
  614.                 }
  615.                 $asset->delete();
  616.                 return $this->adminJson(['success' => true]);
  617.             }
  618.         }
  619.         throw $this->createAccessDeniedHttpException();
  620.     }
  621.     /**
  622.      * @param Asset $element
  623.      *
  624.      * @return array
  625.      */
  626.     protected function getTreeNodeConfig($element)
  627.     {
  628.         $asset $element;
  629.         $permissions =  $asset->getUserPermissions($this->getAdminUser());
  630.         $tmpAsset = [
  631.             'id' => $asset->getId(),
  632.             'key' => $element->getKey(),
  633.             'text' => htmlspecialchars($asset->getFilename()),
  634.             'type' => $asset->getType(),
  635.             'path' => $asset->getRealFullPath(),
  636.             'basePath' => $asset->getRealPath(),
  637.             'locked' => $asset->isLocked(),
  638.             'lockOwner' => $asset->getLocked() ? true false,
  639.             'elementType' => 'asset',
  640.             'permissions' => [
  641.                 'remove' => $permissions['delete'],
  642.                 'settings' => $permissions['settings'],
  643.                 'rename' => $permissions['rename'],
  644.                 'publish' => $permissions['publish'],
  645.                 'view' => $permissions['view'],
  646.                 'list' => $permissions['list'],
  647.             ],
  648.         ];
  649.         $hasChildren $asset->getDao()->hasChildren($this->getAdminUser());
  650.         // set type specific settings
  651.         if ($asset instanceof Asset\Folder) {
  652.             $tmpAsset['leaf'] = false;
  653.             $tmpAsset['expanded'] = !$hasChildren;
  654.             $tmpAsset['loaded'] = !$hasChildren;
  655.             $tmpAsset['permissions']['create'] = $permissions['create'];
  656.             $tmpAsset['thumbnail'] = $this->getThumbnailUrl($asset, ['origin' => 'treeNode']);
  657.         } else {
  658.             $tmpAsset['leaf'] = true;
  659.             $tmpAsset['expandable'] = false;
  660.             $tmpAsset['expanded'] = false;
  661.         }
  662.         $this->addAdminStyle($assetElementAdminStyleEvent::CONTEXT_TREE$tmpAsset);
  663.         if ($asset instanceof Asset\Image) {
  664.             try {
  665.                 $tmpAsset['thumbnail'] = $this->getThumbnailUrl($asset, ['origin' => 'treeNode']);
  666.                 // we need the dimensions for the wysiwyg editors, so that they can resize the image immediately
  667.                 if ($asset->getCustomSetting('imageDimensionsCalculated')) {
  668.                     $tmpAsset['imageWidth'] = $asset->getCustomSetting('imageWidth');
  669.                     $tmpAsset['imageHeight'] = $asset->getCustomSetting('imageHeight');
  670.                 }
  671.             } catch (\Exception $e) {
  672.                 Logger::debug('Cannot get dimensions of image, seems to be broken.');
  673.             }
  674.         } elseif ($asset->getType() == 'video') {
  675.             try {
  676.                 if (\Pimcore\Video::isAvailable()) {
  677.                     $tmpAsset['thumbnail'] = $this->getThumbnailUrl($asset, ['origin' => 'treeNode']);
  678.                 }
  679.             } catch (\Exception $e) {
  680.                 Logger::debug('Cannot get dimensions of video, seems to be broken.');
  681.             }
  682.         } elseif ($asset->getType() == 'document') {
  683.             try {
  684.                 // add the PDF check here, otherwise the preview layer in admin is shown without content
  685.                 if (\Pimcore\Document::isAvailable() && \Pimcore\Document::isFileTypeSupported($asset->getFilename())) {
  686.                     $tmpAsset['thumbnail'] = $this->getThumbnailUrl($asset, ['origin' => 'treeNode']);
  687.                 }
  688.             } catch (\Exception $e) {
  689.                 Logger::debug('Cannot get dimensions of video, seems to be broken.');
  690.             }
  691.         }
  692.         $tmpAsset['cls'] = '';
  693.         if ($asset->isLocked()) {
  694.             $tmpAsset['cls'] .= 'pimcore_treenode_locked ';
  695.         }
  696.         if ($asset->getLocked()) {
  697.             $tmpAsset['cls'] .= 'pimcore_treenode_lockOwner ';
  698.         }
  699.         return $tmpAsset;
  700.     }
  701.     /**
  702.      * @param Asset $asset
  703.      * @param array $params
  704.      *
  705.      * @return null|string
  706.      */
  707.     protected function getThumbnailUrl(Asset $asset, array $params = [])
  708.     {
  709.         $defaults = [
  710.             'id' => $asset->getId(),
  711.             'treepreview' => true,
  712.             '_dc' => $asset->getModificationDate(),
  713.         ];
  714.         $params array_merge($defaults$params);
  715.         if ($asset instanceof Asset\Image) {
  716.             return $this->generateUrl('pimcore_admin_asset_getimagethumbnail'$params);
  717.         }
  718.         if ($asset instanceof Asset\Folder) {
  719.             return $this->generateUrl('pimcore_admin_asset_getfolderthumbnail'$params);
  720.         }
  721.         if ($asset instanceof Asset\Video && \Pimcore\Video::isAvailable()) {
  722.             return $this->generateUrl('pimcore_admin_asset_getvideothumbnail'$params);
  723.         }
  724.         if ($asset instanceof Asset\Document && \Pimcore\Document::isAvailable() && $asset->getPageCount()) {
  725.             return $this->generateUrl('pimcore_admin_asset_getdocumentthumbnail'$params);
  726.         }
  727.         if ($asset instanceof Asset\Audio) {
  728.             return '/bundles/pimcoreadmin/img/flat-color-icons/speaker.svg';
  729.         }
  730.         if ($asset instanceof Asset) {
  731.             return '/bundles/pimcoreadmin/img/filetype-not-supported.svg';
  732.         }
  733.     }
  734.     /**
  735.      * @Route("/update", name="pimcore_admin_asset_update", methods={"PUT"})
  736.      *
  737.      * @param Request $request
  738.      *
  739.      * @return JsonResponse
  740.      *
  741.      * @throws \Exception
  742.      */
  743.     public function updateAction(Request $request)
  744.     {
  745.         $success false;
  746.         $allowUpdate true;
  747.         $updateData array_merge($request->request->all(), $request->query->all());
  748.         $asset Asset::getById((int) $request->get('id'));
  749.         if ($asset->isAllowed('settings')) {
  750.             $asset->setUserModification($this->getAdminUser()->getId());
  751.             // if the position is changed the path must be changed || also from the children
  752.             if ($parentId $request->get('parentId')) {
  753.                 $parentAsset Asset::getById((int) $parentId);
  754.                 //check if parent is changed i.e. asset is moved
  755.                 if ($asset->getParentId() != $parentAsset->getId()) {
  756.                     if (!$parentAsset->isAllowed('create')) {
  757.                         throw new \Exception('Prevented moving asset - no create permission on new parent ');
  758.                     }
  759.                     $intendedPath $parentAsset->getRealPath();
  760.                     $pKey $parentAsset->getKey();
  761.                     if (!empty($pKey)) {
  762.                         $intendedPath .= $parentAsset->getKey() . '/';
  763.                     }
  764.                     $assetWithSamePath Asset::getByPath($intendedPath $asset->getKey());
  765.                     if ($assetWithSamePath != null) {
  766.                         $allowUpdate false;
  767.                     }
  768.                     if ($asset->isLocked()) {
  769.                         $allowUpdate false;
  770.                     }
  771.                 }
  772.             }
  773.             if ($allowUpdate) {
  774.                 if ($request->get('filename') != $asset->getFilename() && !$asset->isAllowed('rename')) {
  775.                     unset($updateData['filename']);
  776.                     Logger::debug('prevented renaming asset because of missing permissions ');
  777.                 }
  778.                 $asset->setValues($updateData);
  779.                 try {
  780.                     $asset->save();
  781.                     $success true;
  782.                 } catch (\Exception $e) {
  783.                     return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  784.                 }
  785.             } else {
  786.                 $msg 'prevented moving asset, asset with same path+key already exists at target location or the asset is locked. ID: ' $asset->getId();
  787.                 Logger::debug($msg);
  788.                 return $this->adminJson(['success' => $success'message' => $msg]);
  789.             }
  790.         } elseif ($asset->isAllowed('rename') && $request->get('filename')) {
  791.             //just rename
  792.             try {
  793.                 $asset->setFilename($request->get('filename'));
  794.                 $asset->save();
  795.                 $success true;
  796.             } catch (\Exception $e) {
  797.                 return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  798.             }
  799.         } else {
  800.             Logger::debug('prevented update asset because of missing permissions ');
  801.         }
  802.         return $this->adminJson(['success' => $success]);
  803.     }
  804.     /**
  805.      * @Route("/webdav{path}", name="pimcore_admin_webdav", requirements={"path"=".*"})
  806.      */
  807.     public function webdavAction()
  808.     {
  809.         $homeDir Asset::getById(1);
  810.         try {
  811.             $publicDir = new Asset\WebDAV\Folder($homeDir);
  812.             $objectTree = new Asset\WebDAV\Tree($publicDir);
  813.             $server = new \Sabre\DAV\Server($objectTree);
  814.             $server->setBaseUri($this->generateUrl('pimcore_admin_webdav', ['path' => '/']));
  815.             // lock plugin
  816.             /** @var \Doctrine\DBAL\Driver\PDOConnection $pdo */
  817.             $pdo \Pimcore\Db::get()->getWrappedConnection();
  818.             $lockBackend = new \Sabre\DAV\Locks\Backend\PDO($pdo);
  819.             $lockBackend->tableName 'webdav_locks';
  820.             $lockPlugin = new \Sabre\DAV\Locks\Plugin($lockBackend);
  821.             $server->addPlugin($lockPlugin);
  822.             // browser plugin
  823.             $server->addPlugin(new \Sabre\DAV\Browser\Plugin());
  824.             $server->start();
  825.         } catch (\Exception $e) {
  826.             Logger::error((string) $e);
  827.         }
  828.         exit;
  829.     }
  830.     /**
  831.      * @Route("/save", name="pimcore_admin_asset_save", methods={"PUT","POST"})
  832.      *
  833.      * @param Request $request
  834.      * @param EventDispatcherInterface $eventDispatcher
  835.      *
  836.      * @return JsonResponse
  837.      *
  838.      * @throws \Exception
  839.      */
  840.     public function saveAction(Request $requestEventDispatcherInterface $eventDispatcher)
  841.     {
  842.         $asset Asset::getById((int) $request->get('id'));
  843.         if (!$asset) {
  844.             throw $this->createNotFoundException('Asset not found');
  845.         }
  846.         if ($asset->isAllowed('publish')) {
  847.             // metadata
  848.             if ($request->get('metadata')) {
  849.                 $metadata $this->decodeJson($request->get('metadata'));
  850.                 $metadataEvent = new GenericEvent($this, [
  851.                     'id' => $asset->getId(),
  852.                     'metadata' => $metadata,
  853.                 ]);
  854.                 $eventDispatcher->dispatch($metadataEventAdminEvents::ASSET_METADATA_PRE_SET);
  855.                 $metadata $metadataEvent->getArgument('metadata');
  856.                 $metadataValues $metadata['values'];
  857.                 $metadataValues Asset\Service::minimizeMetadata($metadataValues'editor');
  858.                 $asset->setMetadataRaw($metadataValues);
  859.             }
  860.             // properties
  861.             if ($request->get('properties')) {
  862.                 $properties = [];
  863.                 $propertiesData $this->decodeJson($request->get('properties'));
  864.                 if (is_array($propertiesData)) {
  865.                     foreach ($propertiesData as $propertyName => $propertyData) {
  866.                         $value $propertyData['data'];
  867.                         try {
  868.                             $property = new Model\Property();
  869.                             $property->setType($propertyData['type']);
  870.                             $property->setName($propertyName);
  871.                             $property->setCtype('asset');
  872.                             $property->setDataFromEditmode($value);
  873.                             $property->setInheritable($propertyData['inheritable']);
  874.                             $properties[$propertyName] = $property;
  875.                         } catch (\Exception $e) {
  876.                             Logger::err("Can't add " $propertyName ' to asset ' $asset->getRealFullPath());
  877.                         }
  878.                     }
  879.                     $asset->setProperties($properties);
  880.                 }
  881.             }
  882.             $this->applySchedulerDataToElement($request$asset);
  883.             if ($request->get('data')) {
  884.                 $asset->setData($request->get('data'));
  885.             }
  886.             // image specific data
  887.             if ($asset instanceof Asset\Image) {
  888.                 if ($request->get('image')) {
  889.                     $imageData $this->decodeJson($request->get('image'));
  890.                     if (isset($imageData['focalPoint'])) {
  891.                         $asset->setCustomSetting('focalPointX'$imageData['focalPoint']['x']);
  892.                         $asset->setCustomSetting('focalPointY'$imageData['focalPoint']['y']);
  893.                         $asset->removeCustomSetting('disableFocalPointDetection');
  894.                     }
  895.                 } else {
  896.                     // wipe all data
  897.                     $asset->removeCustomSetting('focalPointX');
  898.                     $asset->removeCustomSetting('focalPointY');
  899.                     $asset->setCustomSetting('disableFocalPointDetection'true);
  900.                 }
  901.             }
  902.             $asset->setUserModification($this->getAdminUser()->getId());
  903.             if ($request->get('task') === 'session') {
  904.                 // save to session only
  905.                 Asset\Service::saveElementToSession($asset);
  906.             } else {
  907.                 $asset->save();
  908.             }
  909.             $treeData $this->getTreeNodeConfig($asset);
  910.             return $this->adminJson([
  911.                 'success' => true,
  912.                 'data' => [
  913.                     'versionDate' => $asset->getModificationDate(),
  914.                     'versionCount' => $asset->getVersionCount(),
  915.                 ],
  916.                 'treeData' => $treeData,
  917.             ]);
  918.         } else {
  919.             throw $this->createAccessDeniedHttpException();
  920.         }
  921.     }
  922.     /**
  923.      * @Route("/publish-version", name="pimcore_admin_asset_publishversion", methods={"POST"})
  924.      *
  925.      * @param Request $request
  926.      *
  927.      * @return JsonResponse
  928.      */
  929.     public function publishVersionAction(Request $request)
  930.     {
  931.         $version Model\Version::getById((int) $request->get('id'));
  932.         $asset $version->loadData();
  933.         $currentAsset Asset::getById($asset->getId());
  934.         if ($currentAsset->isAllowed('publish')) {
  935.             try {
  936.                 $asset->setUserModification($this->getAdminUser()->getId());
  937.                 $asset->save();
  938.                 $treeData $this->getTreeNodeConfig($asset);
  939.                 return $this->adminJson(['success' => true'treeData' => $treeData]);
  940.             } catch (\Exception $e) {
  941.                 return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  942.             }
  943.         }
  944.         throw $this->createAccessDeniedHttpException();
  945.     }
  946.     /**
  947.      * @Route("/show-version", name="pimcore_admin_asset_showversion", methods={"GET"})
  948.      *
  949.      * @param Request $request
  950.      *
  951.      * @return Response
  952.      */
  953.     public function showVersionAction(Request $request)
  954.     {
  955.         $id = (int)$request->get('id');
  956.         $version Model\Version::getById($id);
  957.         if (!$version) {
  958.             throw $this->createNotFoundException('Version not found');
  959.         }
  960.         $asset $version->loadData();
  961.         if (!$asset->isAllowed('versions')) {
  962.             throw $this->createAccessDeniedHttpException('Permission denied, version id [' $id ']');
  963.         }
  964.         $loader \Pimcore::getContainer()->get('pimcore.implementation_loader.asset.metadata.data');
  965.         return $this->render(
  966.             '@PimcoreAdmin/Admin/Asset/showVersion' ucfirst($asset->getType()) . '.html.twig',
  967.             [
  968.                 'asset' => $asset,
  969.                 'loader' => $loader,
  970.             ]
  971.         );
  972.     }
  973.     /**
  974.      * @Route("/download", name="pimcore_admin_asset_download", methods={"GET"})
  975.      *
  976.      * @param Request $request
  977.      *
  978.      * @return StreamedResponse
  979.      */
  980.     public function downloadAction(Request $request)
  981.     {
  982.         $asset Asset::getById((int) $request->get('id'));
  983.         if (!$asset) {
  984.             throw $this->createNotFoundException('Asset not found');
  985.         }
  986.         if (!$asset->isAllowed('view')) {
  987.             throw $this->createAccessDeniedException('not allowed to view asset');
  988.         }
  989.         $stream $asset->getStream();
  990.         if (!is_resource($stream)) {
  991.             throw $this->createNotFoundException('Unable to get resource for asset ' $asset->getId());
  992.         }
  993.         return new StreamedResponse(function () use ($stream) {
  994.             fpassthru($stream);
  995.         }, 200, [
  996.             'Content-Type' => $asset->getMimeType(),
  997.             'Content-Disposition' => sprintf('attachment; filename="%s"'$asset->getFilename()),
  998.             'Content-Length' => $asset->getFileSize(),
  999.         ]);
  1000.     }
  1001.     /**
  1002.      * @Route("/download-image-thumbnail", name="pimcore_admin_asset_downloadimagethumbnail", methods={"GET"})
  1003.      *
  1004.      * @param Request $request
  1005.      *
  1006.      * @return BinaryFileResponse
  1007.      */
  1008.     public function downloadImageThumbnailAction(Request $request)
  1009.     {
  1010.         $image Asset\Image::getById((int) $request->get('id'));
  1011.         if (!$image) {
  1012.             throw $this->createNotFoundException('Asset not found');
  1013.         }
  1014.         if (!$image->isAllowed('view')) {
  1015.             throw $this->createAccessDeniedException('not allowed to view thumbnail');
  1016.         }
  1017.         $config null;
  1018.         $thumbnail null;
  1019.         $thumbnailName $request->get('thumbnail');
  1020.         $thumbnailFile null;
  1021.         $deleteThumbnail true;
  1022.         if ($request->get('config')) {
  1023.             $config $this->decodeJson($request->get('config'));
  1024.         } elseif ($request->get('type')) {
  1025.             $predefined = [
  1026.                 'web' => [
  1027.                     'resize_mode' => 'scaleByWidth',
  1028.                     'width' => 3500,
  1029.                     'dpi' => 72,
  1030.                     'format' => 'JPEG',
  1031.                     'quality' => 85,
  1032.                 ],
  1033.                 'print' => [
  1034.                     'resize_mode' => 'scaleByWidth',
  1035.                     'width' => 6000,
  1036.                     'dpi' => 300,
  1037.                     'format' => 'JPEG',
  1038.                     'quality' => 95,
  1039.                 ],
  1040.                 'office' => [
  1041.                     'resize_mode' => 'scaleByWidth',
  1042.                     'width' => 1190,
  1043.                     'dpi' => 144,
  1044.                     'format' => 'JPEG',
  1045.                     'quality' => 90,
  1046.                 ],
  1047.             ];
  1048.             $config $predefined[$request->get('type')];
  1049.         } elseif ($thumbnailName) {
  1050.             $thumbnail $image->getThumbnail($thumbnailName);
  1051.             $deleteThumbnail false;
  1052.         }
  1053.         if ($config) {
  1054.             $thumbnailConfig = new Asset\Image\Thumbnail\Config();
  1055.             $thumbnailConfig->setName('pimcore-download-' $image->getId() . '-' md5($request->get('config')));
  1056.             if ($config['resize_mode'] == 'scaleByWidth') {
  1057.                 $thumbnailConfig->addItem('scaleByWidth', [
  1058.                     'width' => $config['width'],
  1059.                 ]);
  1060.             } elseif ($config['resize_mode'] == 'scaleByHeight') {
  1061.                 $thumbnailConfig->addItem('scaleByHeight', [
  1062.                     'height' => $config['height'],
  1063.                 ]);
  1064.             } else {
  1065.                 $thumbnailConfig->addItem('resize', [
  1066.                     'width' => $config['width'],
  1067.                     'height' => $config['height'],
  1068.                 ]);
  1069.             }
  1070.             $thumbnailConfig->setQuality($config['quality']);
  1071.             $thumbnailConfig->setFormat($config['format']);
  1072.             $thumbnailConfig->setRasterizeSVG(true);
  1073.             if ($thumbnailConfig->getFormat() == 'JPEG') {
  1074.                 $thumbnailConfig->setPreserveMetaData(true);
  1075.                 if (empty($config['quality'])) {
  1076.                     $thumbnailConfig->setPreserveColor(true);
  1077.                 }
  1078.             }
  1079.             $thumbnail $image->getThumbnail($thumbnailConfig);
  1080.             $thumbnailFile $thumbnail->getLocalFile();
  1081.             $exiftool \Pimcore\Tool\Console::getExecutable('exiftool');
  1082.             if ($thumbnailConfig->getFormat() == 'JPEG' && $exiftool && isset($config['dpi']) && $config['dpi']) {
  1083.                 $process = new Process([$exiftool'-overwrite_original''-xresolution=' . (int)$config['dpi'], '-yresolution=' . (int)$config['dpi'], '-resolutionunit=inches'$thumbnailFile]);
  1084.                 $process->run();
  1085.             }
  1086.         }
  1087.         if ($thumbnail) {
  1088.             $thumbnailFile $thumbnailFile ?: $thumbnail->getLocalFile();
  1089.             $downloadFilename preg_replace(
  1090.                 '/\.' preg_quote(File::getFileExtension($image->getFilename())) . '$/i',
  1091.                 '.' $thumbnail->getFileExtension(),
  1092.                 $image->getFilename()
  1093.             );
  1094.             $downloadFilename strtolower($downloadFilename);
  1095.             clearstatcache();
  1096.             $response = new BinaryFileResponse($thumbnailFile);
  1097.             $response->headers->set('Content-Type'$thumbnail->getMimeType());
  1098.             $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT$downloadFilename);
  1099.             $this->addThumbnailCacheHeaders($response);
  1100.             $response->deleteFileAfterSend($deleteThumbnail);
  1101.             return $response;
  1102.         }
  1103.         throw $this->createNotFoundException('Thumbnail not found');
  1104.     }
  1105.     /**
  1106.      * @Route("/get-asset", name="pimcore_admin_asset_getasset", methods={"GET"})
  1107.      *
  1108.      * @param Request $request
  1109.      *
  1110.      * @return StreamedResponse
  1111.      */
  1112.     public function getAssetAction(Request $request)
  1113.     {
  1114.         $image Asset::getById((int)$request->get('id'));
  1115.         if (!$image) {
  1116.             throw $this->createNotFoundException('Asset not found');
  1117.         }
  1118.         if (!$image->isAllowed('view')) {
  1119.             throw $this->createAccessDeniedException('not allowed to view asset');
  1120.         }
  1121.         $stream $image->getStream();
  1122.         if (!is_resource($stream)) {
  1123.             throw $this->createNotFoundException('Unable to get resource for asset ' $image->getId());
  1124.         }
  1125.         $response = new StreamedResponse(function () use ($stream) {
  1126.             fpassthru($stream);
  1127.         }, 200, [
  1128.             'Content-Type' => $image->getMimeType(),
  1129.             'Access-Control-Allow-Origin' => '*',
  1130.         ]);
  1131.         $this->addThumbnailCacheHeaders($response);
  1132.         return $response;
  1133.     }
  1134.     /**
  1135.      * @Route("/get-image-thumbnail", name="pimcore_admin_asset_getimagethumbnail", methods={"GET"})
  1136.      *
  1137.      * @param Request $request
  1138.      *
  1139.      * @return StreamedResponse|JsonResponse|BinaryFileResponse
  1140.      */
  1141.     public function getImageThumbnailAction(Request $request)
  1142.     {
  1143.         $fileinfo $request->get('fileinfo');
  1144.         $image Asset\Image::getById((int)$request->get('id'));
  1145.         if (!$image) {
  1146.             throw $this->createNotFoundException('Asset not found');
  1147.         }
  1148.         if (!$image->isAllowed('view')) {
  1149.             throw $this->createAccessDeniedException('not allowed to view thumbnail');
  1150.         }
  1151.         $thumbnailConfig null;
  1152.         if ($request->get('thumbnail')) {
  1153.             $thumbnailConfig $image->getThumbnailConfig($request->get('thumbnail'));
  1154.         }
  1155.         if (!$thumbnailConfig) {
  1156.             if ($request->get('config')) {
  1157.                 $thumbnailConfig $image->getThumbnailConfig($this->decodeJson($request->get('config')));
  1158.             } else {
  1159.                 $thumbnailConfig $image->getThumbnailConfig(array_merge($request->request->all(), $request->query->all()));
  1160.             }
  1161.         } else {
  1162.             // no high-res images in admin mode (editmode)
  1163.             // this is mostly because of the document's image editable, which doesn't know anything about the thumbnail
  1164.             // configuration, so the dimensions would be incorrect (double the size)
  1165.             $thumbnailConfig->setHighResolution(1);
  1166.         }
  1167.         $format strtolower($thumbnailConfig->getFormat());
  1168.         if ($format == 'source' || $format == 'print') {
  1169.             $thumbnailConfig->setFormat('PNG');
  1170.             $thumbnailConfig->setRasterizeSVG(true);
  1171.         }
  1172.         if ($request->get('treepreview')) {
  1173.             $thumbnailConfig Asset\Image\Thumbnail\Config::getPreviewConfig();
  1174.             if ($request->get('origin') === 'treeNode' && !$image->getThumbnail($thumbnailConfig)->exists()) {
  1175.                 \Pimcore::getContainer()->get('messenger.bus.pimcore-core')->dispatch(
  1176.                     new AssetPreviewImageMessage($image->getId())
  1177.                 );
  1178.                 throw $this->createNotFoundException(sprintf('Tree preview thumbnail not available for asset %s'$image->getId()));
  1179.             }
  1180.         }
  1181.         $cropPercent $request->get('cropPercent');
  1182.         if ($cropPercent && filter_var($cropPercentFILTER_VALIDATE_BOOLEAN)) {
  1183.             $thumbnailConfig->addItemAt(0'cropPercent', [
  1184.                 'width' => $request->get('cropWidth'),
  1185.                 'height' => $request->get('cropHeight'),
  1186.                 'y' => $request->get('cropTop'),
  1187.                 'x' => $request->get('cropLeft'),
  1188.             ]);
  1189.             $hash md5(Tool\Serialize::serialize(array_merge($request->request->all(), $request->query->all())));
  1190.             $thumbnailConfig->setName($thumbnailConfig->getName() . '_auto_' $hash);
  1191.         }
  1192.         $thumbnail $image->getThumbnail($thumbnailConfig);
  1193.         if ($fileinfo) {
  1194.             return $this->adminJson([
  1195.                 'width' => $thumbnail->getWidth(),
  1196.                 'height' => $thumbnail->getHeight(), ]);
  1197.         }
  1198.         $stream $thumbnail->getStream();
  1199.         if (!$stream) {
  1200.             return new BinaryFileResponse(PIMCORE_PATH '/bundles/AdminBundle/Resources/public/img/filetype-not-supported.svg');
  1201.         }
  1202.         $response = new StreamedResponse(function () use ($stream) {
  1203.             fpassthru($stream);
  1204.         }, 200, [
  1205.             'Content-Type' => $thumbnail->getMimeType(),
  1206.             'Access-Control-Allow-Origin''*',
  1207.         ]);
  1208.         $this->addThumbnailCacheHeaders($response);
  1209.         return $response;
  1210.     }
  1211.     /**
  1212.      * @Route("/get-folder-thumbnail", name="pimcore_admin_asset_getfolderthumbnail", methods={"GET"})
  1213.      *
  1214.      * @param Request $request
  1215.      *
  1216.      * @return StreamedResponse
  1217.      */
  1218.     public function getFolderThumbnailAction(Request $request)
  1219.     {
  1220.         $folder null;
  1221.         if ($request->get('id')) {
  1222.             $folder Asset\Folder::getById((int)$request->get('id'));
  1223.             if ($folder instanceof  Asset\Folder) {
  1224.                 if (!$folder->isAllowed('view')) {
  1225.                     throw $this->createAccessDeniedException('not allowed to view thumbnail');
  1226.                 }
  1227.                 $stream $folder->getPreviewImage();
  1228.                 if (!$stream) {
  1229.                     throw $this->createNotFoundException(sprintf('Tree preview thumbnail not available for asset %s'$folder->getId()));
  1230.                 } else {
  1231.                     $response = new StreamedResponse(function () use ($stream) {
  1232.                         fpassthru($stream);
  1233.                     }, 200, [
  1234.                         'Content-Type' => 'image/jpg',
  1235.                     ]);
  1236.                 }
  1237.                 $this->addThumbnailCacheHeaders($response);
  1238.                 return $response;
  1239.             }
  1240.         }
  1241.         throw $this->createNotFoundException('could not load asset folder');
  1242.     }
  1243.     /**
  1244.      * @Route("/get-video-thumbnail", name="pimcore_admin_asset_getvideothumbnail", methods={"GET"})
  1245.      *
  1246.      * @param Request $request
  1247.      *
  1248.      * @return StreamedResponse
  1249.      */
  1250.     public function getVideoThumbnailAction(Request $request)
  1251.     {
  1252.         $video null;
  1253.         if ($request->get('id')) {
  1254.             $video Asset\Video::getById((int)$request->get('id'));
  1255.         } elseif ($request->get('path')) {
  1256.             $video Asset\Video::getByPath($request->get('path'));
  1257.         }
  1258.         if (!$video) {
  1259.             throw $this->createNotFoundException('could not load video asset');
  1260.         }
  1261.         if (!$video->isAllowed('view')) {
  1262.             throw $this->createAccessDeniedException('not allowed to view thumbnail');
  1263.         }
  1264.         $thumbnail array_merge($request->request->all(), $request->query->all());
  1265.         if ($request->get('treepreview')) {
  1266.             $thumbnail Asset\Image\Thumbnail\Config::getPreviewConfig();
  1267.         }
  1268.         $time null;
  1269.         if (is_numeric($request->get('time'))) {
  1270.             $time = (int)$request->get('time');
  1271.         }
  1272.         if ($request->get('settime')) {
  1273.             $video->removeCustomSetting('image_thumbnail_asset');
  1274.             $video->setCustomSetting('image_thumbnail_time'$time);
  1275.             $video->save();
  1276.         }
  1277.         $image null;
  1278.         if ($request->get('image')) {
  1279.             $image Asset\Image::getById((int)$request->get('image'));
  1280.         }
  1281.         if ($request->get('setimage') && $image) {
  1282.             $video->removeCustomSetting('image_thumbnail_time');
  1283.             $video->setCustomSetting('image_thumbnail_asset'$image->getId());
  1284.             $video->save();
  1285.         }
  1286.         $thumb $video->getImageThumbnail($thumbnail$time$image);
  1287.         if ($request->get('origin') === 'treeNode' && !$thumb->exists()) {
  1288.             \Pimcore::getContainer()->get('messenger.bus.pimcore-core')->dispatch(
  1289.                 new AssetPreviewImageMessage($video->getId())
  1290.             );
  1291.             throw $this->createNotFoundException(sprintf('Tree preview thumbnail not available for asset %s'$video->getId()));
  1292.         }
  1293.         $stream $thumb->getStream();
  1294.         if (!$stream) {
  1295.             throw $this->createNotFoundException('Unable to get video thumbnail for video ' $video->getId());
  1296.         }
  1297.         $response = new StreamedResponse(function () use ($stream) {
  1298.             fpassthru($stream);
  1299.         }, 200, [
  1300.             'Content-Type' => 'image/' $thumb->getFileExtension(),
  1301.         ]);
  1302.         $this->addThumbnailCacheHeaders($response);
  1303.         return $response;
  1304.     }
  1305.     /**
  1306.      * @Route("/get-document-thumbnail", name="pimcore_admin_asset_getdocumentthumbnail", methods={"GET"})
  1307.      *
  1308.      * @param Request $request
  1309.      *
  1310.      * @return StreamedResponse|BinaryFileResponse
  1311.      */
  1312.     public function getDocumentThumbnailAction(Request $request)
  1313.     {
  1314.         $document Asset\Document::getById((int)$request->get('id'));
  1315.         if (!$document) {
  1316.             throw $this->createNotFoundException('could not load document asset');
  1317.         }
  1318.         if (!$document->isAllowed('view')) {
  1319.             throw $this->createAccessDeniedException('not allowed to view thumbnail');
  1320.         }
  1321.         $thumbnail Asset\Image\Thumbnail\Config::getByAutoDetect(array_merge($request->request->all(), $request->query->all()));
  1322.         $format strtolower($thumbnail->getFormat());
  1323.         if ($format == 'source') {
  1324.             $thumbnail->setFormat('jpeg'); // default format for documents is JPEG not PNG (=too big)
  1325.         }
  1326.         if ($request->get('treepreview')) {
  1327.             $thumbnail Asset\Image\Thumbnail\Config::getPreviewConfig();
  1328.         }
  1329.         $page 1;
  1330.         if (is_numeric($request->get('page'))) {
  1331.             $page = (int)$request->get('page');
  1332.         }
  1333.         $thumb $document->getImageThumbnail($thumbnail$page);
  1334.         if ($request->get('origin') === 'treeNode' && !$thumb->exists()) {
  1335.             \Pimcore::getContainer()->get('messenger.bus.pimcore-core')->dispatch(
  1336.                 new AssetPreviewImageMessage($document->getId())
  1337.             );
  1338.             throw $this->createNotFoundException(sprintf('Tree preview thumbnail not available for asset %s'$document->getId()));
  1339.         }
  1340.         $stream $thumb->getStream();
  1341.         if ($stream) {
  1342.             $response = new StreamedResponse(function () use ($stream) {
  1343.                 fpassthru($stream);
  1344.             }, 200, [
  1345.                 'Content-Type' => 'image/' $thumb->getFileExtension(),
  1346.             ]);
  1347.         } else {
  1348.             $response = new BinaryFileResponse(PIMCORE_PATH '/bundles/AdminBundle/Resources/public/img/filetype-not-supported.svg');
  1349.         }
  1350.         $this->addThumbnailCacheHeaders($response);
  1351.         return $response;
  1352.     }
  1353.     /**
  1354.      * @param Response $response
  1355.      */
  1356.     protected function addThumbnailCacheHeaders(Response $response)
  1357.     {
  1358.         $lifetime 300;
  1359.         $date = new \DateTime('now');
  1360.         $date->add(new \DateInterval('PT' $lifetime 'S'));
  1361.         $response->setMaxAge($lifetime);
  1362.         $response->setPublic();
  1363.         $response->setExpires($date);
  1364.         $response->headers->set('Pragma''');
  1365.     }
  1366.     /**
  1367.      * @Route("/get-preview-document", name="pimcore_admin_asset_getpreviewdocument", methods={"GET"})
  1368.      *
  1369.      * @param Request $request
  1370.      *
  1371.      * @return StreamedResponse
  1372.      */
  1373.     public function getPreviewDocumentAction(Request $request)
  1374.     {
  1375.         $asset Asset\Document::getById((int) $request->get('id'));
  1376.         if (!$asset) {
  1377.             throw $this->createNotFoundException('could not load document asset');
  1378.         }
  1379.         if ($asset->isAllowed('view')) {
  1380.             $stream $this->getDocumentPreviewPdf($asset);
  1381.             if ($stream) {
  1382.                 return new StreamedResponse(function () use ($stream) {
  1383.                     fpassthru($stream);
  1384.                 }, 200, [
  1385.                     'Content-Type' => 'application/pdf',
  1386.                 ]);
  1387.             } else {
  1388.                 throw $this->createNotFoundException('Unable to get preview for asset ' $asset->getId());
  1389.             }
  1390.         } else {
  1391.             throw $this->createAccessDeniedException('Access to asset ' $asset->getId() . ' denied');
  1392.         }
  1393.     }
  1394.     /**
  1395.      * @param Asset\Document $asset
  1396.      *
  1397.      * @return resource|null
  1398.      */
  1399.     protected function getDocumentPreviewPdf(Asset\Document $asset)
  1400.     {
  1401.         $stream null;
  1402.         if ($asset->getMimeType() == 'application/pdf') {
  1403.             $stream $asset->getStream();
  1404.         }
  1405.         if (!$stream && $asset->getPageCount() && \Pimcore\Document::isAvailable() && \Pimcore\Document::isFileTypeSupported($asset->getFilename())) {
  1406.             try {
  1407.                 $document \Pimcore\Document::getInstance();
  1408.                 $stream $document->getPdf($asset);
  1409.             } catch (\Exception $e) {
  1410.                 // nothing to do
  1411.             }
  1412.         }
  1413.         return $stream;
  1414.     }
  1415.     /**
  1416.      * @Route("/get-preview-video", name="pimcore_admin_asset_getpreviewvideo", methods={"GET"})
  1417.      *
  1418.      * @param Request $request
  1419.      *
  1420.      * @return Response
  1421.      */
  1422.     public function getPreviewVideoAction(Request $request)
  1423.     {
  1424.         $asset Asset\Video::getById((int) $request->get('id'));
  1425.         if (!$asset) {
  1426.             throw $this->createNotFoundException('could not load video asset');
  1427.         }
  1428.         if (!$asset->isAllowed('view')) {
  1429.             throw $this->createAccessDeniedException('not allowed to preview');
  1430.         }
  1431.         $previewData = ['asset' => $asset];
  1432.         $config Asset\Video\Thumbnail\Config::getPreviewConfig();
  1433.         $thumbnail $asset->getThumbnail($config, ['mp4']);
  1434.         if ($thumbnail) {
  1435.             $previewData['asset'] = $asset;
  1436.             $previewData['thumbnail'] = $thumbnail;
  1437.             if ($thumbnail['status'] == 'finished') {
  1438.                 return $this->render(
  1439.                     '@PimcoreAdmin/Admin/Asset/getPreviewVideoDisplay.html.twig',
  1440.                     $previewData
  1441.                 );
  1442.             } else {
  1443.                 return $this->render(
  1444.                     '@PimcoreAdmin/Admin/Asset/getPreviewVideoError.html.twig',
  1445.                     $previewData
  1446.                 );
  1447.             }
  1448.         } else {
  1449.             return $this->render(
  1450.                 '@PimcoreAdmin/Admin/Asset/getPreviewVideoError.html.twig',
  1451.                 $previewData
  1452.             );
  1453.         }
  1454.     }
  1455.     /**
  1456.      * @Route("/serve-video-preview", name="pimcore_admin_asset_servevideopreview", methods={"GET"})
  1457.      *
  1458.      * @param Request $request
  1459.      *
  1460.      * @return StreamedResponse
  1461.      */
  1462.     public function serveVideoPreviewAction(Request $request)
  1463.     {
  1464.         $asset Asset\Video::getById((int) $request->get('id'));
  1465.         if (!$asset) {
  1466.             throw $this->createNotFoundException('could not load video asset');
  1467.         }
  1468.         if (!$asset->isAllowed('view')) {
  1469.             throw $this->createAccessDeniedException('not allowed to preview');
  1470.         }
  1471.         $config Asset\Video\Thumbnail\Config::getPreviewConfig();
  1472.         $thumbnail $asset->getThumbnail($config, ['mp4']);
  1473.         $storagePath $asset->getRealPath() . '/' preg_replace('@^' preg_quote($asset->getPath(), '@') . '@'''urldecode($thumbnail['formats']['mp4']));
  1474.         $storage Tool\Storage::get('thumbnail');
  1475.         if ($storage->fileExists($storagePath)) {
  1476.             $fs $storage->fileSize($storagePath);
  1477.             $stream $storage->readStream($storagePath);
  1478.             return new StreamedResponse(function () use ($stream) {
  1479.                 fpassthru($stream);
  1480.             }, 200, [
  1481.                 'Content-Type' => 'video/mp4',
  1482.                 'Content-Length' => $fs,
  1483.                 'Accept-Ranges' => 'bytes',
  1484.             ]);
  1485.         } else {
  1486.             throw $this->createNotFoundException('Video thumbnail not found');
  1487.         }
  1488.     }
  1489.     /**
  1490.      * @Route("/image-editor", name="pimcore_admin_asset_imageeditor", methods={"GET"})
  1491.      *
  1492.      * @param Request $request
  1493.      *
  1494.      * @return Response
  1495.      */
  1496.     public function imageEditorAction(Request $request)
  1497.     {
  1498.         $asset Asset::getById((int) $request->get('id'));
  1499.         if (!$asset->isAllowed('view')) {
  1500.             throw $this->createAccessDeniedException('Not allowed to preview');
  1501.         }
  1502.         return $this->render(
  1503.             '@PimcoreAdmin/Admin/Asset/imageEditor.html.twig',
  1504.             ['asset' => $asset]
  1505.         );
  1506.     }
  1507.     /**
  1508.      * @Route("/image-editor-save", name="pimcore_admin_asset_imageeditorsave", methods={"PUT"})
  1509.      *
  1510.      * @param Request $request
  1511.      *
  1512.      * @return JsonResponse
  1513.      */
  1514.     public function imageEditorSaveAction(Request $request)
  1515.     {
  1516.         $asset Asset::getById((int) $request->get('id'));
  1517.         if (!$asset) {
  1518.             throw $this->createNotFoundException('Asset not found');
  1519.         }
  1520.         if (!$asset->isAllowed('publish')) {
  1521.             throw $this->createAccessDeniedException('not allowed to publish');
  1522.         }
  1523.         $data $request->get('dataUri');
  1524.         $data substr($datastrpos($data','));
  1525.         $data base64_decode($data);
  1526.         $asset->setData($data);
  1527.         $asset->setUserModification($this->getAdminUser()->getId());
  1528.         $asset->save();
  1529.         return $this->adminJson(['success' => true]);
  1530.     }
  1531.     /**
  1532.      * @Route("/get-folder-content-preview", name="pimcore_admin_asset_getfoldercontentpreview", methods={"GET"})
  1533.      *
  1534.      * @param Request $request
  1535.      *
  1536.      * @return JsonResponse
  1537.      */
  1538.     public function getFolderContentPreviewAction(Request $requestEventDispatcherInterface $eventDispatcher)
  1539.     {
  1540.         $allParams array_merge($request->request->all(), $request->query->all());
  1541.         $filterPrepareEvent = new GenericEvent($this, [
  1542.             'requestParams' => $allParams,
  1543.         ]);
  1544.         $eventDispatcher->dispatch($filterPrepareEventAdminEvents::ASSET_LIST_BEFORE_FILTER_PREPARE);
  1545.         $allParams $filterPrepareEvent->getArgument('requestParams');
  1546.         $folder Asset::getById($allParams['id']);
  1547.         $start 0;
  1548.         $limit 10;
  1549.         if ($allParams['limit']) {
  1550.             $limit $allParams['limit'];
  1551.         }
  1552.         if ($allParams['start']) {
  1553.             $start $allParams['start'];
  1554.         }
  1555.         $conditionFilters = [];
  1556.         $list = new Asset\Listing();
  1557.         $conditionFilters[] = 'path LIKE ' . ($folder->getRealFullPath() == '/' "'/%'" $list->quote(Helper::escapeLike($folder->getRealFullPath()) . '/%')) . " AND type != 'folder'";
  1558.         if (!$this->getAdminUser()->isAdmin()) {
  1559.             $userIds $this->getAdminUser()->getRoles();
  1560.             $userIds[] = $this->getAdminUser()->getId();
  1561.             $conditionFilters[] = ' (
  1562.                                                     (select list from users_workspaces_asset where userId in (' implode(','$userIds) . ') and LOCATE(CONCAT(path, filename),cpath)=1  ORDER BY LENGTH(cpath) DESC LIMIT 1)=1
  1563.                                                     OR
  1564.                                                     (select list from users_workspaces_asset where userId in (' implode(','$userIds) . ') and LOCATE(cpath,CONCAT(path, filename))=1  ORDER BY LENGTH(cpath) DESC LIMIT 1)=1
  1565.                                                  )';
  1566.         }
  1567.         $condition implode(' AND '$conditionFilters);
  1568.         $list->setCondition($condition);
  1569.         $list->setLimit($limit);
  1570.         $list->setOffset($start);
  1571.         $list->setOrderKey('CAST(filename AS CHAR CHARACTER SET utf8) COLLATE utf8_general_ci ASC'false);
  1572.         $beforeListLoadEvent = new GenericEvent($this, [
  1573.             'list' => $list,
  1574.             'context' => $allParams,
  1575.         ]);
  1576.         $eventDispatcher->dispatch($beforeListLoadEventAdminEvents::ASSET_LIST_BEFORE_LIST_LOAD);
  1577.         /** @var Asset\Listing $list */
  1578.         $list $beforeListLoadEvent->getArgument('list');
  1579.         $list->load();
  1580.         $assets = [];
  1581.         foreach ($list as $asset) {
  1582.             $filenameDisplay $asset->getFilename();
  1583.             if (strlen($filenameDisplay) > 32) {
  1584.                 $filenameDisplay substr($filenameDisplay025) . '...' \Pimcore\File::getFileExtension($filenameDisplay);
  1585.             }
  1586.             // Like for treeGetChildsByIdAction, so we respect isAllowed method which can be extended (object DI) for custom permissions, so relying only users_workspaces_asset is insufficient and could lead security breach
  1587.             if ($asset->isAllowed('list')) {
  1588.                 $assets[] = [
  1589.                     'id' => $asset->getId(),
  1590.                     'type' => $asset->getType(),
  1591.                     'filename' => $asset->getFilename(),
  1592.                     'filenameDisplay' => htmlspecialchars($filenameDisplay),
  1593.                     'url' => $this->getThumbnailUrl($asset),
  1594.                     'idPath' => $data['idPath'] = Element\Service::getIdPath($asset),
  1595.                 ];
  1596.             }
  1597.         }
  1598.         // We need to temporary use data key to be compatible with the ASSET_LIST_AFTER_LIST_LOAD global event
  1599.         $result = ['data' => $assets'success' => true'total' => $list->getTotalCount()];
  1600.         $afterListLoadEvent = new GenericEvent($this, [
  1601.             'list' => $result,
  1602.             'context' => $allParams,
  1603.         ]);
  1604.         $eventDispatcher->dispatch($afterListLoadEventAdminEvents::ASSET_LIST_AFTER_LIST_LOAD);
  1605.         $result $afterListLoadEvent->getArgument('list');
  1606.         // Here we revert to assets key
  1607.         return $this->adminJson(['assets' => $result['data'], 'success' => $result['success'], 'total' => $result['total']]);
  1608.     }
  1609.     /**
  1610.      * @Route("/copy-info", name="pimcore_admin_asset_copyinfo", methods={"GET"})
  1611.      *
  1612.      * @param Request $request
  1613.      *
  1614.      * @return JsonResponse
  1615.      */
  1616.     public function copyInfoAction(Request $request)
  1617.     {
  1618.         $transactionId time();
  1619.         $pasteJobs = [];
  1620.         Tool\Session::useSession(function (AttributeBagInterface $session) use ($transactionId) {
  1621.             $session->set((string) $transactionId, []);
  1622.         }, 'pimcore_copy');
  1623.         if ($request->get('type') == 'recursive') {
  1624.             $asset Asset::getById((int) $request->get('sourceId'));
  1625.             if (!$asset) {
  1626.                 throw $this->createNotFoundException('Source not found');
  1627.             }
  1628.             // first of all the new parent
  1629.             $pasteJobs[] = [[
  1630.                 'url' => $this->generateUrl('pimcore_admin_asset_copy'),
  1631.                 'method' => 'POST',
  1632.                 'params' => [
  1633.                     'sourceId' => $request->get('sourceId'),
  1634.                     'targetId' => $request->get('targetId'),
  1635.                     'type' => 'child',
  1636.                     'transactionId' => $transactionId,
  1637.                     'saveParentId' => true,
  1638.                 ],
  1639.             ]];
  1640.             if ($asset->hasChildren()) {
  1641.                 // get amount of children
  1642.                 $list = new Asset\Listing();
  1643.                 $list->setCondition('path LIKE ?', [$list->escapeLike($asset->getRealFullPath()) . '/%']);
  1644.                 $list->setOrderKey('LENGTH(path)'false);
  1645.                 $list->setOrder('ASC');
  1646.                 $childIds $list->loadIdList();
  1647.                 if (count($childIds) > 0) {
  1648.                     foreach ($childIds as $id) {
  1649.                         $pasteJobs[] = [[
  1650.                             'url' => $this->generateUrl('pimcore_admin_asset_copy'),
  1651.                             'method' => 'POST',
  1652.                             'params' => [
  1653.                                 'sourceId' => $id,
  1654.                                 'targetParentId' => $request->get('targetId'),
  1655.                                 'sourceParentId' => $request->get('sourceId'),
  1656.                                 'type' => 'child',
  1657.                                 'transactionId' => $transactionId,
  1658.                             ],
  1659.                         ]];
  1660.                     }
  1661.                 }
  1662.             }
  1663.         } elseif ($request->get('type') == 'child' || $request->get('type') == 'replace') {
  1664.             // the object itself is the last one
  1665.             $pasteJobs[] = [[
  1666.                 'url' => $this->generateUrl('pimcore_admin_asset_copy'),
  1667.                 'method' => 'POST',
  1668.                 'params' => [
  1669.                     'sourceId' => $request->get('sourceId'),
  1670.                     'targetId' => $request->get('targetId'),
  1671.                     'type' => $request->get('type'),
  1672.                     'transactionId' => $transactionId,
  1673.                 ],
  1674.             ]];
  1675.         }
  1676.         return $this->adminJson([
  1677.             'pastejobs' => $pasteJobs,
  1678.         ]);
  1679.     }
  1680.     /**
  1681.      * @Route("/copy", name="pimcore_admin_asset_copy", methods={"POST"})
  1682.      *
  1683.      * @param Request $request
  1684.      *
  1685.      * @return JsonResponse
  1686.      */
  1687.     public function copyAction(Request $request)
  1688.     {
  1689.         $success false;
  1690.         $sourceId = (int)$request->get('sourceId');
  1691.         $source Asset::getById($sourceId);
  1692.         $session Tool\Session::get('pimcore_copy');
  1693.         $sessionBag $session->get($request->get('transactionId'));
  1694.         $targetId = (int)$request->get('targetId');
  1695.         if ($request->get('targetParentId')) {
  1696.             $sourceParent Asset::getById((int) $request->get('sourceParentId'));
  1697.             // this is because the key can get the prefix "_copy" if the target does already exists
  1698.             if ($sessionBag['parentId']) {
  1699.                 $targetParent Asset::getById($sessionBag['parentId']);
  1700.             } else {
  1701.                 $targetParent Asset::getById((int) $request->get('targetParentId'));
  1702.             }
  1703.             $targetPath preg_replace('@^' $sourceParent->getRealFullPath() . '@'$targetParent '/'$source->getRealPath());
  1704.             $target Asset::getByPath($targetPath);
  1705.         } else {
  1706.             $target Asset::getById($targetId);
  1707.         }
  1708.         if (!$target) {
  1709.             throw $this->createNotFoundException('Target not found');
  1710.         }
  1711.         if ($target->isAllowed('create')) {
  1712.             $source Asset::getById($sourceId);
  1713.             if ($source != null) {
  1714.                 if ($request->get('type') == 'child') {
  1715.                     $newAsset $this->_assetService->copyAsChild($target$source);
  1716.                     // this is because the key can get the prefix "_copy" if the target does already exists
  1717.                     if ($request->get('saveParentId')) {
  1718.                         $sessionBag['parentId'] = $newAsset->getId();
  1719.                     }
  1720.                 } elseif ($request->get('type') == 'replace') {
  1721.                     $this->_assetService->copyContents($target$source);
  1722.                 }
  1723.                 $session->set($request->get('transactionId'), $sessionBag);
  1724.                 Tool\Session::writeClose();
  1725.                 $success true;
  1726.             } else {
  1727.                 Logger::debug('prevended copy/paste because asset with same path+key already exists in this location');
  1728.             }
  1729.         } else {
  1730.             Logger::error('could not execute copy/paste because of missing permissions on target [ ' $targetId ' ]');
  1731.             throw $this->createAccessDeniedHttpException();
  1732.         }
  1733.         Tool\Session::writeClose();
  1734.         return $this->adminJson(['success' => $success]);
  1735.     }
  1736.     /**
  1737.      * @Route("/download-as-zip-jobs", name="pimcore_admin_asset_downloadaszipjobs", methods={"GET"})
  1738.      *
  1739.      * @param Request $request
  1740.      *
  1741.      * @return JsonResponse
  1742.      */
  1743.     public function downloadAsZipJobsAction(Request $request)
  1744.     {
  1745.         $jobId uniqid();
  1746.         $filesPerJob 5;
  1747.         $jobs = [];
  1748.         $asset Asset::getById((int) $request->get('id'));
  1749.         if (!$asset) {
  1750.             throw $this->createNotFoundException('Asset not found');
  1751.         }
  1752.         if ($asset->isAllowed('view')) {
  1753.             $parentPath $asset->getRealFullPath();
  1754.             if ($asset->getId() == 1) {
  1755.                 $parentPath '';
  1756.             }
  1757.             $db \Pimcore\Db::get();
  1758.             $conditionFilters = [];
  1759.             $selectedIds explode(','$request->get('selectedIds'''));
  1760.             $quotedSelectedIds = [];
  1761.             foreach ($selectedIds as $selectedId) {
  1762.                 if ($selectedId) {
  1763.                     $quotedSelectedIds[] = $db->quote($selectedId);
  1764.                 }
  1765.             }
  1766.             if (!empty($quotedSelectedIds)) {
  1767.                 //add a condition if id numbers are specified
  1768.                 $conditionFilters[] = 'id IN (' implode(','$quotedSelectedIds) . ')';
  1769.             }
  1770.             $conditionFilters[] = 'path LIKE ' $db->quote(Helper::escapeLike($parentPath) . '/%') . ' AND type != ' $db->quote('folder');
  1771.             if (!$this->getAdminUser()->isAdmin()) {
  1772.                 $userIds $this->getAdminUser()->getRoles();
  1773.                 $userIds[] = $this->getAdminUser()->getId();
  1774.                 $conditionFilters[] = ' (
  1775.                                                     (select list from users_workspaces_asset where userId in (' implode(','$userIds) . ') and LOCATE(CONCAT(path, filename),cpath)=1  ORDER BY LENGTH(cpath) DESC LIMIT 1)=1
  1776.                                                     OR
  1777.                                                     (select list from users_workspaces_asset where userId in (' implode(','$userIds) . ') and LOCATE(cpath,CONCAT(path, filename))=1  ORDER BY LENGTH(cpath) DESC LIMIT 1)=1
  1778.                                                  )';
  1779.             }
  1780.             $condition implode(' AND '$conditionFilters);
  1781.             $assetList = new Asset\Listing();
  1782.             $assetList->setCondition($condition);
  1783.             $assetList->setOrderKey('LENGTH(path)'false);
  1784.             $assetList->setOrder('ASC');
  1785.             for ($i 0$i ceil($assetList->getTotalCount() / $filesPerJob); $i++) {
  1786.                 $jobs[] = [[
  1787.                     'url' => $this->generateUrl('pimcore_admin_asset_downloadaszipaddfiles'),
  1788.                     'method' => 'GET',
  1789.                     'params' => [
  1790.                         'id' => $asset->getId(),
  1791.                         'selectedIds' => implode(','$selectedIds),
  1792.                         'offset' => $i $filesPerJob,
  1793.                         'limit' => $filesPerJob,
  1794.                         'jobId' => $jobId,
  1795.                     ],
  1796.                 ]];
  1797.             }
  1798.         }
  1799.         return $this->adminJson([
  1800.             'success' => true,
  1801.             'jobs' => $jobs,
  1802.             'jobId' => $jobId,
  1803.         ]);
  1804.     }
  1805.     /**
  1806.      * @Route("/download-as-zip-add-files", name="pimcore_admin_asset_downloadaszipaddfiles", methods={"GET"})
  1807.      *
  1808.      * @param Request $request
  1809.      *
  1810.      * @return JsonResponse
  1811.      */
  1812.     public function downloadAsZipAddFilesAction(Request $request)
  1813.     {
  1814.         $zipFile PIMCORE_SYSTEM_TEMP_DIRECTORY '/download-zip-' $request->get('jobId') . '.zip';
  1815.         $asset Asset::getById((int) $request->get('id'));
  1816.         $success false;
  1817.         if (!$asset) {
  1818.             throw $this->createNotFoundException('Asset not found');
  1819.         }
  1820.         if ($asset->isAllowed('view')) {
  1821.             $zip = new \ZipArchive();
  1822.             if (!is_file($zipFile)) {
  1823.                 $zipState $zip->open($zipFile\ZipArchive::CREATE);
  1824.             } else {
  1825.                 $zipState $zip->open($zipFile);
  1826.             }
  1827.             if ($zipState === true) {
  1828.                 $parentPath $asset->getRealFullPath();
  1829.                 if ($asset->getId() == 1) {
  1830.                     $parentPath '';
  1831.                 }
  1832.                 $db \Pimcore\Db::get();
  1833.                 $conditionFilters = [];
  1834.                 $selectedIds $request->get('selectedIds', []);
  1835.                 if (!empty($selectedIds)) {
  1836.                     $selectedIds explode(','$selectedIds);
  1837.                     //add a condition if id numbers are specified
  1838.                     $conditionFilters[] = 'id IN (' implode(','$selectedIds) . ')';
  1839.                 }
  1840.                 $conditionFilters[] = "type != 'folder' AND path LIKE " $db->quote(Helper::escapeLike($parentPath) . '/%');
  1841.                 if (!$this->getAdminUser()->isAdmin()) {
  1842.                     $userIds $this->getAdminUser()->getRoles();
  1843.                     $userIds[] = $this->getAdminUser()->getId();
  1844.                     $conditionFilters[] = ' (
  1845.                                                     (select list from users_workspaces_asset where userId in (' implode(','$userIds) . ') and LOCATE(CONCAT(path, filename),cpath)=1  ORDER BY LENGTH(cpath) DESC LIMIT 1)=1
  1846.                                                     OR
  1847.                                                     (select list from users_workspaces_asset where userId in (' implode(','$userIds) . ') and LOCATE(cpath,CONCAT(path, filename))=1  ORDER BY LENGTH(cpath) DESC LIMIT 1)=1
  1848.                                                  )';
  1849.                 }
  1850.                 $condition implode(' AND '$conditionFilters);
  1851.                 $assetList = new Asset\Listing();
  1852.                 $assetList->setCondition($condition);
  1853.                 $assetList->setOrderKey('LENGTH(path) ASC, id ASC'false);
  1854.                 $assetList->setOffset((int)$request->get('offset'));
  1855.                 $assetList->setLimit((int)$request->get('limit'));
  1856.                 foreach ($assetList as $a) {
  1857.                     if ($a->isAllowed('view')) {
  1858.                         if (!$a instanceof Asset\Folder) {
  1859.                             // add the file with the relative path to the parent directory
  1860.                             $zip->addFile($a->getLocalFile(), preg_replace('@^' preg_quote($asset->getRealPath(), '@') . '@i'''$a->getRealFullPath()));
  1861.                         }
  1862.                     }
  1863.                 }
  1864.                 $zip->close();
  1865.                 $success true;
  1866.             }
  1867.         }
  1868.         return $this->adminJson([
  1869.             'success' => $success,
  1870.         ]);
  1871.     }
  1872.     /**
  1873.      * @Route("/download-as-zip", name="pimcore_admin_asset_downloadaszip", methods={"GET"})
  1874.      *
  1875.      * @param Request $request
  1876.      *
  1877.      * @return BinaryFileResponse
  1878.      * Download all assets contained in the folder with parameter id as ZIP file.
  1879.      * The suggested filename is either [folder name].zip or assets.zip for the root folder.
  1880.      */
  1881.     public function downloadAsZipAction(Request $request)
  1882.     {
  1883.         $asset Asset::getById((int) $request->get('id'));
  1884.         if (!$asset) {
  1885.             throw $this->createNotFoundException('Asset not found');
  1886.         }
  1887.         $zipFile PIMCORE_SYSTEM_TEMP_DIRECTORY '/download-zip-' $request->get('jobId') . '.zip';
  1888.         $suggestedFilename $asset->getFilename();
  1889.         if (empty($suggestedFilename)) {
  1890.             $suggestedFilename 'assets';
  1891.         }
  1892.         $response = new BinaryFileResponse($zipFile);
  1893.         $response->headers->set('Content-Type''application/zip');
  1894.         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT$suggestedFilename '.zip');
  1895.         $response->deleteFileAfterSend(true);
  1896.         return $response;
  1897.     }
  1898.     /**
  1899.      * @Route("/import-zip", name="pimcore_admin_asset_importzip", methods={"POST"})
  1900.      *
  1901.      * @param Request $request
  1902.      *
  1903.      * @return Response
  1904.      */
  1905.     public function importZipAction(Request $request)
  1906.     {
  1907.         $jobId uniqid();
  1908.         $filesPerJob 5;
  1909.         $jobs = [];
  1910.         $asset Asset::getById((int) $request->get('parentId'));
  1911.         if (!is_file($_FILES['Filedata']['tmp_name'])) {
  1912.             return $this->adminJson([
  1913.                 'success' => false,
  1914.                 'message' => 'Something went wrong, please check upload_max_filesize and post_max_size in your php.ini as well as the write permissions on the file system',
  1915.             ]);
  1916.         }
  1917.         if (!$asset) {
  1918.             throw $this->createNotFoundException('Parent asset not found');
  1919.         }
  1920.         if (!$asset->isAllowed('create')) {
  1921.             throw $this->createAccessDeniedException('not allowed to create');
  1922.         }
  1923.         $zipFile PIMCORE_SYSTEM_TEMP_DIRECTORY '/' $jobId '.zip';
  1924.         copy($_FILES['Filedata']['tmp_name'], $zipFile);
  1925.         $zip = new \ZipArchive;
  1926.         $retCode $zip->open($zipFile);
  1927.         if ($retCode === true) {
  1928.             $jobAmount ceil($zip->numFiles $filesPerJob);
  1929.             for ($i 0$i $jobAmount$i++) {
  1930.                 $jobs[] = [[
  1931.                     'url' => $this->generateUrl('pimcore_admin_asset_importzipfiles'),
  1932.                     'method' => 'POST',
  1933.                     'params' => [
  1934.                         'parentId' => $asset->getId(),
  1935.                         'offset' => $i $filesPerJob,
  1936.                         'limit' => $filesPerJob,
  1937.                         'jobId' => $jobId,
  1938.                         'last' => (($i 1) >= $jobAmount) ? 'true' '',
  1939.                     ],
  1940.                 ]];
  1941.             }
  1942.             $zip->close();
  1943.             // here we have to use this method and not the JSON action helper ($this->_helper->json()) because this will add
  1944.             // Content-Type: application/json which fires a download window in most browsers, because this is a normal POST
  1945.             // request and not XHR where the content-type doesn't matter
  1946.             $responseJson $this->encodeJson([
  1947.                 'success' => true,
  1948.                 'jobs' => $jobs,
  1949.                 'jobId' => $jobId,
  1950.             ]);
  1951.             return new Response($responseJson);
  1952.         } else {
  1953.             return $this->adminJson([
  1954.                 'success' => false,
  1955.                 'message' => $this->trans('could_not_open_zip_file'),
  1956.             ]);
  1957.         }
  1958.     }
  1959.     /**
  1960.      * @Route("/import-zip-files", name="pimcore_admin_asset_importzipfiles", methods={"POST"})
  1961.      *
  1962.      * @param Request $request
  1963.      *
  1964.      * @return JsonResponse
  1965.      */
  1966.     public function importZipFilesAction(Request $request)
  1967.     {
  1968.         $jobId $request->get('jobId');
  1969.         $limit = (int)$request->get('limit');
  1970.         $offset = (int)$request->get('offset');
  1971.         $importAsset Asset::getById((int) $request->get('parentId'));
  1972.         $zipFile PIMCORE_SYSTEM_TEMP_DIRECTORY '/' $jobId '.zip';
  1973.         $tmpDir PIMCORE_SYSTEM_TEMP_DIRECTORY '/zip-import';
  1974.         if (!is_dir($tmpDir)) {
  1975.             File::mkdir($tmpDir0777true);
  1976.         }
  1977.         $zip = new \ZipArchive;
  1978.         if ($zip->open($zipFile) === true) {
  1979.             for ($i $offset$i < ($offset $limit); $i++) {
  1980.                 $path $zip->getNameIndex($i);
  1981.                 if (str_starts_with($path'__MACOSX/')) {
  1982.                     continue;
  1983.                 }
  1984.                 if ($path !== false) {
  1985.                     if ($zip->extractTo($tmpDir '/'$path)) {
  1986.                         $tmpFile $tmpDir '/' preg_replace('@^/@'''$path);
  1987.                         $filename Element\Service::getValidKey(basename($path), 'asset');
  1988.                         $relativePath '';
  1989.                         if (dirname($path) != '.') {
  1990.                             $relativePath dirname($path);
  1991.                         }
  1992.                         $parentPath $importAsset->getRealFullPath() . '/' preg_replace('@^/@'''$relativePath);
  1993.                         $parent Asset\Service::createFolderByPath($parentPath);
  1994.                         // check for duplicate filename
  1995.                         $filename $this->getSafeFilename($parent->getRealFullPath(), $filename);
  1996.                         if ($parent->isAllowed('create')) {
  1997.                             $asset Asset::create($parent->getId(), [
  1998.                                 'filename' => $filename,
  1999.                                 'sourcePath' => $tmpFile,
  2000.                                 'userOwner' => $this->getAdminUser()->getId(),
  2001.                                 'userModification' => $this->getAdminUser()->getId(),
  2002.                             ]);
  2003.                             @unlink($tmpFile);
  2004.                         } else {
  2005.                             Logger::debug('prevented creating asset because of missing permissions');
  2006.                         }
  2007.                     }
  2008.                 }
  2009.             }
  2010.             $zip->close();
  2011.         }
  2012.         if ($request->get('last')) {
  2013.             unlink($zipFile);
  2014.         }
  2015.         return $this->adminJson([
  2016.             'success' => true,
  2017.         ]);
  2018.     }
  2019.     /**
  2020.      * @Route("/import-server", name="pimcore_admin_asset_importserver", methods={"POST"})
  2021.      *
  2022.      * @param Request $request
  2023.      *
  2024.      * @return JsonResponse
  2025.      */
  2026.     public function importServerAction(Request $request)
  2027.     {
  2028.         $success true;
  2029.         $filesPerJob 5;
  2030.         $jobs = [];
  2031.         $importDirectory str_replace('/fileexplorer'PIMCORE_PROJECT_ROOT$request->get('serverPath'));
  2032.         if (preg_match('@^' preg_quote(PIMCORE_PROJECT_ROOT'@') . '@'$importDirectory) && is_dir($importDirectory)) {
  2033.             $this->checkForPharStreamWrapper($importDirectory);
  2034.             $files rscandir($importDirectory '/');
  2035.             $count count($files);
  2036.             $jobFiles = [];
  2037.             for ($i 0$i $count$i++) {
  2038.                 if (is_dir($files[$i])) {
  2039.                     continue;
  2040.                 }
  2041.                 $jobFiles[] = preg_replace('@^' preg_quote($importDirectory'@') . '@'''$files[$i]);
  2042.                 if (count($jobFiles) >= $filesPerJob || $i >= ($count 1)) {
  2043.                     $relativeImportDirectory preg_replace('@^' preg_quote(PIMCORE_PROJECT_ROOT'@') . '@'''$importDirectory);
  2044.                     $jobs[] = [[
  2045.                         'url' => $this->generateUrl('pimcore_admin_asset_importserverfiles'),
  2046.                         'method' => 'POST',
  2047.                         'params' => [
  2048.                             'parentId' => $request->get('parentId'),
  2049.                             'serverPath' => $relativeImportDirectory,
  2050.                             'files' => implode('::'$jobFiles),
  2051.                         ],
  2052.                     ]];
  2053.                     $jobFiles = [];
  2054.                 }
  2055.             }
  2056.         }
  2057.         return $this->adminJson([
  2058.             'success' => $success,
  2059.             'jobs' => $jobs,
  2060.         ]);
  2061.     }
  2062.     /**
  2063.      * @Route("/import-server-files", name="pimcore_admin_asset_importserverfiles", methods={"POST"})
  2064.      *
  2065.      * @param Request $request
  2066.      *
  2067.      * @return JsonResponse
  2068.      */
  2069.     public function importServerFilesAction(Request $request)
  2070.     {
  2071.         $assetFolder Asset::getById((int) $request->get('parentId'));
  2072.         if (!$assetFolder) {
  2073.             throw $this->createNotFoundException('Parent asset not found');
  2074.         }
  2075.         $serverPath PIMCORE_PROJECT_ROOT $request->get('serverPath');
  2076.         $files explode('::'$request->get('files'));
  2077.         foreach ($files as $file) {
  2078.             $absolutePath $serverPath $file;
  2079.             $this->checkForPharStreamWrapper($absolutePath);
  2080.             if (is_file($absolutePath)) {
  2081.                 $relFolderPath str_replace('\\''/'dirname($file));
  2082.                 $folder Asset\Service::createFolderByPath($assetFolder->getRealFullPath() . $relFolderPath);
  2083.                 $filename basename($file);
  2084.                 // check for duplicate filename
  2085.                 $filename Element\Service::getValidKey($filename'asset');
  2086.                 $filename $this->getSafeFilename($folder->getRealFullPath(), $filename);
  2087.                 if ($assetFolder->isAllowed('create')) {
  2088.                     $asset Asset::create($folder->getId(), [
  2089.                         'filename' => $filename,
  2090.                         'sourcePath' => $absolutePath,
  2091.                         'userOwner' => $this->getAdminUser()->getId(),
  2092.                         'userModification' => $this->getAdminUser()->getId(),
  2093.                     ]);
  2094.                 } else {
  2095.                     Logger::debug('prevented creating asset because of missing permissions ');
  2096.                 }
  2097.             }
  2098.         }
  2099.         return $this->adminJson([
  2100.             'success' => true,
  2101.         ]);
  2102.     }
  2103.     protected function checkForPharStreamWrapper($path)
  2104.     {
  2105.         if (stripos($path'phar://') !== false) {
  2106.             throw $this->createAccessDeniedException('Using PHAR files is not allowed!');
  2107.         }
  2108.     }
  2109.     /**
  2110.      * @Route("/import-url", name="pimcore_admin_asset_importurl", methods={"POST"})
  2111.      *
  2112.      * @param Request $request
  2113.      *
  2114.      * @return JsonResponse
  2115.      *
  2116.      * @throws \Exception
  2117.      */
  2118.     public function importUrlAction(Request $request)
  2119.     {
  2120.         $success true;
  2121.         $data Tool::getHttpData($request->get('url'));
  2122.         $filename basename($request->get('url'));
  2123.         $parentId $request->get('id');
  2124.         $parentAsset Asset::getById((int)$parentId);
  2125.         if (!$parentAsset) {
  2126.             throw $this->createNotFoundException('Parent asset not found');
  2127.         }
  2128.         $filename Element\Service::getValidKey($filename'asset');
  2129.         $filename $this->getSafeFilename($parentAsset->getRealFullPath(), $filename);
  2130.         if (empty($filename)) {
  2131.             throw new \Exception('The filename of the asset is empty');
  2132.         }
  2133.         // check for duplicate filename
  2134.         $filename $this->getSafeFilename($parentAsset->getRealFullPath(), $filename);
  2135.         if ($parentAsset->isAllowed('create')) {
  2136.             $asset Asset::create($parentId, [
  2137.                 'filename' => $filename,
  2138.                 'data' => $data,
  2139.                 'userOwner' => $this->getAdminUser()->getId(),
  2140.                 'userModification' => $this->getAdminUser()->getId(),
  2141.             ]);
  2142.             $success true;
  2143.         } else {
  2144.             Logger::debug('prevented creating asset because of missing permissions');
  2145.         }
  2146.         return $this->adminJson(['success' => $success]);
  2147.     }
  2148.     /**
  2149.      * @Route("/clear-thumbnail", name="pimcore_admin_asset_clearthumbnail", methods={"POST"})
  2150.      *
  2151.      * @param Request $request
  2152.      *
  2153.      * @return JsonResponse
  2154.      */
  2155.     public function clearThumbnailAction(Request $request)
  2156.     {
  2157.         $success false;
  2158.         if ($asset Asset::getById((int) $request->get('id'))) {
  2159.             if (method_exists($asset'clearThumbnails')) {
  2160.                 if (!$asset->isAllowed('publish')) {
  2161.                     throw $this->createAccessDeniedException('not allowed to publish');
  2162.                 }
  2163.                 $asset->clearThumbnails(true); // force clear
  2164.                 $asset->save();
  2165.                 $success true;
  2166.             }
  2167.         }
  2168.         return $this->adminJson(['success' => $success]);
  2169.     }
  2170.     /**
  2171.      * @Route("/grid-proxy", name="pimcore_admin_asset_gridproxy", methods={"GET", "POST", "PUT"})
  2172.      *
  2173.      * @param Request $request
  2174.      * @param EventDispatcherInterface $eventDispatcher
  2175.      * @param GridHelperService $gridHelperService
  2176.      * @param CsrfProtectionHandler $csrfProtection
  2177.      *
  2178.      * @return JsonResponse
  2179.      */
  2180.     public function gridProxyAction(Request $requestEventDispatcherInterface $eventDispatcherGridHelperService $gridHelperServiceCsrfProtectionHandler $csrfProtection)
  2181.     {
  2182.         $allParams array_merge($request->request->all(), $request->query->all());
  2183.         $filterPrepareEvent = new GenericEvent($this, [
  2184.             'requestParams' => $allParams,
  2185.         ]);
  2186.         $language $request->get('language') != 'default' $request->get('language') : null;
  2187.         $eventDispatcher->dispatch($filterPrepareEventAdminEvents::ASSET_LIST_BEFORE_FILTER_PREPARE);
  2188.         $allParams $filterPrepareEvent->getArgument('requestParams');
  2189.         $loader \Pimcore::getContainer()->get('pimcore.implementation_loader.asset.metadata.data');
  2190.         if (isset($allParams['data']) && $allParams['data']) {
  2191.             $csrfProtection->checkCsrfToken($request);
  2192.             if ($allParams['xaction'] == 'update') {
  2193.                 try {
  2194.                     $data $this->decodeJson($allParams['data']);
  2195.                     $updateEvent = new GenericEvent($this, [
  2196.                         'data' => $data,
  2197.                         'processed' => false,
  2198.                     ]);
  2199.                     $eventDispatcher->dispatch($updateEventAdminEvents::ASSET_LIST_BEFORE_UPDATE);
  2200.                     $processed $updateEvent->getArgument('processed');
  2201.                     if ($processed) {
  2202.                         // update already processed by event handler
  2203.                         return $this->adminJson(['success' => true]);
  2204.                     }
  2205.                     $data $updateEvent->getArgument('data');
  2206.                     // save
  2207.                     $asset Asset::getById($data['id']);
  2208.                     if (!$asset) {
  2209.                         throw $this->createNotFoundException('Asset not found');
  2210.                     }
  2211.                     if (!$asset->isAllowed('publish')) {
  2212.                         throw $this->createAccessDeniedException("Permission denied. You don't have the rights to save this asset.");
  2213.                     }
  2214.                     $metadata $asset->getMetadata(nullnullfalsetrue);
  2215.                     $dirty false;
  2216.                     unset($data['id']);
  2217.                     foreach ($data as $key => $value) {
  2218.                         $fieldDef explode('~'$key);
  2219.                         $key $fieldDef[0];
  2220.                         if (isset($fieldDef[1])) {
  2221.                             $language = ($fieldDef[1] == 'none' '' $fieldDef[1]);
  2222.                         }
  2223.                         foreach ($metadata as $idx => &$em) {
  2224.                             if ($em['name'] == $key && $em['language'] == $language) {
  2225.                                 try {
  2226.                                     $dataImpl $loader->build($em['type']);
  2227.                                     $value $dataImpl->getDataFromListfolderGrid($value$em);
  2228.                                 } catch (UnsupportedException $le) {
  2229.                                     Logger::error('could not resolve metadata implementation for ' $em['type']);
  2230.                                 }
  2231.                                 $em['data'] = $value;
  2232.                                 $dirty true;
  2233.                                 break;
  2234.                             }
  2235.                         }
  2236.                         if (!$dirty) {
  2237.                             $defaulMetadata = ['title''alt''copyright'];
  2238.                             if (in_array($key$defaulMetadata)) {
  2239.                                 $newEm = [
  2240.                                     'name' => $key,
  2241.                                     'language' => $language,
  2242.                                     'type' => 'input',
  2243.                                     'data' => $value,
  2244.                                 ];
  2245.                                 try {
  2246.                                     $dataImpl $loader->build($newEm['type']);
  2247.                                     $newEm['data'] = $dataImpl->getDataFromListfolderGrid($value$newEm);
  2248.                                 } catch (UnsupportedException $le) {
  2249.                                     Logger::error('could not resolve metadata implementation for ' $newEm['type']);
  2250.                                 }
  2251.                                 $metadata[] = $newEm;
  2252.                                 $dirty true;
  2253.                             } else {
  2254.                                 $predefined Model\Metadata\Predefined::getByName($key);
  2255.                                 if ($predefined && (empty($predefined->getTargetSubtype())
  2256.                                         || $predefined->getTargetSubtype() == $asset->getType())) {
  2257.                                     $newEm = [
  2258.                                         'name' => $key,
  2259.                                         'language' => $language,
  2260.                                         'type' => $predefined->getType(),
  2261.                                         'data' => $value,
  2262.                                     ];
  2263.                                     try {
  2264.                                         $dataImpl $loader->build($newEm['type']);
  2265.                                         $newEm['data'] = $dataImpl->getDataFromListfolderGrid($value$newEm);
  2266.                                     } catch (UnsupportedException $le) {
  2267.                                         Logger::error('could not resolve metadata implementation for ' $newEm['type']);
  2268.                                     }
  2269.                                     $metadata[] = $newEm;
  2270.                                     $dirty true;
  2271.                                 }
  2272.                             }
  2273.                         }
  2274.                     }
  2275.                     if ($dirty) {
  2276.                         // $metadata = Asset\Service::minimizeMetadata($metadata, "grid");
  2277.                         $asset->setMetadataRaw($metadata);
  2278.                         $asset->save();
  2279.                         return $this->adminJson(['success' => true]);
  2280.                     }
  2281.                     return $this->adminJson(['success' => false'message' => 'something went wrong.']);
  2282.                 } catch (\Exception $e) {
  2283.                     return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  2284.                 }
  2285.             }
  2286.         } else {
  2287.             $list $gridHelperService->prepareAssetListingForGrid($allParams$this->getAdminUser());
  2288.             $beforeListLoadEvent = new GenericEvent($this, [
  2289.                 'list' => $list,
  2290.                 'context' => $allParams,
  2291.             ]);
  2292.             $eventDispatcher->dispatch($beforeListLoadEventAdminEvents::ASSET_LIST_BEFORE_LIST_LOAD);
  2293.             /** @var Asset\Listing $list */
  2294.             $list $beforeListLoadEvent->getArgument('list');
  2295.             $list->load();
  2296.             $assets = [];
  2297.             foreach ($list->getAssets() as $index => $asset) {
  2298.                 // Like for treeGetChildsByIdAction, so we respect isAllowed method which can be extended (object DI) for custom permissions, so relying only users_workspaces_asset is insufficient and could lead security breach
  2299.                 if ($asset->isAllowed('list')) {
  2300.                     $a Asset\Service::gridAssetData($asset$allParams['fields'], $allParams['language'] ?? '');
  2301.                     $assets[] = $a;
  2302.                 }
  2303.             }
  2304.             $result = ['data' => $assets'success' => true'total' => $list->getTotalCount()];
  2305.             $afterListLoadEvent = new GenericEvent($this, [
  2306.                 'list' => $result,
  2307.                 'context' => $allParams,
  2308.             ]);
  2309.             $eventDispatcher->dispatch($afterListLoadEventAdminEvents::ASSET_LIST_AFTER_LIST_LOAD);
  2310.             $result $afterListLoadEvent->getArgument('list');
  2311.             return $this->adminJson($result);
  2312.         }
  2313.         return $this->adminJson(['success' => false]);
  2314.     }
  2315.     /**
  2316.      * @Route("/get-text", name="pimcore_admin_asset_gettext", methods={"GET"})
  2317.      *
  2318.      * @param Request $request
  2319.      *
  2320.      * @return JsonResponse
  2321.      */
  2322.     public function getTextAction(Request $request)
  2323.     {
  2324.         $asset Asset::getById((int) $request->get('id'));
  2325.         if (!$asset) {
  2326.             throw $this->createNotFoundException('Asset not found');
  2327.         }
  2328.         if (!$asset->isAllowed('view')) {
  2329.             throw $this->createAccessDeniedException('not allowed to view');
  2330.         }
  2331.         $page $request->get('page');
  2332.         $text null;
  2333.         if ($asset instanceof Asset\Document) {
  2334.             $text $asset->getText($page);
  2335.         }
  2336.         return $this->adminJson(['success' => 'true''text' => $text]);
  2337.     }
  2338.     /**
  2339.      * @Route("/detect-image-features", name="pimcore_admin_asset_detectimagefeatures", methods={"GET"})
  2340.      *
  2341.      * @param Request $request
  2342.      *
  2343.      * @return JsonResponse
  2344.      */
  2345.     public function detectImageFeaturesAction(Request $request)
  2346.     {
  2347.         $asset Asset\Image::getById((int)$request->get('id'));
  2348.         if (!$asset instanceof Asset) {
  2349.             return $this->adminJson(['success' => false'message' => "asset doesn't exist"]);
  2350.         }
  2351.         if ($asset->isAllowed('publish')) {
  2352.             $asset->detectFaces();
  2353.             $asset->removeCustomSetting('disableImageFeatureAutoDetection');
  2354.             $asset->save();
  2355.             return $this->adminJson(['success' => true]);
  2356.         }
  2357.         throw $this->createAccessDeniedHttpException();
  2358.     }
  2359.     /**
  2360.      * @Route("/delete-image-features", name="pimcore_admin_asset_deleteimagefeatures", methods={"GET"})
  2361.      *
  2362.      * @param Request $request
  2363.      *
  2364.      * @return JsonResponse
  2365.      */
  2366.     public function deleteImageFeaturesAction(Request $request)
  2367.     {
  2368.         $asset Asset::getById((int)$request->get('id'));
  2369.         if (!$asset instanceof Asset) {
  2370.             return $this->adminJson(['success' => false'message' => "asset doesn't exist"]);
  2371.         }
  2372.         if ($asset->isAllowed('publish')) {
  2373.             $asset->removeCustomSetting('faceCoordinates');
  2374.             $asset->setCustomSetting('disableImageFeatureAutoDetection'true);
  2375.             $asset->save();
  2376.             return $this->adminJson(['success' => true]);
  2377.         }
  2378.         throw $this->createAccessDeniedHttpException();
  2379.     }
  2380.     /**
  2381.      * @param ControllerEvent $event
  2382.      */
  2383.     public function onKernelControllerEvent(ControllerEvent $event)
  2384.     {
  2385.         if (!$event->isMainRequest()) {
  2386.             return;
  2387.         }
  2388.         $this->checkActionPermission($event'assets', [
  2389.             'getImageThumbnailAction''getVideoThumbnailAction''getDocumentThumbnailAction',
  2390.         ]);
  2391.         $this->_assetService = new Asset\Service($this->getAdminUser());
  2392.     }
  2393.     /**
  2394.      * @throws ValidationException
  2395.      */
  2396.     private function validateManyToManyRelationAssetType(array $contextstring $filenamestring $sourcePath): void
  2397.     {
  2398.         if (isset($context['containerType'], $context['objectId'], $context['fieldname'])
  2399.             && 'object' === $context['containerType']
  2400.             && $object Concrete::getById($context['objectId'])
  2401.         ) {
  2402.             $fieldDefinition $object->getClass()?->getFieldDefinition($context['fieldname']);
  2403.             if (!$fieldDefinition instanceof ManyToManyRelation) {
  2404.                 return;
  2405.             }
  2406.             $mimeType MimeTypes::getDefault()->guessMimeType($sourcePath);
  2407.             $type Asset::getTypeFromMimeMapping($mimeType$filename);
  2408.             $allowedAssetTypes $fieldDefinition->getAssetTypes();
  2409.             $allowedAssetTypes array_column($allowedAssetTypes'assetTypes');
  2410.             if (
  2411.                 !(
  2412.                     $fieldDefinition->getAssetsAllowed()
  2413.                     && ($allowedAssetTypes === [] || in_array($type$allowedAssetTypestrue))
  2414.                 )
  2415.             ) {
  2416.                 throw new ValidationException(sprintf('Invalid relation in field `%s` [type: %s]'$context['fieldname'], $type));
  2417.             }
  2418.         }
  2419.     }
  2420. }