Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageUploader.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Model;
7 
12 {
19 
25  protected $mediaDirectory;
26 
32  private $uploaderFactory;
33 
39  protected $storeManager;
40 
44  protected $logger;
45 
51  protected $baseTmpPath;
52 
58  protected $basePath;
59 
65  protected $allowedExtensions;
66 
72  private $allowedMimeTypes = [
73  'image/jpg',
74  'image/jpeg',
75  'image/gif',
76  'image/png',
77  ];
78 
91  public function __construct(
92  \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase,
93  \Magento\Framework\Filesystem $filesystem,
94  \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
96  \Psr\Log\LoggerInterface $logger,
98  $basePath,
100  ) {
101  $this->coreFileStorageDatabase = $coreFileStorageDatabase;
102  $this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
103  $this->uploaderFactory = $uploaderFactory;
104  $this->storeManager = $storeManager;
105  $this->logger = $logger;
106  $this->baseTmpPath = $baseTmpPath;
107  $this->basePath = $basePath;
108  $this->allowedExtensions = $allowedExtensions;
109  }
110 
118  public function setBaseTmpPath($baseTmpPath)
119  {
120  $this->baseTmpPath = $baseTmpPath;
121  }
122 
130  public function setBasePath($basePath)
131  {
132  $this->basePath = $basePath;
133  }
134 
143  {
144  $this->allowedExtensions = $allowedExtensions;
145  }
146 
152  public function getBaseTmpPath()
153  {
154  return $this->baseTmpPath;
155  }
156 
162  public function getBasePath()
163  {
164  return $this->basePath;
165  }
166 
172  public function getAllowedExtensions()
173  {
175  }
176 
185  public function getFilePath($path, $imageName)
186  {
187  return rtrim($path, '/') . '/' . ltrim($imageName, '/');
188  }
189 
199  public function moveFileFromTmp($imageName)
200  {
201  $baseTmpPath = $this->getBaseTmpPath();
202  $basePath = $this->getBasePath();
203 
204  $baseImagePath = $this->getFilePath($basePath, $imageName);
205  $baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
206 
207  try {
208  $this->coreFileStorageDatabase->copyFile(
209  $baseTmpImagePath,
210  $baseImagePath
211  );
212  $this->mediaDirectory->renameFile(
213  $baseTmpImagePath,
214  $baseImagePath
215  );
216  } catch (\Exception $e) {
217  throw new \Magento\Framework\Exception\LocalizedException(
218  __('Something went wrong while saving the file(s).')
219  );
220  }
221 
222  return $imageName;
223  }
224 
234  public function saveFileToTmpDir($fileId)
235  {
236  $baseTmpPath = $this->getBaseTmpPath();
237 
239  $uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
240  $uploader->setAllowedExtensions($this->getAllowedExtensions());
241  $uploader->setAllowRenameFiles(true);
242  if (!$uploader->checkMimeType($this->allowedMimeTypes)) {
243  throw new \Magento\Framework\Exception\LocalizedException(__('File validation failed.'));
244  }
245  $result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
246  unset($result['path']);
247 
248  if (!$result) {
249  throw new \Magento\Framework\Exception\LocalizedException(
250  __('File can not be saved to the destination folder.')
251  );
252  }
253 
257  $result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']);
258  $result['url'] = $this->storeManager
259  ->getStore()
260  ->getBaseUrl(
261  \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
262  ) . $this->getFilePath($baseTmpPath, $result['file']);
263  $result['name'] = $result['file'];
264 
265  if (isset($result['file'])) {
266  try {
267  $relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
268  $this->coreFileStorageDatabase->saveFile($relativePath);
269  } catch (\Exception $e) {
270  $this->logger->critical($e);
271  throw new \Magento\Framework\Exception\LocalizedException(
272  __('Something went wrong while saving the file(s).')
273  );
274  }
275  }
276 
277  return $result;
278  }
279 }
setAllowedExtensions($allowedExtensions)
__()
Definition: __.php:13
__construct(\Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase, \Magento\Framework\Filesystem $filesystem, \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Psr\Log\LoggerInterface $logger, $baseTmpPath, $basePath, $allowedExtensions)
$relativePath
Definition: get.php:35
$filesystem