DeleteFolder.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;
  8. use Magento\Framework\App\Action\HttpPostActionInterface;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. /**
  11. * Delete image folder.
  12. */
  13. class DeleteFolder extends \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images implements HttpPostActionInterface
  14. {
  15. /**
  16. * @var \Magento\Framework\Controller\Result\JsonFactory
  17. */
  18. protected $resultJsonFactory;
  19. /**
  20. * @var \Magento\Framework\Controller\Result\RawFactory
  21. */
  22. protected $resultRawFactory;
  23. /**
  24. * @var \Magento\Framework\App\Filesystem\DirectoryResolver
  25. */
  26. private $directoryResolver;
  27. /**
  28. * @param \Magento\Backend\App\Action\Context $context
  29. * @param \Magento\Framework\Registry $coreRegistry
  30. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  31. * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
  32. * @param \Magento\Framework\App\Filesystem\DirectoryResolver|null $directoryResolver
  33. */
  34. public function __construct(
  35. \Magento\Backend\App\Action\Context $context,
  36. \Magento\Framework\Registry $coreRegistry,
  37. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
  38. \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
  39. \Magento\Framework\App\Filesystem\DirectoryResolver $directoryResolver = null
  40. ) {
  41. parent::__construct($context, $coreRegistry);
  42. $this->resultRawFactory = $resultRawFactory;
  43. $this->resultJsonFactory = $resultJsonFactory;
  44. $this->directoryResolver = $directoryResolver
  45. ?: $this->_objectManager->get(\Magento\Framework\App\Filesystem\DirectoryResolver::class);
  46. }
  47. /**
  48. * Delete folder action.
  49. *
  50. * @return \Magento\Framework\Controller\ResultInterface
  51. * @throws \Magento\Framework\Exception\LocalizedException
  52. */
  53. public function execute()
  54. {
  55. try {
  56. $path = $this->getStorage()->getCmsWysiwygImages()->getCurrentPath();
  57. if (!$this->directoryResolver->validatePath($path, DirectoryList::MEDIA)) {
  58. throw new \Magento\Framework\Exception\LocalizedException(
  59. __('Directory %1 is not under storage root path.', $path)
  60. );
  61. }
  62. $this->getStorage()->deleteDirectory($path);
  63. return $this->resultRawFactory->create();
  64. } catch (\Exception $e) {
  65. $result = ['error' => true, 'message' => $e->getMessage()];
  66. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  67. $resultJson = $this->resultJsonFactory->create();
  68. return $resultJson->setData($result);
  69. }
  70. }
  71. }