Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Storage.php
Go to the documentation of this file.
1 <?php
11 
13 
19 class Storage
20 {
24  const TYPE_FONT = 'font';
25 
29  const TYPE_IMAGE = 'image';
30 
34  const THUMBNAIL_DIRECTORY = '.thumbnail';
35 
39  const THUMBNAIL_WIDTH = 100;
40 
44  const THUMBNAIL_HEIGHT = 100;
45 
49  const DIRECTORY_NAME_REGEXP = '/^[a-z0-9\-\_]+$/si';
50 
56  protected $_helper;
57 
61  protected $_objectManager;
62 
66  protected $_imageFactory;
67 
72 
76  protected $urlEncoder;
77 
81  protected $urlDecoder;
82 
93  public function __construct(
94  \Magento\Framework\Filesystem $filesystem,
95  \Magento\Theme\Helper\Storage $helper,
96  \Magento\Framework\ObjectManagerInterface $objectManager,
97  \Magento\Framework\Image\AdapterFactory $imageFactory,
98  \Magento\Framework\Url\EncoderInterface $urlEncoder,
99  \Magento\Framework\Url\DecoderInterface $urlDecoder
100  ) {
101  $this->mediaWriteDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
102  $this->_helper = $helper;
103  $this->_objectManager = $objectManager;
104  $this->_imageFactory = $imageFactory;
105  $this->urlEncoder = $urlEncoder;
106  $this->urlDecoder = $urlDecoder;
107  }
108 
116  public function uploadFile($targetPath)
117  {
119  $uploader = $this->_objectManager->create(
120  \Magento\MediaStorage\Model\File\Uploader::class,
121  ['fileId' => 'file']
122  );
123  $uploader->setAllowedExtensions($this->_helper->getAllowedExtensionsByType());
124  $uploader->setAllowRenameFiles(true);
125  $uploader->setFilesDispersion(false);
126  $result = $uploader->save($targetPath);
127  unset($result['path']);
128 
129  if (!$result) {
130  throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t upload the file right now.'));
131  }
132 
133  $this->_createThumbnail($targetPath . '/' . $uploader->getUploadedFileName());
134 
135  return $result;
136  }
137 
144  public function _createThumbnail($source)
145  {
146  if (self::TYPE_IMAGE != $this->_helper->getStorageType() || !$this->mediaWriteDirectory->isFile(
147  $source
148  ) || !$this->mediaWriteDirectory->isReadable(
149  $source
150  )
151  ) {
152  return false;
153  }
154  $thumbnailDir = $this->_helper->getThumbnailDirectory($source);
155  $thumbnailPath = $thumbnailDir . '/' . pathinfo($source, PATHINFO_BASENAME);
156  try {
157  $this->mediaWriteDirectory->isExist($thumbnailDir);
158  $image = $this->_imageFactory->create();
159  $image->open($this->mediaWriteDirectory->getAbsolutePath($source));
160  $image->keepAspectRatio(true);
161  $image->resize(self::THUMBNAIL_WIDTH, self::THUMBNAIL_HEIGHT);
162  $image->save($this->mediaWriteDirectory->getAbsolutePath($thumbnailPath));
163  } catch (\Magento\Framework\Exception\FileSystemException $e) {
164  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
165  return false;
166  }
167 
168  if ($this->mediaWriteDirectory->isFile($thumbnailPath)) {
169  return $thumbnailPath;
170  }
171  return false;
172  }
173 
182  public function createFolder($name, $path)
183  {
184  if (!preg_match(self::DIRECTORY_NAME_REGEXP, $name)) {
185  throw new \Magento\Framework\Exception\LocalizedException(
186  __('Use only standard alphanumeric, dashes and underscores.')
187  );
188  }
189  if (!$this->mediaWriteDirectory->isWritable($path)) {
190  $path = $this->_helper->getStorageRoot();
191  }
192 
193  $newPath = $path . '/' . $name;
194 
195  if ($this->mediaWriteDirectory->isExist($newPath)) {
196  throw new \Magento\Framework\Exception\LocalizedException(__('We found a directory with the same name.'));
197  }
198 
199  $this->mediaWriteDirectory->create($newPath);
200 
201  $result = [
202  'name' => $name,
203  'short_name' => $this->_helper->getShortFilename($name),
204  'path' => str_replace($this->_helper->getStorageRoot(), '', $newPath),
205  'id' => $this->_helper->convertPathToId($newPath)
206  ];
207 
208  return $result;
209  }
210 
217  public function deleteFile($file)
218  {
219  $file = $this->urlDecoder->decode($file);
220  $path = $this->mediaWriteDirectory->getRelativePath($this->_helper->getCurrentPath());
221 
222  $filePath = $this->mediaWriteDirectory->getRelativePath($path . '/' . $file);
223  $thumbnailPath = $this->_helper->getThumbnailDirectory($filePath) . '/' . $file;
224 
225  if (0 === strpos($filePath, $path) && 0 === strpos($filePath, $this->_helper->getStorageRoot())) {
226  $this->mediaWriteDirectory->delete($filePath);
227  $this->mediaWriteDirectory->delete($thumbnailPath);
228  }
229  return $this;
230  }
231 
239  public function getDirsCollection($currentPath)
240  {
241  if (!$this->mediaWriteDirectory->isExist($currentPath)) {
242  throw new \Magento\Framework\Exception\LocalizedException(__('We cannot find a directory with this name.'));
243  }
244  $paths = $this->mediaWriteDirectory->search('.*', $currentPath);
245  $directories = [];
246  foreach ($paths as $path) {
247  if ($this->mediaWriteDirectory->isDirectory($path)) {
248  $directories[] = $path;
249  }
250  }
251  return $directories;
252  }
253 
259  public function getFilesCollection()
260  {
261  $paths = $this->mediaWriteDirectory->search('.*', $this->_helper->getCurrentPath());
262  $files = [];
263  $requestParams = $this->_helper->getRequestParams();
264  $storageType = $this->_helper->getStorageType();
265  foreach ($paths as $path) {
266  if (!$this->mediaWriteDirectory->isFile($path)) {
267  continue;
268  }
269  $fileName = pathinfo($path, PATHINFO_BASENAME);
270  $file = ['text' => $fileName, 'id' => $this->urlEncoder->encode($fileName)];
271  if (self::TYPE_IMAGE == $storageType) {
272  $requestParams['file'] = $fileName;
273  $file['thumbnailParams'] = $requestParams;
274 
275  $size = @getimagesize($path);
276  if (is_array($size)) {
277  $file['width'] = $size[0];
278  $file['height'] = $size[1];
279  }
280  }
281  $files[] = $file;
282  }
283  return $files;
284  }
285 
291  public function getTreeArray()
292  {
293  $directories = $this->getDirsCollection($this->_helper->getCurrentPath());
294  $resultArray = [];
295  foreach ($directories as $path) {
296  $resultArray[] = [
297  'text' => $this->_helper->getShortFilename(pathinfo($path, PATHINFO_BASENAME), 20),
298  'id' => $this->_helper->convertPathToId($path),
299  'cls' => 'folder'
300  ];
301  }
302  return $resultArray;
303  }
304 
312  public function deleteDirectory($path)
313  {
314  $rootCmp = rtrim($this->_helper->getStorageRoot(), '/');
315  $pathCmp = rtrim($path, '/');
316 
317  if ($rootCmp == $pathCmp) {
318  throw new \Magento\Framework\Exception\LocalizedException(
319  __('We can\'t delete root directory %1 right now.', $path)
320  );
321  }
322 
323  return $this->mediaWriteDirectory->delete($path);
324  }
325 }
$helper
Definition: iframe.phtml:13
$objectManager
Definition: bootstrap.php:17
$source
Definition: source.php:23
__()
Definition: __.php:13
$fileName
Definition: translate.phtml:15
__construct(\Magento\Framework\Filesystem $filesystem, \Magento\Theme\Helper\Storage $helper, \Magento\Framework\ObjectManagerInterface $objectManager, \Magento\Framework\Image\AdapterFactory $imageFactory, \Magento\Framework\Url\EncoderInterface $urlEncoder, \Magento\Framework\Url\DecoderInterface $urlDecoder)
Definition: Storage.php:93
$paths
Definition: _bootstrap.php:83
$filesystem
foreach($appDirs as $dir) $files
if(!isset($_GET['name'])) $name
Definition: log.php:14