Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
File.php
Go to the documentation of this file.
1 <?php
7 
12 
21 class File extends \Magento\Framework\App\Config\Value
22 {
26  protected $_requestData;
27 
33  protected $_maxFileSize = 0;
34 
38  protected $_filesystem;
39 
43  protected $_mediaDirectory;
44 
48  protected $_uploaderFactory;
49 
62  public function __construct(
63  \Magento\Framework\Model\Context $context,
64  \Magento\Framework\Registry $registry,
66  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
67  \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
68  \Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface $requestData,
70  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
71  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
72  array $data = []
73  ) {
74  $this->_uploaderFactory = $uploaderFactory;
75  $this->_requestData = $requestData;
76  $this->_filesystem = $filesystem;
77  $this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
78  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
79  }
80 
87  public function beforeSave()
88  {
89  $value = $this->getValue();
90  $file = $this->getFileData();
91  if (!empty($file)) {
92  $uploadDir = $this->_getUploadDir();
93  try {
95  $uploader = $this->_uploaderFactory->create(['fileId' => $file]);
96  $uploader->setAllowedExtensions($this->_getAllowedExtensions());
97  $uploader->setAllowRenameFiles(true);
98  $uploader->addValidateCallback('size', $this, 'validateMaxSize');
99  $result = $uploader->save($uploadDir);
100  } catch (\Exception $e) {
101  throw new \Magento\Framework\Exception\LocalizedException(__('%1', $e->getMessage()));
102  }
103 
104  $filename = $result['file'];
105  if ($filename) {
106  if ($this->_addWhetherScopeInfo()) {
107  $filename = $this->_prependScopeInfo($filename);
108  }
109  $this->setValue($filename);
110  }
111  } else {
112  if (is_array($value) && !empty($value['delete'])) {
113  $this->setValue('');
114  } elseif (is_array($value) && !empty($value['value'])) {
115  $this->setValue($value['value']);
116  } else {
117  $this->unsValue();
118  }
119  }
120 
121  return $this;
122  }
123 
130  protected function getFileData()
131  {
132  $file = [];
133  $value = $this->getValue();
134  $tmpName = $this->_requestData->getTmpName($this->getPath());
135  if ($tmpName) {
136  $file['tmp_name'] = $tmpName;
137  $file['name'] = $this->_requestData->getName($this->getPath());
138  } elseif (!empty($value['tmp_name'])) {
139  $file['tmp_name'] = $value['tmp_name'];
140  $file['name'] = isset($value['value']) ? $value['value'] : $value['name'];
141  }
142 
143  return $file;
144  }
145 
153  public function validateMaxSize($filePath)
154  {
155  $directory = $this->_filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
156  if ($this->_maxFileSize > 0 && $directory->stat(
157  $directory->getRelativePath($filePath)
158  )['size'] > $this->_maxFileSize * 1024
159  ) {
160  throw new \Magento\Framework\Exception\LocalizedException(
161  __('The file you\'re uploading exceeds the server size limit of %1 kilobytes.', $this->_maxFileSize)
162  );
163  }
164  }
165 
171  protected function _addWhetherScopeInfo()
172  {
173  $fieldConfig = $this->getFieldConfig();
174  $dirParams = array_key_exists('upload_dir', $fieldConfig) ? $fieldConfig['upload_dir'] : [];
175  return is_array($dirParams) && array_key_exists('scope_info', $dirParams) && $dirParams['scope_info'];
176  }
177 
184  protected function _getUploadDir()
185  {
186  $fieldConfig = $this->getFieldConfig();
187 
188  if (!array_key_exists('upload_dir', $fieldConfig)) {
189  throw new \Magento\Framework\Exception\LocalizedException(
190  __('The base directory to upload file is not specified.')
191  );
192  }
193 
194  if (is_array($fieldConfig['upload_dir'])) {
195  $uploadDir = $fieldConfig['upload_dir']['value'];
196  if (array_key_exists('scope_info', $fieldConfig['upload_dir'])
197  && $fieldConfig['upload_dir']['scope_info']
198  ) {
199  $uploadDir = $this->_appendScopeInfo($uploadDir);
200  }
201 
202  if (array_key_exists('config', $fieldConfig['upload_dir'])) {
203  $uploadDir = $this->getUploadDirPath($uploadDir);
204  }
205  } else {
206  $uploadDir = (string)$fieldConfig['upload_dir'];
207  }
208 
209  return $uploadDir;
210  }
211 
219  protected function getUploadDirPath($uploadDir)
220  {
221  return $this->_mediaDirectory->getAbsolutePath($uploadDir);
222  }
223 
232  protected function _prependScopeInfo($path)
233  {
234  $scopeInfo = $this->getScope();
235  if (ScopeConfigInterface::SCOPE_TYPE_DEFAULT != $this->getScope()) {
236  $scopeInfo .= '/' . $this->getScopeId();
237  }
238  return $scopeInfo . '/' . $path;
239  }
240 
249  protected function _appendScopeInfo($path)
250  {
251  $path .= '/' . $this->getScope();
252  if (ScopeConfigInterface::SCOPE_TYPE_DEFAULT != $this->getScope()) {
253  $path .= '/' . $this->getScopeId();
254  }
255  return $path;
256  }
257 
263  protected function _getAllowedExtensions()
264  {
265  return [];
266  }
267 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
$filesystem
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory, \Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface $requestData, Filesystem $filesystem, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: File.php:62