Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Upload.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
19  protected $resultJsonFactory;
20 
24  private $directoryResolver;
25 
32  public function __construct(
33  \Magento\Backend\App\Action\Context $context,
34  \Magento\Framework\Registry $coreRegistry,
35  \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
36  \Magento\Framework\App\Filesystem\DirectoryResolver $directoryResolver = null
37  ) {
38  parent::__construct($context, $coreRegistry);
39  $this->resultJsonFactory = $resultJsonFactory;
40  $this->directoryResolver = $directoryResolver
41  ?: $this->_objectManager->get(\Magento\Framework\App\Filesystem\DirectoryResolver::class);
42  }
43 
50  public function execute()
51  {
52  try {
53  $this->_initAction();
54  $path = $this->getStorage()->getSession()->getCurrentPath();
55  if (!$this->directoryResolver->validatePath($path, DirectoryList::MEDIA)) {
56  throw new \Magento\Framework\Exception\LocalizedException(
57  __('Directory %1 is not under storage root path.', $path)
58  );
59  }
60  $result = $this->getStorage()->uploadFile($path, $this->getRequest()->getParam('type'));
61  } catch (\Exception $e) {
62  $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
63  }
65  $resultJson = $this->resultJsonFactory->create();
66 
67  return $resultJson->setData($result);
68  }
69 }
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, \Magento\Framework\App\Filesystem\DirectoryResolver $directoryResolver=null)
Definition: Upload.php:32
__()
Definition: __.php:13