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

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