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
7 
10 
17 {
21  protected $_link;
22 
26  protected $_sample;
27 
33  protected $_fileHelper;
34 
38  private $uploaderFactory;
39 
43  private $storageDatabase;
44 
56  public function __construct(
57  \Magento\Backend\App\Action\Context $context,
58  \Magento\Downloadable\Model\Link $link,
59  \Magento\Downloadable\Model\Sample $sample,
60  \Magento\Downloadable\Helper\File $fileHelper,
61  \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
62  \Magento\MediaStorage\Helper\File\Storage\Database $storageDatabase
63  ) {
64  parent::__construct($context);
65  $this->_link = $link;
66  $this->_sample = $sample;
67  $this->_fileHelper = $fileHelper;
68  $this->uploaderFactory = $uploaderFactory;
69  $this->storageDatabase = $storageDatabase;
70  }
71 
77  public function execute()
78  {
79  $type = $this->getRequest()->getParam('type');
80  $tmpPath = '';
81  if ($type == 'samples') {
82  $tmpPath = $this->_sample->getBaseTmpPath();
83  } elseif ($type == 'links') {
84  $tmpPath = $this->_link->getBaseTmpPath();
85  } elseif ($type == 'link_samples') {
86  $tmpPath = $this->_link->getBaseSampleTmpPath();
87  }
88 
89  try {
90  $uploader = $this->uploaderFactory->create(['fileId' => $type]);
91 
92  $result = $this->_fileHelper->uploadFromTmp($tmpPath, $uploader);
93 
94  if (!$result) {
95  throw new \Exception('File can not be moved from temporary folder to the destination folder.');
96  }
97 
98  unset($result['tmp_name'], $result['path']);
99 
100  if (isset($result['file'])) {
101  $relativePath = rtrim($tmpPath, '/') . '/' . ltrim($result['file'], '/');
102  $this->storageDatabase->saveFile($relativePath);
103  }
104  } catch (\Exception $e) {
105  $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
106  }
107 
108  return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
109  }
110 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Downloadable\Model\Link $link, \Magento\Downloadable\Model\Sample $sample, \Magento\Downloadable\Helper\File $fileHelper, \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory, \Magento\MediaStorage\Helper\File\Storage\Database $storageDatabase)
Definition: Upload.php:56
$type
Definition: item.phtml:13
$relativePath
Definition: get.php:35