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

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.             ],
  641.         ];
  642.         $hasChildren $asset->getDao()->hasChildren($this->getAdminUser());
  643.         // set type specific settings
  644.         if ($asset instanceof Asset\Folder) {
  645.             $tmpAsset['leaf'] = false;
  646.             $tmpAsset['expanded'] = !$hasChildren;
  647.             $tmpAsset['loaded'] = !$hasChildren;
  648.             $tmpAsset['permissions']['create'] = $permissions['create'];
  649.             $tmpAsset['thumbnail'] = $this->getThumbnailUrl($asset, ['origin' => 'treeNode']);
  650.         } else {
  651.             $tmpAsset['leaf'] = true;
  652.             $tmpAsset['expandable'] = false;
  653.             $tmpAsset['expanded'] = false;
  654.         }
  655.         $this->addAdminStyle($assetElementAdminStyleEvent::CONTEXT_TREE$tmpAsset);
  656.         if ($asset instanceof Asset\Image) {
  657.             try {
  658.                 $tmpAsset['thumbnail'] = $this->getThumbnailUrl($asset, ['origin' => 'treeNode']);
  659.                 // we need the dimensions for the wysiwyg editors, so that they can resize the image immediately
  660.                 if ($asset->getCustomSetting('imageDimensionsCalculated')) {
  661.                     $tmpAsset['imageWidth'] = $asset->getCustomSetting('imageWidth');
  662.                     $tmpAsset['imageHeight'] = $asset->getCustomSetting('imageHeight');
  663.                 }
  664.             } catch (\Exception $e) {
  665.                 Logger::debug('Cannot get dimensions of image, seems to be broken.');
  666.             }
  667.         } elseif ($asset->getType() == 'video') {
  668.             try {
  669.                 if (\Pimcore\Video::isAvailable()) {
  670.                     $tmpAsset['thumbnail'] = $this->getThumbnailUrl($asset, ['origin' => 'treeNode']);
  671.                 }
  672.             } catch (\Exception $e) {
  673.                 Logger::debug('Cannot get dimensions of video, seems to be broken.');
  674.             }
  675.         } elseif ($asset->getType() == 'document') {
  676.             try {
  677.                 // add the PDF check here, otherwise the preview layer in admin is shown without content
  678.                 if (\Pimcore\Document::isAvailable() && \Pimcore\Document::isFileTypeSupported($asset->getFilename())) {
  679.                     $tmpAsset['thumbnail'] = $this->getThumbnailUrl($asset, ['origin' => 'treeNode']);
  680.                 }
  681.             } catch (\Exception $e) {
  682.                 Logger::debug('Cannot get dimensions of video, seems to be broken.');
  683.             }
  684.         }
  685.         $tmpAsset['cls'] = '';
  686.         if ($asset->isLocked()) {
  687.             $tmpAsset['cls'] .= 'pimcore_treenode_locked ';
  688.         }
  689.         if ($asset->getLocked()) {
  690.             $tmpAsset['cls'] .= 'pimcore_treenode_lockOwner ';
  691.         }
  692.         return $tmpAsset;
  693.     }
  694.     /**
  695.      * @param Asset $asset
  696.      * @param array $params
  697.      *
  698.      * @return null|string
  699.      */
  700.     protected function getThumbnailUrl(Asset $asset, array $params = [])
  701.     {
  702.         $defaults = [
  703.             'id' => $asset->getId(),
  704.             'treepreview' => true,
  705.             '_dc' => $asset->getModificationDate(),
  706.         ];
  707.         $params array_merge($defaults$params);
  708.         if ($asset instanceof Asset\Image) {
  709.             return $this->generateUrl('pimcore_admin_asset_getimagethumbnail'$params);
  710.         }
  711.         if ($asset instanceof Asset\Folder) {
  712.             return $this->generateUrl('pimcore_admin_asset_getfolderthumbnail'$params);
  713.         }
  714.         if ($asset instanceof Asset\Video && \Pimcore\Video::isAvailable()) {
  715.             return $this->generateUrl('pimcore_admin_asset_getvideothumbnail'$params);
  716.         }
  717.         if ($asset instanceof Asset\Document && \Pimcore\Document::isAvailable() && $asset->getPageCount()) {
  718.             return $this->generateUrl('pimcore_admin_asset_getdocumentthumbnail'$params);
  719.         }
  720.         return null;
  721.     }
  722.     /**
  723.      * @Route("/update", name="pimcore_admin_asset_update", methods={"PUT"})
  724.      *
  725.      * @param Request $request
  726.      *
  727.      * @return JsonResponse
  728.      *
  729.      * @throws \Exception
  730.      */
  731.     public function updateAction(Request $request)
  732.     {
  733.         $success false;
  734.         $allowUpdate true;
  735.         $updateData array_merge($request->request->all(), $request->query->all());
  736.         $asset Asset::getById((int) $request->get('id'));
  737.         if ($asset->isAllowed('settings')) {
  738.             $asset->setUserModification($this->getAdminUser()->getId());
  739.             // if the position is changed the path must be changed || also from the children
  740.             if ($parentId $request->get('parentId')) {
  741.                 $parentAsset Asset::getById((int) $parentId);
  742.                 //check if parent is changed i.e. asset is moved
  743.                 if ($asset->getParentId() != $parentAsset->getId()) {
  744.                     if (!$parentAsset->isAllowed('create')) {
  745.                         throw new \Exception('Prevented moving asset - no create permission on new parent ');
  746.                     }
  747.                     $intendedPath $parentAsset->getRealPath();
  748.                     $pKey $parentAsset->getKey();
  749.                     if (!empty($pKey)) {
  750.                         $intendedPath .= $parentAsset->getKey() . '/';
  751.                     }
  752.                     $assetWithSamePath Asset::getByPath($intendedPath $asset->getKey());
  753.                     if ($assetWithSamePath != null) {
  754.                         $allowUpdate false;
  755.                     }
  756.                     if ($asset->isLocked()) {
  757.                         $allowUpdate false;
  758.                     }
  759.                 }
  760.             }
  761.             if ($allowUpdate) {
  762.                 if ($request->get('filename') != $asset->getFilename() && !$asset->isAllowed('rename')) {
  763.                     unset($updateData['filename']);
  764.                     Logger::debug('prevented renaming asset because of missing permissions ');
  765.                 }
  766.                 $asset->setValues($updateData);
  767.                 try {
  768.                     $asset->save();
  769.                     $success true;
  770.                 } catch (\Exception $e) {
  771.                     return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  772.                 }
  773.             } else {
  774.                 $msg 'prevented moving asset, asset with same path+key already exists at target location or the asset is locked. ID: ' $asset->getId();
  775.                 Logger::debug($msg);
  776.                 return $this->adminJson(['success' => $success'message' => $msg]);
  777.             }
  778.         } elseif ($asset->isAllowed('rename') && $request->get('filename')) {
  779.             //just rename
  780.             try {
  781.                 $asset->setFilename($request->get('filename'));
  782.                 $asset->save();
  783.                 $success true;
  784.             } catch (\Exception $e) {
  785.                 return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  786.             }
  787.         } else {
  788.             Logger::debug('prevented update asset because of missing permissions ');
  789.         }
  790.         return $this->adminJson(['success' => $success]);
  791.     }
  792.     /**
  793.      * @Route("/webdav{path}", name="pimcore_admin_webdav", requirements={"path"=".*"})
  794.      */
  795.     public function webdavAction()
  796.     {
  797.         $homeDir Asset::getById(1);
  798.         try {
  799.             $publicDir = new Asset\WebDAV\Folder($homeDir);
  800.             $objectTree = new Asset\WebDAV\Tree($publicDir);
  801.             $server = new \Sabre\DAV\Server($objectTree);
  802.             $server->setBaseUri($this->generateUrl('pimcore_admin_webdav', ['path' => '/']));
  803.             // lock plugin
  804.             /** @var \Doctrine\DBAL\Driver\PDOConnection $pdo */
  805.             $pdo \Pimcore\Db::get()->getWrappedConnection();
  806.             $lockBackend = new \Sabre\DAV\Locks\Backend\PDO($pdo);
  807.             $lockBackend->tableName 'webdav_locks';
  808.             $lockPlugin = new \Sabre\DAV\Locks\Plugin($lockBackend);
  809.             $server->addPlugin($lockPlugin);
  810.             // browser plugin
  811.             $server->addPlugin(new \Sabre\DAV\Browser\Plugin());
  812.             $server->start();
  813.         } catch (\Exception $e) {
  814.             Logger::error((string) $e);
  815.         }
  816.         exit;
  817.     }
  818.     /**
  819.      * @Route("/save", name="pimcore_admin_asset_save", methods={"PUT","POST"})
  820.      *
  821.      * @param Request $request
  822.      * @param EventDispatcherInterface $eventDispatcher
  823.      *
  824.      * @return JsonResponse
  825.      *
  826.      * @throws \Exception
  827.      */
  828.     public function saveAction(Request $requestEventDispatcherInterface $eventDispatcher)
  829.     {
  830.         $asset Asset::getById((int) $request->get('id'));
  831.         if (!$asset) {
  832.             throw $this->createNotFoundException('Asset not found');
  833.         }
  834.         if ($asset->isAllowed('publish')) {
  835.             // metadata
  836.             if ($request->get('metadata')) {
  837.                 $metadata $this->decodeJson($request->get('metadata'));
  838.                 $metadataEvent = new GenericEvent($this, [
  839.                     'id' => $asset->getId(),
  840.                     'metadata' => $metadata,
  841.                 ]);
  842.                 $eventDispatcher->dispatch($metadataEventAdminEvents::ASSET_METADATA_PRE_SET);
  843.                 $metadata $metadataEvent->getArgument('metadata');
  844.                 $metadataValues $metadata['values'];
  845.                 $metadataValues Asset\Service::minimizeMetadata($metadataValues'editor');
  846.                 $asset->setMetadataRaw($metadataValues);
  847.             }
  848.             // properties
  849.             if ($request->get('properties')) {
  850.                 $properties = [];
  851.                 $propertiesData $this->decodeJson($request->get('properties'));
  852.                 if (is_array($propertiesData)) {
  853.                     foreach ($propertiesData as $propertyName => $propertyData) {
  854.                         $value $propertyData['data'];
  855.                         try {
  856.                             $property = new Model\Property();
  857.                             $property->setType($propertyData['type']);
  858.                             $property->setName($propertyName);
  859.                             $property->setCtype('asset');
  860.                             $property->setDataFromEditmode($value);
  861.                             $property->setInheritable($propertyData['inheritable']);
  862.                             $properties[$propertyName] = $property;
  863.                         } catch (\Exception $e) {
  864.                             Logger::err("Can't add " $propertyName ' to asset ' $asset->getRealFullPath());
  865.                         }
  866.                     }
  867.                     $asset->setProperties($properties);
  868.                 }
  869.             }
  870.             $this->applySchedulerDataToElement($request$asset);
  871.             if ($request->get('data')) {
  872.                 $asset->setData($request->get('data'));
  873.             }
  874.             // image specific data
  875.             if ($asset instanceof Asset\Image) {
  876.                 if ($request->get('image')) {
  877.                     $imageData $this->decodeJson($request->get('image'));
  878.                     if (isset($imageData['focalPoint'])) {
  879.                         $asset->setCustomSetting('focalPointX'$imageData['focalPoint']['x']);
  880.                         $asset->setCustomSetting('focalPointY'$imageData['focalPoint']['y']);
  881.                         $asset->removeCustomSetting('disableFocalPointDetection');
  882.                     }
  883.                 } else {
  884.                     // wipe all data
  885.                     $asset->removeCustomSetting('focalPointX');
  886.                     $asset->removeCustomSetting('focalPointY');
  887.                     $asset->setCustomSetting('disableFocalPointDetection'true);
  888.                 }
  889.             }
  890.             $asset->setUserModification($this->getAdminUser()->getId());
  891.             if ($request->get('task') === 'session') {
  892.                 // save to session only
  893.                 Asset\Service::saveElementToSession($asset);
  894.             } else {
  895.                 $asset->save();
  896.             }
  897.             $treeData $this->getTreeNodeConfig($asset);
  898.             return $this->adminJson([
  899.                 'success' => true,
  900.                 'data' => [
  901.                     'versionDate' => $asset->getModificationDate(),
  902.                     'versionCount' => $asset->getVersionCount(),
  903.                 ],
  904.                 'treeData' => $treeData,
  905.             ]);
  906.         } else {
  907.             throw $this->createAccessDeniedHttpException();
  908.         }
  909.     }
  910.     /**
  911.      * @Route("/publish-version", name="pimcore_admin_asset_publishversion", methods={"POST"})
  912.      *
  913.      * @param Request $request
  914.      *
  915.      * @return JsonResponse
  916.      */
  917.     public function publishVersionAction(Request $request)
  918.     {
  919.         $version Model\Version::getById((int) $request->get('id'));
  920.         $asset $version->loadData();
  921.         $currentAsset Asset::getById($asset->getId());
  922.         if ($currentAsset->isAllowed('publish')) {
  923.             try {
  924.                 $asset->setUserModification($this->getAdminUser()->getId());
  925.                 $asset->save();
  926.                 $treeData $this->getTreeNodeConfig($asset);
  927.                 return $this->adminJson(['success' => true'treeData' => $treeData]);
  928.             } catch (\Exception $e) {
  929.                 return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  930.             }
  931.         }
  932.         throw $this->createAccessDeniedHttpException();
  933.     }
  934.     /**
  935.      * @Route("/show-version", name="pimcore_admin_asset_showversion", methods={"GET"})
  936.      *
  937.      * @param Request $request
  938.      *
  939.      * @return Response
  940.      */
  941.     public function showVersionAction(Request $request)
  942.     {
  943.         $id = (int)$request->get('id');
  944.         $version Model\Version::getById($id);
  945.         if (!$version) {
  946.             throw $this->createNotFoundException('Version not found');
  947.         }
  948.         $asset $version->loadData();
  949.         if (!$asset->isAllowed('versions')) {
  950.             throw $this->createAccessDeniedHttpException('Permission denied, version id [' $id ']');
  951.         }
  952.         $loader \Pimcore::getContainer()->get('pimcore.implementation_loader.asset.metadata.data');
  953.         return $this->render(
  954.             '@PimcoreAdmin/Admin/Asset/showVersion' ucfirst($asset->getType()) . '.html.twig',
  955.             [
  956.                 'asset' => $asset,
  957.                 'loader' => $loader,
  958.             ]
  959.         );
  960.     }
  961.     /**
  962.      * @Route("/download", name="pimcore_admin_asset_download", methods={"GET"})
  963.      *
  964.      * @param Request $request
  965.      *
  966.      * @return StreamedResponse
  967.      */
  968.     public function downloadAction(Request $request)
  969.     {
  970.         $asset Asset::getById((int) $request->get('id'));
  971.         if (!$asset) {
  972.             throw $this->createNotFoundException('Asset not found');
  973.         }
  974.         if (!$asset->isAllowed('view')) {
  975.             throw $this->createAccessDeniedException('not allowed to view asset');
  976.         }
  977.         $stream $asset->getStream();
  978.         return new StreamedResponse(function () use ($stream) {
  979.             fpassthru($stream);
  980.         }, 200, [
  981.             'Content-Type' => $asset->getMimeType(),
  982.             'Content-Disposition' => sprintf('attachment; filename="%s"'$asset->getFilename()),
  983.             'Content-Length' => $asset->getFileSize(),
  984.         ]);
  985.     }
  986.     /**
  987.      * @Route("/download-image-thumbnail", name="pimcore_admin_asset_downloadimagethumbnail", methods={"GET"})
  988.      *
  989.      * @param Request $request
  990.      *
  991.      * @return BinaryFileResponse
  992.      */
  993.     public function downloadImageThumbnailAction(Request $request)
  994.     {
  995.         $image Asset\Image::getById((int) $request->get('id'));
  996.         if (!$image) {
  997.             throw $this->createNotFoundException('Asset not found');
  998.         }
  999.         if (!$image->isAllowed('view')) {
  1000.             throw $this->createAccessDeniedException('not allowed to view thumbnail');
  1001.         }
  1002.         $config null;
  1003.         $thumbnail null;
  1004.         $thumbnailName $request->get('thumbnail');
  1005.         $thumbnailFile null;
  1006.         $deleteThumbnail true;
  1007.         if ($request->get('config')) {
  1008.             $config $this->decodeJson($request->get('config'));
  1009.         } elseif ($request->get('type')) {
  1010.             $predefined = [
  1011.                 'web' => [
  1012.                     'resize_mode' => 'scaleByWidth',
  1013.                     'width' => 3500,
  1014.                     'dpi' => 72,
  1015.                     'format' => 'JPEG',
  1016.                     'quality' => 85,
  1017.                 ],
  1018.                 'print' => [
  1019.                     'resize_mode' => 'scaleByWidth',
  1020.                     'width' => 6000,
  1021.                     'dpi' => 300,
  1022.                     'format' => 'JPEG',
  1023.                     'quality' => 95,
  1024.                 ],
  1025.                 'office' => [
  1026.                     'resize_mode' => 'scaleByWidth',
  1027.                     'width' => 1190,
  1028.                     'dpi' => 144,
  1029.                     'format' => 'JPEG',
  1030.                     'quality' => 90,
  1031.                 ],
  1032.             ];
  1033.             $config $predefined[$request->get('type')];
  1034.         } elseif ($thumbnailName) {
  1035.             $thumbnail $image->getThumbnail($thumbnailName);
  1036.             $deleteThumbnail false;
  1037.         }
  1038.         if ($config) {
  1039.             $thumbnailConfig = new Asset\Image\Thumbnail\Config();
  1040.             $thumbnailConfig->setName('pimcore-download-' $image->getId() . '-' md5($request->get('config')));
  1041.             if ($config['resize_mode'] == 'scaleByWidth') {
  1042.                 $thumbnailConfig->addItem('scaleByWidth', [
  1043.                     'width' => $config['width'],
  1044.                 ]);
  1045.             } elseif ($config['resize_mode'] == 'scaleByHeight') {
  1046.                 $thumbnailConfig->addItem('scaleByHeight', [
  1047.                     'height' => $config['height'],
  1048.                 ]);
  1049.             } else {
  1050.                 $thumbnailConfig->addItem('resize', [
  1051.                     'width' => $config['width'],
  1052.                     'height' => $config['height'],
  1053.                 ]);
  1054.             }
  1055.             $thumbnailConfig->setQuality($config['quality']);
  1056.             $thumbnailConfig->setFormat($config['format']);
  1057.             $thumbnailConfig->setRasterizeSVG(true);
  1058.             if ($thumbnailConfig->getFormat() == 'JPEG') {
  1059.                 $thumbnailConfig->setPreserveMetaData(true);
  1060.                 if (empty($config['quality'])) {
  1061.                     $thumbnailConfig->setPreserveColor(true);
  1062.                 }
  1063.             }
  1064.             $thumbnail $image->getThumbnail($thumbnailConfig);
  1065.             $thumbnailFile $thumbnail->getLocalFile();
  1066.             $exiftool \Pimcore\Tool\Console::getExecutable('exiftool');
  1067.             if ($thumbnailConfig->getFormat() == 'JPEG' && $exiftool && isset($config['dpi']) && $config['dpi']) {
  1068.                 $process = new Process([$exiftool'-overwrite_original''-xresolution=' . (int)$config['dpi'], '-yresolution=' . (int)$config['dpi'], '-resolutionunit=inches'$thumbnailFile]);
  1069.                 $process->run();
  1070.             }
  1071.         }
  1072.         if ($thumbnail) {
  1073.             $thumbnailFile $thumbnailFile ?: $thumbnail->getLocalFile();
  1074.             $downloadFilename preg_replace(
  1075.                 '/\.' preg_quote(File::getFileExtension($image->getFilename())) . '$/i',
  1076.                 '.' $thumbnail->getFileExtension(),
  1077.                 $image->getFilename()
  1078.             );
  1079.             $downloadFilename strtolower($downloadFilename);
  1080.             clearstatcache();
  1081.             $response = new BinaryFileResponse($thumbnailFile);
  1082.             $response->headers->set('Content-Type'$thumbnail->getMimeType());
  1083.             $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT$downloadFilename);
  1084.             $this->addThumbnailCacheHeaders($response);
  1085.             $response->deleteFileAfterSend($deleteThumbnail);
  1086.             return $response;
  1087.         }
  1088.         throw $this->createNotFoundException('Thumbnail not found');
  1089.     }
  1090.     /**
  1091.      * @Route("/get-asset", name="pimcore_admin_asset_getasset", methods={"GET"})
  1092.      *
  1093.      * @param Request $request
  1094.      *
  1095.      * @return StreamedResponse
  1096.      */
  1097.     public function getAssetAction(Request $request)
  1098.     {
  1099.         $image Asset::getById((int)$request->get('id'));
  1100.         if (!$image) {
  1101.             throw $this->createNotFoundException('Asset not found');
  1102.         }
  1103.         if (!$image->isAllowed('view')) {
  1104.             throw $this->createAccessDeniedException('not allowed to view asset');
  1105.         }
  1106.         $stream $image->getStream();
  1107.         $response = new StreamedResponse(function () use ($stream) {
  1108.             fpassthru($stream);
  1109.         }, 200, [
  1110.             'Content-Type' => $image->getMimeType(),
  1111.             'Access-Control-Allow-Origin' => '*',
  1112.         ]);
  1113.         $this->addThumbnailCacheHeaders($response);
  1114.         return $response;
  1115.     }
  1116.     /**
  1117.      * @Route("/get-image-thumbnail", name="pimcore_admin_asset_getimagethumbnail", methods={"GET"})
  1118.      *
  1119.      * @param Request $request
  1120.      *
  1121.      * @return StreamedResponse|JsonResponse|BinaryFileResponse
  1122.      */
  1123.     public function getImageThumbnailAction(Request $request)
  1124.     {
  1125.         $fileinfo $request->get('fileinfo');
  1126.         $image Asset\Image::getById((int)$request->get('id'));
  1127.         if (!$image) {
  1128.             throw $this->createNotFoundException('Asset not found');
  1129.         }
  1130.         if (!$image->isAllowed('view')) {
  1131.             throw $this->createAccessDeniedException('not allowed to view thumbnail');
  1132.         }
  1133.         $thumbnailConfig null;
  1134.         if ($request->get('thumbnail')) {
  1135.             $thumbnailConfig $image->getThumbnailConfig($request->get('thumbnail'));
  1136.         }
  1137.         if (!$thumbnailConfig) {
  1138.             if ($request->get('config')) {
  1139.                 $thumbnailConfig $image->getThumbnailConfig($this->decodeJson($request->get('config')));
  1140.             } else {
  1141.                 $thumbnailConfig $image->getThumbnailConfig(array_merge($request->request->all(), $request->query->all()));
  1142.             }
  1143.         } else {
  1144.             // no high-res images in admin mode (editmode)
  1145.             // this is mostly because of the document's image editable, which doesn't know anything about the thumbnail
  1146.             // configuration, so the dimensions would be incorrect (double the size)
  1147.             $thumbnailConfig->setHighResolution(1);
  1148.         }
  1149.         $format strtolower($thumbnailConfig->getFormat());
  1150.         if ($format == 'source' || $format == 'print') {
  1151.             $thumbnailConfig->setFormat('PNG');
  1152.             $thumbnailConfig->setRasterizeSVG(true);
  1153.         }
  1154.         if ($request->get('treepreview')) {
  1155.             $thumbnailConfig Asset\Image\Thumbnail\Config::getPreviewConfig();
  1156.             if ($request->get('origin') === 'treeNode' && !$image->getThumbnail($thumbnailConfig)->exists()) {
  1157.                 \Pimcore::getContainer()->get('messenger.bus.pimcore-core')->dispatch(
  1158.                     new AssetPreviewImageMessage($image->getId())
  1159.                 );
  1160.                 throw $this->createNotFoundException(sprintf('Tree preview thumbnail not available for asset %s'$image->getId()));
  1161.             }
  1162.         }
  1163.         $cropPercent $request->get('cropPercent');
  1164.         if ($cropPercent && filter_var($cropPercentFILTER_VALIDATE_BOOLEAN)) {
  1165.             $thumbnailConfig->addItemAt(0'cropPercent', [
  1166.                 'width' => $request->get('cropWidth'),
  1167.                 'height' => $request->get('cropHeight'),
  1168.                 'y' => $request->get('cropTop'),
  1169.                 'x' => $request->get('cropLeft'),
  1170.             ]);
  1171.             $hash md5(Tool\Serialize::serialize(array_merge($request->request->all(), $request->query->all())));
  1172.             $thumbnailConfig->setName($thumbnailConfig->getName() . '_auto_' $hash);
  1173.         }
  1174.         $thumbnail $image->getThumbnail($thumbnailConfig);
  1175.         if ($fileinfo) {
  1176.             return $this->adminJson([
  1177.                 'width' => $thumbnail->getWidth(),
  1178.                 'height' => $thumbnail->getHeight(), ]);
  1179.         }
  1180.         $stream $thumbnail->getStream();
  1181.         if (!$stream) {
  1182.             return new BinaryFileResponse(PIMCORE_PATH '/bundles/AdminBundle/Resources/public/img/filetype-not-supported.svg');
  1183.         }
  1184.         $response = new StreamedResponse(function () use ($stream) {
  1185.             fpassthru($stream);
  1186.         }, 200, [
  1187.             'Content-Type' => $thumbnail->getMimeType(),
  1188.             'Access-Control-Allow-Origin''*',
  1189.         ]);
  1190.         $this->addThumbnailCacheHeaders($response);
  1191.         return $response;
  1192.     }
  1193.     /**
  1194.      * @Route("/get-folder-thumbnail", name="pimcore_admin_asset_getfolderthumbnail", methods={"GET"})
  1195.      *
  1196.      * @param Request $request
  1197.      *
  1198.      * @return StreamedResponse
  1199.      */
  1200.     public function getFolderThumbnailAction(Request $request)
  1201.     {
  1202.         $folder null;
  1203.         if ($request->get('id')) {
  1204.             $folder Asset\Folder::getById((int)$request->get('id'));
  1205.             if ($folder instanceof  Asset\Folder) {
  1206.                 if (!$folder->isAllowed('view')) {
  1207.                     throw $this->createAccessDeniedException('not allowed to view thumbnail');
  1208.                 }
  1209.                 $stream $folder->getPreviewImage();
  1210.                 if (!$stream) {
  1211.                     throw $this->createNotFoundException(sprintf('Tree preview thumbnail not available for asset %s'$folder->getId()));
  1212.                 } else {
  1213.                     $response = new StreamedResponse(function () use ($stream) {
  1214.                         fpassthru($stream);
  1215.                     }, 200, [
  1216.                         'Content-Type' => 'image/jpg',
  1217.                     ]);
  1218.                 }
  1219.                 $this->addThumbnailCacheHeaders($response);
  1220.                 return $response;
  1221.             }
  1222.         }
  1223.         throw $this->createNotFoundException('could not load asset folder');
  1224.     }
  1225.     /**
  1226.      * @Route("/get-video-thumbnail", name="pimcore_admin_asset_getvideothumbnail", methods={"GET"})
  1227.      *
  1228.      * @param Request $request
  1229.      *
  1230.      * @return StreamedResponse
  1231.      */
  1232.     public function getVideoThumbnailAction(Request $request)
  1233.     {
  1234.         $video null;
  1235.         if ($request->get('id')) {
  1236.             $video Asset\Video::getById((int)$request->get('id'));
  1237.         } elseif ($request->get('path')) {
  1238.             $video Asset\Video::getByPath($request->get('path'));
  1239.         }
  1240.         if (!$video) {
  1241.             throw $this->createNotFoundException('could not load video asset');
  1242.         }
  1243.         if (!$video->isAllowed('view')) {
  1244.             throw $this->createAccessDeniedException('not allowed to view thumbnail');
  1245.         }
  1246.         $thumbnail array_merge($request->request->all(), $request->query->all());
  1247.         if ($request->get('treepreview')) {
  1248.             $thumbnail Asset\Image\Thumbnail\Config::getPreviewConfig();
  1249.         }
  1250.         $time null;
  1251.         if (is_numeric($request->get('time'))) {
  1252.             $time = (int)$request->get('time');
  1253.         }
  1254.         if ($request->get('settime')) {
  1255.             $video->removeCustomSetting('image_thumbnail_asset');
  1256.             $video->setCustomSetting('image_thumbnail_time'$time);
  1257.             $video->save();
  1258.         }
  1259.         $image null;
  1260.         if ($request->get('image')) {
  1261.             $image Asset\Image::getById((int)$request->get('image'));
  1262.         }
  1263.         if ($request->get('setimage') && $image) {
  1264.             $video->removeCustomSetting('image_thumbnail_time');
  1265.             $video->setCustomSetting('image_thumbnail_asset'$image->getId());
  1266.             $video->save();
  1267.         }
  1268.         $thumb $video->getImageThumbnail($thumbnail$time$image);
  1269.         if ($request->get('origin') === 'treeNode' && !$thumb->exists()) {
  1270.             \Pimcore::getContainer()->get('messenger.bus.pimcore-core')->dispatch(
  1271.                 new AssetPreviewImageMessage($video->getId())
  1272.             );
  1273.             throw $this->createNotFoundException(sprintf('Tree preview thumbnail not available for asset %s'$video->getId()));
  1274.         }
  1275.         $stream $thumb->getStream();
  1276.         $response = new StreamedResponse(function () use ($stream) {
  1277.             fpassthru($stream);
  1278.         }, 200, [
  1279.             'Content-Type' => 'image/' $thumb->getFileExtension(),
  1280.         ]);
  1281.         $this->addThumbnailCacheHeaders($response);
  1282.         return $response;
  1283.     }
  1284.     /**
  1285.      * @Route("/get-document-thumbnail", name="pimcore_admin_asset_getdocumentthumbnail", methods={"GET"})
  1286.      *
  1287.      * @param Request $request
  1288.      *
  1289.      * @return StreamedResponse|BinaryFileResponse
  1290.      */
  1291.     public function getDocumentThumbnailAction(Request $request)
  1292.     {
  1293.         $document Asset\Document::getById((int)$request->get('id'));
  1294.         if (!$document) {
  1295.             throw $this->createNotFoundException('could not load document asset');
  1296.         }
  1297.         if (!$document->isAllowed('view')) {
  1298.             throw $this->createAccessDeniedException('not allowed to view thumbnail');
  1299.         }
  1300.         $thumbnail Asset\Image\Thumbnail\Config::getByAutoDetect(array_merge($request->request->all(), $request->query->all()));
  1301.         $format strtolower($thumbnail->getFormat());
  1302.         if ($format == 'source') {
  1303.             $thumbnail->setFormat('jpeg'); // default format for documents is JPEG not PNG (=too big)
  1304.         }
  1305.         if ($request->get('treepreview')) {
  1306.             $thumbnail Asset\Image\Thumbnail\Config::getPreviewConfig();
  1307.         }
  1308.         $page 1;
  1309.         if (is_numeric($request->get('page'))) {
  1310.             $page = (int)$request->get('page');
  1311.         }
  1312.         $thumb $document->getImageThumbnail($thumbnail$page);
  1313.         if ($request->get('origin') === 'treeNode' && !$thumb->exists()) {
  1314.             \Pimcore::getContainer()->get('messenger.bus.pimcore-core')->dispatch(
  1315.                 new AssetPreviewImageMessage($document->getId())
  1316.             );
  1317.             throw $this->createNotFoundException(sprintf('Tree preview thumbnail not available for asset %s'$document->getId()));
  1318.         }
  1319.         $stream $thumb->getStream();
  1320.         if ($stream) {
  1321.             $response = new StreamedResponse(function () use ($stream) {
  1322.                 fpassthru($stream);
  1323.             }, 200, [
  1324.                 'Content-Type' => 'image/' $thumb->getFileExtension(),
  1325.             ]);
  1326.         } else {
  1327.             $response = new BinaryFileResponse(PIMCORE_PATH '/bundles/AdminBundle/Resources/public/img/filetype-not-supported.svg');
  1328.         }
  1329.         $this->addThumbnailCacheHeaders($response);
  1330.         return $response;
  1331.     }
  1332.     /**
  1333.      * @param Response $response
  1334.      */
  1335.     protected function addThumbnailCacheHeaders(Response $response)
  1336.     {
  1337.         $lifetime 300;
  1338.         $date = new \DateTime('now');
  1339.         $date->add(new \DateInterval('PT' $lifetime 'S'));
  1340.         $response->setMaxAge($lifetime);
  1341.         $response->setPublic();
  1342.         $response->setExpires($date);
  1343.         $response->headers->set('Pragma''');
  1344.     }
  1345.     /**
  1346.      * @Route("/get-preview-document", name="pimcore_admin_asset_getpreviewdocument", methods={"GET"})
  1347.      *
  1348.      * @param Request $request
  1349.      *
  1350.      * @return StreamedResponse
  1351.      */
  1352.     public function getPreviewDocumentAction(Request $request)
  1353.     {
  1354.         $asset Asset\Document::getById((int) $request->get('id'));
  1355.         if (!$asset) {
  1356.             throw $this->createNotFoundException('could not load document asset');
  1357.         }
  1358.         if ($asset->isAllowed('view')) {
  1359.             $stream $this->getDocumentPreviewPdf($asset);
  1360.             if ($stream) {
  1361.                 return new StreamedResponse(function () use ($stream) {
  1362.                     fpassthru($stream);
  1363.                 }, 200, [
  1364.                     'Content-Type' => 'application/pdf',
  1365.                 ]);
  1366.             } else {
  1367.                 throw $this->createNotFoundException('Unable to get preview for asset ' $asset->getId());
  1368.             }
  1369.         } else {
  1370.             throw $this->createAccessDeniedException('Access to asset ' $asset->getId() . ' denied');
  1371.         }
  1372.     }
  1373.     /**
  1374.      * @param Asset\Document $asset
  1375.      *
  1376.      * @return resource|null
  1377.      */
  1378.     protected function getDocumentPreviewPdf(Asset\Document $asset)
  1379.     {
  1380.         $stream null;
  1381.         if ($asset->getMimeType() == 'application/pdf') {
  1382.             $stream $asset->getStream();
  1383.         }
  1384.         if (!$stream && $asset->getPageCount() && \Pimcore\Document::isAvailable() && \Pimcore\Document::isFileTypeSupported($asset->getFilename())) {
  1385.             try {
  1386.                 $document \Pimcore\Document::getInstance();
  1387.                 $stream $document->getPdf($asset);
  1388.             } catch (\Exception $e) {
  1389.                 // nothing to do
  1390.             }
  1391.         }
  1392.         return $stream;
  1393.     }
  1394.     /**
  1395.      * @Route("/get-preview-video", name="pimcore_admin_asset_getpreviewvideo", methods={"GET"})
  1396.      *
  1397.      * @param Request $request
  1398.      *
  1399.      * @return Response
  1400.      */
  1401.     public function getPreviewVideoAction(Request $request)
  1402.     {
  1403.         $asset Asset\Video::getById((int) $request->get('id'));
  1404.         if (!$asset) {
  1405.             throw $this->createNotFoundException('could not load video asset');
  1406.         }
  1407.         if (!$asset->isAllowed('view')) {
  1408.             throw $this->createAccessDeniedException('not allowed to preview');
  1409.         }
  1410.         $previewData = ['asset' => $asset];
  1411.         $config Asset\Video\Thumbnail\Config::getPreviewConfig();
  1412.         $thumbnail $asset->getThumbnail($config, ['mp4']);
  1413.         if ($thumbnail) {
  1414.             $previewData['asset'] = $asset;
  1415.             $previewData['thumbnail'] = $thumbnail;
  1416.             if ($thumbnail['status'] == 'finished') {
  1417.                 return $this->render(
  1418.                     '@PimcoreAdmin/Admin/Asset/getPreviewVideoDisplay.html.twig',
  1419.                     $previewData
  1420.                 );
  1421.             } else {
  1422.                 return $this->render(
  1423.                     '@PimcoreAdmin/Admin/Asset/getPreviewVideoError.html.twig',
  1424.                     $previewData
  1425.                 );
  1426.             }
  1427.         } else {
  1428.             return $this->render(
  1429.                 '@PimcoreAdmin/Admin/Asset/getPreviewVideoError.html.twig',
  1430.                 $previewData
  1431.             );
  1432.         }
  1433.     }
  1434.     /**
  1435.      * @Route("/serve-video-preview", name="pimcore_admin_asset_servevideopreview", methods={"GET"})
  1436.      *
  1437.      * @param Request $request
  1438.      *
  1439.      * @return StreamedResponse
  1440.      */
  1441.     public function serveVideoPreviewAction(Request $request)
  1442.     {
  1443.         $asset Asset\Video::getById((int) $request->get('id'));
  1444.         if (!$asset) {
  1445.             throw $this->createNotFoundException('could not load video asset');
  1446.         }
  1447.         if (!$asset->isAllowed('view')) {
  1448.             throw $this->createAccessDeniedException('not allowed to preview');
  1449.         }
  1450.         $config Asset\Video\Thumbnail\Config::getPreviewConfig();
  1451.         $thumbnail $asset->getThumbnail($config, ['mp4']);
  1452.         $storagePath $asset->getRealPath() . '/' preg_replace('@^' preg_quote($asset->getPath(), '@') . '@'''urldecode($thumbnail['formats']['mp4']));
  1453.         $storage Tool\Storage::get('thumbnail');
  1454.         if ($storage->fileExists($storagePath)) {
  1455.             $fs $storage->fileSize($storagePath);
  1456.             $stream $storage->readStream($storagePath);
  1457.             return new StreamedResponse(function () use ($stream) {
  1458.                 fpassthru($stream);
  1459.             }, 200, [
  1460.                 'Content-Type' => 'video/mp4',
  1461.                 'Content-Length' => $fs,
  1462.                 'Accept-Ranges' => 'bytes',
  1463.             ]);
  1464.         } else {
  1465.             throw $this->createNotFoundException('Video thumbnail not found');
  1466.         }
  1467.     }
  1468.     /**
  1469.      * @Route("/image-editor", name="pimcore_admin_asset_imageeditor", methods={"GET"})
  1470.      *
  1471.      * @param Request $request
  1472.      *
  1473.      * @return Response
  1474.      */
  1475.     public function imageEditorAction(Request $request)
  1476.     {
  1477.         $asset Asset::getById((int) $request->get('id'));
  1478.         if (!$asset->isAllowed('view')) {
  1479.             throw $this->createAccessDeniedException('Not allowed to preview');
  1480.         }
  1481.         return $this->render(
  1482.             '@PimcoreAdmin/Admin/Asset/imageEditor.html.twig',
  1483.             ['asset' => $asset]
  1484.         );
  1485.     }
  1486.     /**
  1487.      * @Route("/image-editor-save", name="pimcore_admin_asset_imageeditorsave", methods={"PUT"})
  1488.      *
  1489.      * @param Request $request
  1490.      *
  1491.      * @return JsonResponse
  1492.      */
  1493.     public function imageEditorSaveAction(Request $request)
  1494.     {
  1495.         $asset Asset::getById((int) $request->get('id'));
  1496.         if (!$asset) {
  1497.             throw $this->createNotFoundException('Asset not found');
  1498.         }
  1499.         if (!$asset->isAllowed('publish')) {
  1500.             throw $this->createAccessDeniedException('not allowed to publish');
  1501.         }
  1502.         $data $request->get('dataUri');
  1503.         $data substr($datastrpos($data','));
  1504.         $data base64_decode($data);
  1505.         $asset->setData($data);
  1506.         $asset->setUserModification($this->getAdminUser()->getId());
  1507.         $asset->save();
  1508.         return $this->adminJson(['success' => true]);
  1509.     }
  1510.     /**
  1511.      * @Route("/get-folder-content-preview", name="pimcore_admin_asset_getfoldercontentpreview", methods={"GET"})
  1512.      *
  1513.      * @param Request $request
  1514.      *
  1515.      * @return JsonResponse
  1516.      */
  1517.     public function getFolderContentPreviewAction(Request $requestEventDispatcherInterface $eventDispatcher)
  1518.     {
  1519.         $allParams array_merge($request->request->all(), $request->query->all());
  1520.         $filterPrepareEvent = new GenericEvent($this, [
  1521.             'requestParams' => $allParams,
  1522.         ]);
  1523.         $eventDispatcher->dispatch($filterPrepareEventAdminEvents::ASSET_LIST_BEFORE_FILTER_PREPARE);
  1524.         $allParams $filterPrepareEvent->getArgument('requestParams');
  1525.         $folder Asset::getById($allParams['id']);
  1526.         $start 0;
  1527.         $limit 10;
  1528.         if ($allParams['limit']) {
  1529.             $limit $allParams['limit'];
  1530.         }
  1531.         if ($allParams['start']) {
  1532.             $start $allParams['start'];
  1533.         }
  1534.         $conditionFilters = [];
  1535.         $list = new Asset\Listing();
  1536.         $conditionFilters[] = 'path LIKE ' . ($folder->getRealFullPath() == '/' "'/%'" $list->quote($list->escapeLike($folder->getRealFullPath()) . '/%')) . " AND type != 'folder'";
  1537.         if (!$this->getAdminUser()->isAdmin()) {
  1538.             $userIds $this->getAdminUser()->getRoles();
  1539.             $userIds[] = $this->getAdminUser()->getId();
  1540.             $conditionFilters[] = ' (
  1541.                                                     (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
  1542.                                                     OR
  1543.                                                     (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
  1544.                                                  )';
  1545.         }
  1546.         $condition implode(' AND '$conditionFilters);
  1547.         $list->setCondition($condition);
  1548.         $list->setLimit($limit);
  1549.         $list->setOffset($start);
  1550.         $list->setOrderKey('CAST(filename AS CHAR CHARACTER SET utf8) COLLATE utf8_general_ci ASC'false);
  1551.         $beforeListLoadEvent = new GenericEvent($this, [
  1552.             'list' => $list,
  1553.             'context' => $allParams,
  1554.         ]);
  1555.         $eventDispatcher->dispatch($beforeListLoadEventAdminEvents::ASSET_LIST_BEFORE_LIST_LOAD);
  1556.         /** @var Asset\Listing $list */
  1557.         $list $beforeListLoadEvent->getArgument('list');
  1558.         $list->load();
  1559.         $assets = [];
  1560.         foreach ($list as $asset) {
  1561.             $thumbnailMethod Asset\Service::getPreviewThumbnail($asset, [], true);
  1562.             if (!empty($thumbnailMethod)) {
  1563.                 $filenameDisplay $asset->getFilename();
  1564.                 if (strlen($filenameDisplay) > 32) {
  1565.                     $filenameDisplay substr($filenameDisplay025) . '...' \Pimcore\File::getFileExtension($filenameDisplay);
  1566.                 }
  1567.                 // 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
  1568.                 if ($asset->isAllowed('list')) {
  1569.                     $assets[] = [
  1570.                         'id' => $asset->getId(),
  1571.                         'type' => $asset->getType(),
  1572.                         'filename' => $asset->getFilename(),
  1573.                         'filenameDisplay' => htmlspecialchars($filenameDisplay),
  1574.                         'url' => $this->getThumbnailUrl($asset),
  1575.                         'idPath' => $data['idPath'] = Element\Service::getIdPath($asset),
  1576.                     ];
  1577.                 }
  1578.             }
  1579.         }
  1580.         // We need to temporary use data key to be compatible with the ASSET_LIST_AFTER_LIST_LOAD global event
  1581.         $result = ['data' => $assets'success' => true'total' => $list->getTotalCount()];
  1582.         $afterListLoadEvent = new GenericEvent($this, [
  1583.             'list' => $result,
  1584.             'context' => $allParams,
  1585.         ]);
  1586.         $eventDispatcher->dispatch($afterListLoadEventAdminEvents::ASSET_LIST_AFTER_LIST_LOAD);
  1587.         $result $afterListLoadEvent->getArgument('list');
  1588.         // Here we revert to assets key
  1589.         return $this->adminJson(['assets' => $result['data'], 'success' => $result['success'], 'total' => $result['total']]);
  1590.     }
  1591.     /**
  1592.      * @Route("/copy-info", name="pimcore_admin_asset_copyinfo", methods={"GET"})
  1593.      *
  1594.      * @param Request $request
  1595.      *
  1596.      * @return JsonResponse
  1597.      */
  1598.     public function copyInfoAction(Request $request)
  1599.     {
  1600.         $transactionId time();
  1601.         $pasteJobs = [];
  1602.         Tool\Session::useSession(function (AttributeBagInterface $session) use ($transactionId) {
  1603.             $session->set((string) $transactionId, []);
  1604.         }, 'pimcore_copy');
  1605.         if ($request->get('type') == 'recursive') {
  1606.             $asset Asset::getById((int) $request->get('sourceId'));
  1607.             if (!$asset) {
  1608.                 throw $this->createNotFoundException('Source not found');
  1609.             }
  1610.             // first of all the new parent
  1611.             $pasteJobs[] = [[
  1612.                 'url' => $this->generateUrl('pimcore_admin_asset_copy'),
  1613.                 'method' => 'POST',
  1614.                 'params' => [
  1615.                     'sourceId' => $request->get('sourceId'),
  1616.                     'targetId' => $request->get('targetId'),
  1617.                     'type' => 'child',
  1618.                     'transactionId' => $transactionId,
  1619.                     'saveParentId' => true,
  1620.                 ],
  1621.             ]];
  1622.             if ($asset->hasChildren()) {
  1623.                 // get amount of children
  1624.                 $list = new Asset\Listing();
  1625.                 $list->setCondition('path LIKE ?', [$list->escapeLike($asset->getRealFullPath()) . '/%']);
  1626.                 $list->setOrderKey('LENGTH(path)'false);
  1627.                 $list->setOrder('ASC');
  1628.                 $childIds $list->loadIdList();
  1629.                 if (count($childIds) > 0) {
  1630.                     foreach ($childIds as $id) {
  1631.                         $pasteJobs[] = [[
  1632.                             'url' => $this->generateUrl('pimcore_admin_asset_copy'),
  1633.                             'method' => 'POST',
  1634.                             'params' => [
  1635.                                 'sourceId' => $id,
  1636.                                 'targetParentId' => $request->get('targetId'),
  1637.                                 'sourceParentId' => $request->get('sourceId'),
  1638.                                 'type' => 'child',
  1639.                                 'transactionId' => $transactionId,
  1640.                             ],
  1641.                         ]];
  1642.                     }
  1643.                 }
  1644.             }
  1645.         } elseif ($request->get('type') == 'child' || $request->get('type') == 'replace') {
  1646.             // the object itself is the last one
  1647.             $pasteJobs[] = [[
  1648.                 'url' => $this->generateUrl('pimcore_admin_asset_copy'),
  1649.                 'method' => 'POST',
  1650.                 'params' => [
  1651.                     'sourceId' => $request->get('sourceId'),
  1652.                     'targetId' => $request->get('targetId'),
  1653.                     'type' => $request->get('type'),
  1654.                     'transactionId' => $transactionId,
  1655.                 ],
  1656.             ]];
  1657.         }
  1658.         return $this->adminJson([
  1659.             'pastejobs' => $pasteJobs,
  1660.         ]);
  1661.     }
  1662.     /**
  1663.      * @Route("/copy", name="pimcore_admin_asset_copy", methods={"POST"})
  1664.      *
  1665.      * @param Request $request
  1666.      *
  1667.      * @return JsonResponse
  1668.      */
  1669.     public function copyAction(Request $request)
  1670.     {
  1671.         $success false;
  1672.         $sourceId = (int)$request->get('sourceId');
  1673.         $source Asset::getById($sourceId);
  1674.         $session Tool\Session::get('pimcore_copy');
  1675.         $sessionBag $session->get($request->get('transactionId'));
  1676.         $targetId = (int)$request->get('targetId');
  1677.         if ($request->get('targetParentId')) {
  1678.             $sourceParent Asset::getById((int) $request->get('sourceParentId'));
  1679.             // this is because the key can get the prefix "_copy" if the target does already exists
  1680.             if ($sessionBag['parentId']) {
  1681.                 $targetParent Asset::getById($sessionBag['parentId']);
  1682.             } else {
  1683.                 $targetParent Asset::getById((int) $request->get('targetParentId'));
  1684.             }
  1685.             $targetPath preg_replace('@^' $sourceParent->getRealFullPath() . '@'$targetParent '/'$source->getRealPath());
  1686.             $target Asset::getByPath($targetPath);
  1687.         } else {
  1688.             $target Asset::getById($targetId);
  1689.         }
  1690.         if (!$target) {
  1691.             throw $this->createNotFoundException('Target not found');
  1692.         }
  1693.         if ($target->isAllowed('create')) {
  1694.             $source Asset::getById($sourceId);
  1695.             if ($source != null) {
  1696.                 if ($request->get('type') == 'child') {
  1697.                     $newAsset $this->_assetService->copyAsChild($target$source);
  1698.                     // this is because the key can get the prefix "_copy" if the target does already exists
  1699.                     if ($request->get('saveParentId')) {
  1700.                         $sessionBag['parentId'] = $newAsset->getId();
  1701.                     }
  1702.                 } elseif ($request->get('type') == 'replace') {
  1703.                     $this->_assetService->copyContents($target$source);
  1704.                 }
  1705.                 $session->set($request->get('transactionId'), $sessionBag);
  1706.                 Tool\Session::writeClose();
  1707.                 $success true;
  1708.             } else {
  1709.                 Logger::debug('prevended copy/paste because asset with same path+key already exists in this location');
  1710.             }
  1711.         } else {
  1712.             Logger::error('could not execute copy/paste because of missing permissions on target [ ' $targetId ' ]');
  1713.             throw $this->createAccessDeniedHttpException();
  1714.         }
  1715.         Tool\Session::writeClose();
  1716.         return $this->adminJson(['success' => $success]);
  1717.     }
  1718.     /**
  1719.      * @Route("/download-as-zip-jobs", name="pimcore_admin_asset_downloadaszipjobs", methods={"GET"})
  1720.      *
  1721.      * @param Request $request
  1722.      *
  1723.      * @return JsonResponse
  1724.      */
  1725.     public function downloadAsZipJobsAction(Request $request)
  1726.     {
  1727.         $jobId uniqid();
  1728.         $filesPerJob 5;
  1729.         $jobs = [];
  1730.         $asset Asset::getById((int) $request->get('id'));
  1731.         if (!$asset) {
  1732.             throw $this->createNotFoundException('Asset not found');
  1733.         }
  1734.         if ($asset->isAllowed('view')) {
  1735.             $parentPath $asset->getRealFullPath();
  1736.             if ($asset->getId() == 1) {
  1737.                 $parentPath '';
  1738.             }
  1739.             $db \Pimcore\Db::get();
  1740.             $conditionFilters = [];
  1741.             $selectedIds explode(','$request->get('selectedIds'''));
  1742.             $quotedSelectedIds = [];
  1743.             foreach ($selectedIds as $selectedId) {
  1744.                 if ($selectedId) {
  1745.                     $quotedSelectedIds[] = $db->quote($selectedId);
  1746.                 }
  1747.             }
  1748.             if (!empty($quotedSelectedIds)) {
  1749.                 //add a condition if id numbers are specified
  1750.                 $conditionFilters[] = 'id IN (' implode(','$quotedSelectedIds) . ')';
  1751.             }
  1752.             $conditionFilters[] = 'path LIKE ' $db->quote($db->escapeLike($parentPath) . '/%') . ' AND type != ' $db->quote('folder');
  1753.             if (!$this->getAdminUser()->isAdmin()) {
  1754.                 $userIds $this->getAdminUser()->getRoles();
  1755.                 $userIds[] = $this->getAdminUser()->getId();
  1756.                 $conditionFilters[] = ' (
  1757.                                                     (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
  1758.                                                     OR
  1759.                                                     (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
  1760.                                                  )';
  1761.             }
  1762.             $condition implode(' AND '$conditionFilters);
  1763.             $assetList = new Asset\Listing();
  1764.             $assetList->setCondition($condition);
  1765.             $assetList->setOrderKey('LENGTH(path)'false);
  1766.             $assetList->setOrder('ASC');
  1767.             for ($i 0$i ceil($assetList->getTotalCount() / $filesPerJob); $i++) {
  1768.                 $jobs[] = [[
  1769.                     'url' => $this->generateUrl('pimcore_admin_asset_downloadaszipaddfiles'),
  1770.                     'method' => 'GET',
  1771.                     'params' => [
  1772.                         'id' => $asset->getId(),
  1773.                         'selectedIds' => implode(','$selectedIds),
  1774.                         'offset' => $i $filesPerJob,
  1775.                         'limit' => $filesPerJob,
  1776.                         'jobId' => $jobId,
  1777.                     ],
  1778.                 ]];
  1779.             }
  1780.         }
  1781.         return $this->adminJson([
  1782.             'success' => true,
  1783.             'jobs' => $jobs,
  1784.             'jobId' => $jobId,
  1785.         ]);
  1786.     }
  1787.     /**
  1788.      * @Route("/download-as-zip-add-files", name="pimcore_admin_asset_downloadaszipaddfiles", methods={"GET"})
  1789.      *
  1790.      * @param Request $request
  1791.      *
  1792.      * @return JsonResponse
  1793.      */
  1794.     public function downloadAsZipAddFilesAction(Request $request)
  1795.     {
  1796.         $zipFile PIMCORE_SYSTEM_TEMP_DIRECTORY '/download-zip-' $request->get('jobId') . '.zip';
  1797.         $asset Asset::getById((int) $request->get('id'));
  1798.         $success false;
  1799.         if (!$asset) {
  1800.             throw $this->createNotFoundException('Asset not found');
  1801.         }
  1802.         if ($asset->isAllowed('view')) {
  1803.             $zip = new \ZipArchive();
  1804.             if (!is_file($zipFile)) {
  1805.                 $zipState $zip->open($zipFile\ZipArchive::CREATE);
  1806.             } else {
  1807.                 $zipState $zip->open($zipFile);
  1808.             }
  1809.             if ($zipState === true) {
  1810.                 $parentPath $asset->getRealFullPath();
  1811.                 if ($asset->getId() == 1) {
  1812.                     $parentPath '';
  1813.                 }
  1814.                 $db \Pimcore\Db::get();
  1815.                 $conditionFilters = [];
  1816.                 $selectedIds $request->get('selectedIds', []);
  1817.                 if (!empty($selectedIds)) {
  1818.                     $selectedIds explode(','$selectedIds);
  1819.                     //add a condition if id numbers are specified
  1820.                     $conditionFilters[] = 'id IN (' implode(','$selectedIds) . ')';
  1821.                 }
  1822.                 $conditionFilters[] = "type != 'folder' AND path LIKE " $db->quote($db->escapeLike($parentPath) . '/%');
  1823.                 if (!$this->getAdminUser()->isAdmin()) {
  1824.                     $userIds $this->getAdminUser()->getRoles();
  1825.                     $userIds[] = $this->getAdminUser()->getId();
  1826.                     $conditionFilters[] = ' (
  1827.                                                     (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
  1828.                                                     OR
  1829.                                                     (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
  1830.                                                  )';
  1831.                 }
  1832.                 $condition implode(' AND '$conditionFilters);
  1833.                 $assetList = new Asset\Listing();
  1834.                 $assetList->setCondition($condition);
  1835.                 $assetList->setOrderKey('LENGTH(path) ASC, id ASC'false);
  1836.                 $assetList->setOffset((int)$request->get('offset'));
  1837.                 $assetList->setLimit((int)$request->get('limit'));
  1838.                 foreach ($assetList as $a) {
  1839.                     if ($a->isAllowed('view')) {
  1840.                         if (!$a instanceof Asset\Folder) {
  1841.                             // add the file with the relative path to the parent directory
  1842.                             $zip->addFile($a->getLocalFile(), preg_replace('@^' preg_quote($asset->getRealPath(), '@') . '@i'''$a->getRealFullPath()));
  1843.                         }
  1844.                     }
  1845.                 }
  1846.                 $zip->close();
  1847.                 $success true;
  1848.             }
  1849.         }
  1850.         return $this->adminJson([
  1851.             'success' => $success,
  1852.         ]);
  1853.     }
  1854.     /**
  1855.      * @Route("/download-as-zip", name="pimcore_admin_asset_downloadaszip", methods={"GET"})
  1856.      *
  1857.      * @param Request $request
  1858.      *
  1859.      * @return BinaryFileResponse
  1860.      * Download all assets contained in the folder with parameter id as ZIP file.
  1861.      * The suggested filename is either [folder name].zip or assets.zip for the root folder.
  1862.      */
  1863.     public function downloadAsZipAction(Request $request)
  1864.     {
  1865.         $asset Asset::getById((int) $request->get('id'));
  1866.         if (!$asset) {
  1867.             throw $this->createNotFoundException('Asset not found');
  1868.         }
  1869.         $zipFile PIMCORE_SYSTEM_TEMP_DIRECTORY '/download-zip-' $request->get('jobId') . '.zip';
  1870.         $suggestedFilename $asset->getFilename();
  1871.         if (empty($suggestedFilename)) {
  1872.             $suggestedFilename 'assets';
  1873.         }
  1874.         $response = new BinaryFileResponse($zipFile);
  1875.         $response->headers->set('Content-Type''application/zip');
  1876.         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT$suggestedFilename '.zip');
  1877.         $response->deleteFileAfterSend(true);
  1878.         return $response;
  1879.     }
  1880.     /**
  1881.      * @Route("/import-zip", name="pimcore_admin_asset_importzip", methods={"POST"})
  1882.      *
  1883.      * @param Request $request
  1884.      *
  1885.      * @return Response
  1886.      */
  1887.     public function importZipAction(Request $request)
  1888.     {
  1889.         $jobId uniqid();
  1890.         $filesPerJob 5;
  1891.         $jobs = [];
  1892.         $asset Asset::getById((int) $request->get('parentId'));
  1893.         if (!is_file($_FILES['Filedata']['tmp_name'])) {
  1894.             return $this->adminJson([
  1895.                 'success' => false,
  1896.                 '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',
  1897.             ]);
  1898.         }
  1899.         if (!$asset) {
  1900.             throw $this->createNotFoundException('Parent asset not found');
  1901.         }
  1902.         if (!$asset->isAllowed('create')) {
  1903.             throw $this->createAccessDeniedException('not allowed to create');
  1904.         }
  1905.         $zipFile PIMCORE_SYSTEM_TEMP_DIRECTORY '/' $jobId '.zip';
  1906.         copy($_FILES['Filedata']['tmp_name'], $zipFile);
  1907.         $zip = new \ZipArchive;
  1908.         $retCode $zip->open($zipFile);
  1909.         if ($retCode === true) {
  1910.             $jobAmount ceil($zip->numFiles $filesPerJob);
  1911.             for ($i 0$i $jobAmount$i++) {
  1912.                 $jobs[] = [[
  1913.                     'url' => $this->generateUrl('pimcore_admin_asset_importzipfiles'),
  1914.                     'method' => 'POST',
  1915.                     'params' => [
  1916.                         'parentId' => $asset->getId(),
  1917.                         'offset' => $i $filesPerJob,
  1918.                         'limit' => $filesPerJob,
  1919.                         'jobId' => $jobId,
  1920.                         'last' => (($i 1) >= $jobAmount) ? 'true' '',
  1921.                     ],
  1922.                 ]];
  1923.             }
  1924.             $zip->close();
  1925.             // here we have to use this method and not the JSON action helper ($this->_helper->json()) because this will add
  1926.             // Content-Type: application/json which fires a download window in most browsers, because this is a normal POST
  1927.             // request and not XHR where the content-type doesn't matter
  1928.             $responseJson $this->encodeJson([
  1929.                 'success' => true,
  1930.                 'jobs' => $jobs,
  1931.                 'jobId' => $jobId,
  1932.             ]);
  1933.             return new Response($responseJson);
  1934.         } else {
  1935.             return $this->adminJson([
  1936.                 'success' => false,
  1937.                 'message' => $this->trans('could_not_open_zip_file'),
  1938.             ]);
  1939.         }
  1940.     }
  1941.     /**
  1942.      * @Route("/import-zip-files", name="pimcore_admin_asset_importzipfiles", methods={"POST"})
  1943.      *
  1944.      * @param Request $request
  1945.      *
  1946.      * @return JsonResponse
  1947.      */
  1948.     public function importZipFilesAction(Request $request)
  1949.     {
  1950.         $jobId $request->get('jobId');
  1951.         $limit = (int)$request->get('limit');
  1952.         $offset = (int)$request->get('offset');
  1953.         $importAsset Asset::getById((int) $request->get('parentId'));
  1954.         $zipFile PIMCORE_SYSTEM_TEMP_DIRECTORY '/' $jobId '.zip';
  1955.         $tmpDir PIMCORE_SYSTEM_TEMP_DIRECTORY '/zip-import';
  1956.         if (!is_dir($tmpDir)) {
  1957.             File::mkdir($tmpDir0777true);
  1958.         }
  1959.         $zip = new \ZipArchive;
  1960.         if ($zip->open($zipFile) === true) {
  1961.             for ($i $offset$i < ($offset $limit); $i++) {
  1962.                 $path $zip->getNameIndex($i);
  1963.                 if (str_starts_with($path'__MACOSX/')) {
  1964.                     continue;
  1965.                 }
  1966.                 if ($path !== false) {
  1967.                     if ($zip->extractTo($tmpDir '/'$path)) {
  1968.                         $tmpFile $tmpDir '/' preg_replace('@^/@'''$path);
  1969.                         $filename Element\Service::getValidKey(basename($path), 'asset');
  1970.                         $relativePath '';
  1971.                         if (dirname($path) != '.') {
  1972.                             $relativePath dirname($path);
  1973.                         }
  1974.                         $parentPath $importAsset->getRealFullPath() . '/' preg_replace('@^/@'''$relativePath);
  1975.                         $parent Asset\Service::createFolderByPath($parentPath);
  1976.                         // check for duplicate filename
  1977.                         $filename $this->getSafeFilename($parent->getRealFullPath(), $filename);
  1978.                         if ($parent->isAllowed('create')) {
  1979.                             $asset Asset::create($parent->getId(), [
  1980.                                 'filename' => $filename,
  1981.                                 'sourcePath' => $tmpFile,
  1982.                                 'userOwner' => $this->getAdminUser()->getId(),
  1983.                                 'userModification' => $this->getAdminUser()->getId(),
  1984.                             ]);
  1985.                             @unlink($tmpFile);
  1986.                         } else {
  1987.                             Logger::debug('prevented creating asset because of missing permissions');
  1988.                         }
  1989.                     }
  1990.                 }
  1991.             }
  1992.             $zip->close();
  1993.         }
  1994.         if ($request->get('last')) {
  1995.             unlink($zipFile);
  1996.         }
  1997.         return $this->adminJson([
  1998.             'success' => true,
  1999.         ]);
  2000.     }
  2001.     /**
  2002.      * @Route("/import-server", name="pimcore_admin_asset_importserver", methods={"POST"})
  2003.      *
  2004.      * @param Request $request
  2005.      *
  2006.      * @return JsonResponse
  2007.      */
  2008.     public function importServerAction(Request $request)
  2009.     {
  2010.         $success true;
  2011.         $filesPerJob 5;
  2012.         $jobs = [];
  2013.         $importDirectory str_replace('/fileexplorer'PIMCORE_PROJECT_ROOT$request->get('serverPath'));
  2014.         if (preg_match('@^' preg_quote(PIMCORE_PROJECT_ROOT'@') . '@'$importDirectory) && is_dir($importDirectory)) {
  2015.             $this->checkForPharStreamWrapper($importDirectory);
  2016.             $files rscandir($importDirectory '/');
  2017.             $count count($files);
  2018.             $jobFiles = [];
  2019.             for ($i 0$i $count$i++) {
  2020.                 if (is_dir($files[$i])) {
  2021.                     continue;
  2022.                 }
  2023.                 $jobFiles[] = preg_replace('@^' preg_quote($importDirectory'@') . '@'''$files[$i]);
  2024.                 if (count($jobFiles) >= $filesPerJob || $i >= ($count 1)) {
  2025.                     $relativeImportDirectory preg_replace('@^' preg_quote(PIMCORE_PROJECT_ROOT'@') . '@'''$importDirectory);
  2026.                     $jobs[] = [[
  2027.                         'url' => $this->generateUrl('pimcore_admin_asset_importserverfiles'),
  2028.                         'method' => 'POST',
  2029.                         'params' => [
  2030.                             'parentId' => $request->get('parentId'),
  2031.                             'serverPath' => $relativeImportDirectory,
  2032.                             'files' => implode('::'$jobFiles),
  2033.                         ],
  2034.                     ]];
  2035.                     $jobFiles = [];
  2036.                 }
  2037.             }
  2038.         }
  2039.         return $this->adminJson([
  2040.             'success' => $success,
  2041.             'jobs' => $jobs,
  2042.         ]);
  2043.     }
  2044.     /**
  2045.      * @Route("/import-server-files", name="pimcore_admin_asset_importserverfiles", methods={"POST"})
  2046.      *
  2047.      * @param Request $request
  2048.      *
  2049.      * @return JsonResponse
  2050.      */
  2051.     public function importServerFilesAction(Request $request)
  2052.     {
  2053.         $assetFolder Asset::getById((int) $request->get('parentId'));
  2054.         if (!$assetFolder) {
  2055.             throw $this->createNotFoundException('Parent asset not found');
  2056.         }
  2057.         $serverPath PIMCORE_PROJECT_ROOT $request->get('serverPath');
  2058.         $files explode('::'$request->get('files'));
  2059.         foreach ($files as $file) {
  2060.             $absolutePath $serverPath $file;
  2061.             $this->checkForPharStreamWrapper($absolutePath);
  2062.             if (is_file($absolutePath)) {
  2063.                 $relFolderPath str_replace('\\''/'dirname($file));
  2064.                 $folder Asset\Service::createFolderByPath($assetFolder->getRealFullPath() . $relFolderPath);
  2065.                 $filename basename($file);
  2066.                 // check for duplicate filename
  2067.                 $filename Element\Service::getValidKey($filename'asset');
  2068.                 $filename $this->getSafeFilename($folder->getRealFullPath(), $filename);
  2069.                 if ($assetFolder->isAllowed('create')) {
  2070.                     $asset Asset::create($folder->getId(), [
  2071.                         'filename' => $filename,
  2072.                         'sourcePath' => $absolutePath,
  2073.                         'userOwner' => $this->getAdminUser()->getId(),
  2074.                         'userModification' => $this->getAdminUser()->getId(),
  2075.                     ]);
  2076.                 } else {
  2077.                     Logger::debug('prevented creating asset because of missing permissions ');
  2078.                 }
  2079.             }
  2080.         }
  2081.         return $this->adminJson([
  2082.             'success' => true,
  2083.         ]);
  2084.     }
  2085.     protected function checkForPharStreamWrapper($path)
  2086.     {
  2087.         if (stripos($path'phar://') !== false) {
  2088.             throw $this->createAccessDeniedException('Using PHAR files is not allowed!');
  2089.         }
  2090.     }
  2091.     /**
  2092.      * @Route("/import-url", name="pimcore_admin_asset_importurl", methods={"POST"})
  2093.      *
  2094.      * @param Request $request
  2095.      *
  2096.      * @return JsonResponse
  2097.      *
  2098.      * @throws \Exception
  2099.      */
  2100.     public function importUrlAction(Request $request)
  2101.     {
  2102.         $success true;
  2103.         $data Tool::getHttpData($request->get('url'));
  2104.         $filename basename($request->get('url'));
  2105.         $parentId $request->get('id');
  2106.         $parentAsset Asset::getById((int)$parentId);
  2107.         if (!$parentAsset) {
  2108.             throw $this->createNotFoundException('Parent asset not found');
  2109.         }
  2110.         $filename Element\Service::getValidKey($filename'asset');
  2111.         $filename $this->getSafeFilename($parentAsset->getRealFullPath(), $filename);
  2112.         if (empty($filename)) {
  2113.             throw new \Exception('The filename of the asset is empty');
  2114.         }
  2115.         // check for duplicate filename
  2116.         $filename $this->getSafeFilename($parentAsset->getRealFullPath(), $filename);
  2117.         if ($parentAsset->isAllowed('create')) {
  2118.             $asset Asset::create($parentId, [
  2119.                 'filename' => $filename,
  2120.                 'data' => $data,
  2121.                 'userOwner' => $this->getAdminUser()->getId(),
  2122.                 'userModification' => $this->getAdminUser()->getId(),
  2123.             ]);
  2124.             $success true;
  2125.         } else {
  2126.             Logger::debug('prevented creating asset because of missing permissions');
  2127.         }
  2128.         return $this->adminJson(['success' => $success]);
  2129.     }
  2130.     /**
  2131.      * @Route("/clear-thumbnail", name="pimcore_admin_asset_clearthumbnail", methods={"POST"})
  2132.      *
  2133.      * @param Request $request
  2134.      *
  2135.      * @return JsonResponse
  2136.      */
  2137.     public function clearThumbnailAction(Request $request)
  2138.     {
  2139.         $success false;
  2140.         if ($asset Asset::getById((int) $request->get('id'))) {
  2141.             if (method_exists($asset'clearThumbnails')) {
  2142.                 if (!$asset->isAllowed('publish')) {
  2143.                     throw $this->createAccessDeniedException('not allowed to publish');
  2144.                 }
  2145.                 $asset->clearThumbnails(true); // force clear
  2146.                 $asset->save();
  2147.                 $success true;
  2148.             }
  2149.         }
  2150.         return $this->adminJson(['success' => $success]);
  2151.     }
  2152.     /**
  2153.      * @Route("/grid-proxy", name="pimcore_admin_asset_gridproxy", methods={"GET", "POST", "PUT"})
  2154.      *
  2155.      * @param Request $request
  2156.      * @param EventDispatcherInterface $eventDispatcher
  2157.      * @param GridHelperService $gridHelperService
  2158.      * @param CsrfProtectionHandler $csrfProtection
  2159.      *
  2160.      * @return JsonResponse
  2161.      */
  2162.     public function gridProxyAction(Request $requestEventDispatcherInterface $eventDispatcherGridHelperService $gridHelperServiceCsrfProtectionHandler $csrfProtection)
  2163.     {
  2164.         $allParams array_merge($request->request->all(), $request->query->all());
  2165.         $filterPrepareEvent = new GenericEvent($this, [
  2166.             'requestParams' => $allParams,
  2167.         ]);
  2168.         $language $request->get('language') != 'default' $request->get('language') : null;
  2169.         $eventDispatcher->dispatch($filterPrepareEventAdminEvents::ASSET_LIST_BEFORE_FILTER_PREPARE);
  2170.         $allParams $filterPrepareEvent->getArgument('requestParams');
  2171.         $loader \Pimcore::getContainer()->get('pimcore.implementation_loader.asset.metadata.data');
  2172.         if (isset($allParams['data']) && $allParams['data']) {
  2173.             $csrfProtection->checkCsrfToken($request);
  2174.             if ($allParams['xaction'] == 'update') {
  2175.                 try {
  2176.                     $data $this->decodeJson($allParams['data']);
  2177.                     $updateEvent = new GenericEvent($this, [
  2178.                         'data' => $data,
  2179.                         'processed' => false,
  2180.                     ]);
  2181.                     $eventDispatcher->dispatch($updateEventAdminEvents::ASSET_LIST_BEFORE_UPDATE);
  2182.                     $processed $updateEvent->getArgument('processed');
  2183.                     if ($processed) {
  2184.                         // update already processed by event handler
  2185.                         return $this->adminJson(['success' => true]);
  2186.                     }
  2187.                     $data $updateEvent->getArgument('data');
  2188.                     // save
  2189.                     $asset Asset::getById($data['id']);
  2190.                     if (!$asset) {
  2191.                         throw $this->createNotFoundException('Asset not found');
  2192.                     }
  2193.                     if (!$asset->isAllowed('publish')) {
  2194.                         throw $this->createAccessDeniedException("Permission denied. You don't have the rights to save this asset.");
  2195.                     }
  2196.                     $metadata $asset->getMetadata(nullnullfalsetrue);
  2197.                     $dirty false;
  2198.                     unset($data['id']);
  2199.                     foreach ($data as $key => $value) {
  2200.                         $fieldDef explode('~'$key);
  2201.                         $key $fieldDef[0];
  2202.                         if (isset($fieldDef[1])) {
  2203.                             $language = ($fieldDef[1] == 'none' '' $fieldDef[1]);
  2204.                         }
  2205.                         foreach ($metadata as $idx => &$em) {
  2206.                             if ($em['name'] == $key && $em['language'] == $language) {
  2207.                                 try {
  2208.                                     $dataImpl $loader->build($em['type']);
  2209.                                     $value $dataImpl->getDataFromListfolderGrid($value$em);
  2210.                                 } catch (UnsupportedException $le) {
  2211.                                     Logger::error('could not resolve metadata implementation for ' $em['type']);
  2212.                                 }
  2213.                                 $em['data'] = $value;
  2214.                                 $dirty true;
  2215.                                 break;
  2216.                             }
  2217.                         }
  2218.                         if (!$dirty) {
  2219.                             $defaulMetadata = ['title''alt''copyright'];
  2220.                             if (in_array($key$defaulMetadata)) {
  2221.                                 $newEm = [
  2222.                                     'name' => $key,
  2223.                                     'language' => $language,
  2224.                                     'type' => 'input',
  2225.                                     'data' => $value,
  2226.                                 ];
  2227.                                 try {
  2228.                                     $dataImpl $loader->build($newEm['type']);
  2229.                                     $newEm['data'] = $dataImpl->getDataFromListfolderGrid($value$newEm);
  2230.                                 } catch (UnsupportedException $le) {
  2231.                                     Logger::error('could not resolve metadata implementation for ' $newEm['type']);
  2232.                                 }
  2233.                                 $metadata[] = $newEm;
  2234.                                 $dirty true;
  2235.                             } else {
  2236.                                 $predefined Model\Metadata\Predefined::getByName($key);
  2237.                                 if ($predefined && (empty($predefined->getTargetSubtype())
  2238.                                         || $predefined->getTargetSubtype() == $asset->getType())) {
  2239.                                     $newEm = [
  2240.                                         'name' => $key,
  2241.                                         'language' => $language,
  2242.                                         'type' => $predefined->getType(),
  2243.                                         'data' => $value,
  2244.                                     ];
  2245.                                     try {
  2246.                                         $dataImpl $loader->build($newEm['type']);
  2247.                                         $newEm['data'] = $dataImpl->getDataFromListfolderGrid($value$newEm);
  2248.                                     } catch (UnsupportedException $le) {
  2249.                                         Logger::error('could not resolve metadata implementation for ' $newEm['type']);
  2250.                                     }
  2251.                                     $metadata[] = $newEm;
  2252.                                     $dirty true;
  2253.                                 }
  2254.                             }
  2255.                         }
  2256.                     }
  2257.                     if ($dirty) {
  2258.                         // $metadata = Asset\Service::minimizeMetadata($metadata, "grid");
  2259.                         $asset->setMetadataRaw($metadata);
  2260.                         $asset->save();
  2261.                         return $this->adminJson(['success' => true]);
  2262.                     }
  2263.                     return $this->adminJson(['success' => false'message' => 'something went wrong.']);
  2264.                 } catch (\Exception $e) {
  2265.                     return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  2266.                 }
  2267.             }
  2268.         } else {
  2269.             $list $gridHelperService->prepareAssetListingForGrid($allParams$this->getAdminUser());
  2270.             $beforeListLoadEvent = new GenericEvent($this, [
  2271.                 'list' => $list,
  2272.                 'context' => $allParams,
  2273.             ]);
  2274.             $eventDispatcher->dispatch($beforeListLoadEventAdminEvents::ASSET_LIST_BEFORE_LIST_LOAD);
  2275.             /** @var Asset\Listing $list */
  2276.             $list $beforeListLoadEvent->getArgument('list');
  2277.             $list->load();
  2278.             $assets = [];
  2279.             foreach ($list->getAssets() as $index => $asset) {
  2280.                 // 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
  2281.                 if ($asset->isAllowed('list')) {
  2282.                     $a Asset\Service::gridAssetData($asset$allParams['fields'], $allParams['language'] ?? '');
  2283.                     $assets[] = $a;
  2284.                 }
  2285.             }
  2286.             $result = ['data' => $assets'success' => true'total' => $list->getTotalCount()];
  2287.             $afterListLoadEvent = new GenericEvent($this, [
  2288.                 'list' => $result,
  2289.                 'context' => $allParams,
  2290.             ]);
  2291.             $eventDispatcher->dispatch($afterListLoadEventAdminEvents::ASSET_LIST_AFTER_LIST_LOAD);
  2292.             $result $afterListLoadEvent->getArgument('list');
  2293.             return $this->adminJson($result);
  2294.         }
  2295.         return $this->adminJson(['success' => false]);
  2296.     }
  2297.     /**
  2298.      * @Route("/get-text", name="pimcore_admin_asset_gettext", methods={"GET"})
  2299.      *
  2300.      * @param Request $request
  2301.      *
  2302.      * @return JsonResponse
  2303.      */
  2304.     public function getTextAction(Request $request)
  2305.     {
  2306.         $asset Asset::getById((int) $request->get('id'));
  2307.         if (!$asset) {
  2308.             throw $this->createNotFoundException('Asset not found');
  2309.         }
  2310.         if (!$asset->isAllowed('view')) {
  2311.             throw $this->createAccessDeniedException('not allowed to view');
  2312.         }
  2313.         $page $request->get('page');
  2314.         $text null;
  2315.         if ($asset instanceof Asset\Document) {
  2316.             $text $asset->getText($page);
  2317.         }
  2318.         return $this->adminJson(['success' => 'true''text' => $text]);
  2319.     }
  2320.     /**
  2321.      * @Route("/detect-image-features", name="pimcore_admin_asset_detectimagefeatures", methods={"GET"})
  2322.      *
  2323.      * @param Request $request
  2324.      *
  2325.      * @return JsonResponse
  2326.      */
  2327.     public function detectImageFeaturesAction(Request $request)
  2328.     {
  2329.         $asset Asset\Image::getById((int)$request->get('id'));
  2330.         if (!$asset instanceof Asset) {
  2331.             return $this->adminJson(['success' => false'message' => "asset doesn't exist"]);
  2332.         }
  2333.         if ($asset->isAllowed('publish')) {
  2334.             $asset->detectFaces();
  2335.             $asset->removeCustomSetting('disableImageFeatureAutoDetection');
  2336.             $asset->save();
  2337.             return $this->adminJson(['success' => true]);
  2338.         }
  2339.         throw $this->createAccessDeniedHttpException();
  2340.     }
  2341.     /**
  2342.      * @Route("/delete-image-features", name="pimcore_admin_asset_deleteimagefeatures", methods={"GET"})
  2343.      *
  2344.      * @param Request $request
  2345.      *
  2346.      * @return JsonResponse
  2347.      */
  2348.     public function deleteImageFeaturesAction(Request $request)
  2349.     {
  2350.         $asset Asset::getById((int)$request->get('id'));
  2351.         if (!$asset instanceof Asset) {
  2352.             return $this->adminJson(['success' => false'message' => "asset doesn't exist"]);
  2353.         }
  2354.         if ($asset->isAllowed('publish')) {
  2355.             $asset->removeCustomSetting('faceCoordinates');
  2356.             $asset->setCustomSetting('disableImageFeatureAutoDetection'true);
  2357.             $asset->save();
  2358.             return $this->adminJson(['success' => true]);
  2359.         }
  2360.         throw $this->createAccessDeniedHttpException();
  2361.     }
  2362.     /**
  2363.      * @param ControllerEvent $event
  2364.      */
  2365.     public function onKernelControllerEvent(ControllerEvent $event)
  2366.     {
  2367.         if (!$event->isMainRequest()) {
  2368.             return;
  2369.         }
  2370.         $this->checkActionPermission($event'assets', [
  2371.             'getImageThumbnailAction''getVideoThumbnailAction''getDocumentThumbnailAction',
  2372.         ]);
  2373.         $this->_assetService = new Asset\Service($this->getAdminUser());
  2374.     }
  2375. }