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 
21 use Magento\MediaStorage\Model\File\UploaderFactory;
23 
27 class File extends BackendFile
28 {
32  protected $urlBuilder;
33 
37  private $mime;
38 
53  public function __construct(
54  Context $context,
58  UploaderFactory $uploaderFactory,
63  AbstractDb $resourceCollection = null,
64  array $data = []
65  ) {
66  parent::__construct(
67  $context,
68  $registry,
69  $config,
71  $uploaderFactory,
74  $resource,
75  $resourceCollection,
76  $data
77  );
78  $this->urlBuilder = $urlBuilder;
79  }
80 
87  public function beforeSave()
88  {
89  $values = $this->getValue();
90  $value = reset($values) ?: [];
91  if (!isset($value['file'])) {
92  throw new LocalizedException(
93  __('%1 does not contain field \'file\'', $this->getData('field_config/field'))
94  );
95  }
96  if (isset($value['exists'])) {
97  $this->setValue($value['file']);
98  return $this;
99  }
100 
101  $filename = basename($value['file']);
102  $result = $this->_mediaDirectory->copyFile(
103  $this->getTmpMediaPath($filename),
104  $this->_getUploadDir() . '/' . $filename
105  );
106  if ($result) {
107  $this->_mediaDirectory->delete($this->getTmpMediaPath($filename));
108  if ($this->_addWhetherScopeInfo()) {
109  $filename = $this->_prependScopeInfo($filename);
110  }
111  $this->setValue($filename);
112  } else {
113  $this->unsValue();
114  }
115 
116  return $this;
117  }
118 
122  public function afterLoad()
123  {
124  $value = $this->getValue();
125  if ($value && !is_array($value)) {
126  $fileName = $this->_getUploadDir() . '/' . basename($value);
127  $fileInfo = null;
128  if ($this->_mediaDirectory->isExist($fileName)) {
129  $stat = $this->_mediaDirectory->stat($fileName);
130  $url = $this->getStoreMediaUrl($value);
131  $fileInfo = [
132  [
133  'url' => $url,
134  'file' => $value,
135  'size' => is_array($stat) ? $stat['size'] : 0,
136  'name' => basename($value),
137  'type' => $this->getMimeType($fileName),
138  'exists' => true,
139  ]
140  ];
141  }
142  $this->setValue($fileInfo);
143  }
144  return $this;
145  }
146 
152  public function getAllowedExtensions()
153  {
154  return [];
155  }
156 
163  protected function getUploadDirPath($uploadDir)
164  {
165  return $this->_mediaDirectory->getRelativePath($uploadDir);
166  }
167 
171  public function getValue()
172  {
173  return $this->getData('value') ?: [];
174  }
175 
182  protected function getStoreMediaUrl($fileName)
183  {
184  $fieldConfig = $this->getFieldConfig();
185  $baseUrl = '';
186  $urlType = ['_type' => UrlInterface::URL_TYPE_MEDIA];
187  if (isset($fieldConfig['base_url'])) {
188  $baseUrl = $fieldConfig['base_url'];
189  $urlType = ['_type' => empty($baseUrl['type']) ? 'link' : (string)$baseUrl['type']];
190  $baseUrl = $baseUrl['value'] . '/';
191  }
192  return $this->urlBuilder->getBaseUrl($urlType) . $baseUrl . $fileName;
193  }
194 
201  protected function getTmpMediaPath($filename)
202  {
203  return 'tmp/' . FileProcessor::FILE_DIR . '/' . $filename;
204  }
205 
212  private function getMimeType($fileName)
213  {
214  $absoluteFilePath = $this->_mediaDirectory->getAbsolutePath($fileName);
215 
216  $result = $this->getMime()->getMimeType($absoluteFilePath);
217  return $result;
218  }
219 
227  private function getMime()
228  {
229  if ($this->mime === null) {
230  $this->mime = ObjectManager::getInstance()->get(Mime::class);
231  }
232  return $this->mime;
233  }
234 }
getData($key='', $index=null)
Definition: DataObject.php:119
$config
Definition: fraud_order.php:17
$values
Definition: options.phtml:88
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$fileName
Definition: translate.phtml:15
$value
Definition: gender.phtml:16
__construct(Context $context, Registry $registry, ScopeConfigInterface $config, TypeListInterface $cacheTypeList, UploaderFactory $uploaderFactory, RequestDataInterface $requestData, Filesystem $filesystem, UrlInterface $urlBuilder, AbstractResource $resource=null, AbstractDb $resourceCollection=null, array $data=[])
Definition: File.php:53
$filesystem