/** * Create thumbnail for image and save it to thumbnails directory * * @param string $source Image path to be resized * @param bool $keepRation Keep aspect ratio or not * @return bool|string Resized filepath or false if errors were occurred */ public function resizeFile($source, $keepRation = true) { $realPath = $this->_directory->getRelativePath($source); if (!$this->_directory->isFile($realPath) || !$this->_directory->isExist($realPath)) { return false; } $targetDir = $this->getThumbsPath($source); $pathTargetDir = $this->_directory->getRelativePath($targetDir); if (!$this->_directory->isExist($pathTargetDir)) { $this->_directory->create($pathTargetDir); } if (!$this->_directory->isExist($pathTargetDir)) { return false; } $image = $this->_imageFactory->create(); $image->open($source); $image->keepAspectRatio($keepRation); $image->resize($this->_resizeParameters['width'], $this->_resizeParameters['height']); $dest = $targetDir . '/' . pathinfo($source, PATHINFO_BASENAME); $image->save($dest); if ($this->_directory->isFile($this->_directory->getRelativePath($dest))) { return $dest; } return false; }
/** * Copy file to tmp theme customization path * * @param string $sourceFile * @return string */ protected function _copyFileToTmpCustomizationPath($sourceFile) { $targetFile = $this->_helperStorage->getStorageRoot() . '/' . basename($sourceFile); $this->directoryTmp->create(pathinfo($targetFile, PATHINFO_DIRNAME)); $this->directoryVar->copyFile($this->directoryVar->getRelativePath($sourceFile), $this->directoryTmp->getRelativePath($targetFile), $this->directoryTmp); return $targetFile; }
/** * Return URL based on current selected directory or root directory for startup * * @return string */ public function getCurrentUrl() { if (!$this->_currentUrl) { $path = $this->getCurrentPath(); $mediaUrl = $this->_storeManager->getStore($this->_storeId)->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); $this->_currentUrl = $mediaUrl . $this->_directory->getRelativePath($path) . '/'; } return $this->_currentUrl; }
/** * @param string $pathId * @param string $expectedPath * @param bool $isExist * @dataProvider providerGetCurrentPath */ public function testGetCurrentPath($pathId, $expectedPath, $isExist) { $this->requestMock->expects($this->once())->method('getParam')->willReturn($pathId); $this->directoryWriteMock->expects($this->any())->method('isDirectory')->willReturnMap([['/../wysiwyg/test_path', true], ['/../wysiwyg/my.jpg', false], ['/../wysiwyg', true]]); $this->directoryWriteMock->expects($this->any())->method('getRelativePath')->willReturnMap([['PATH/wysiwyg/test_path', '/../wysiwyg/test_path'], ['PATH/wysiwyg/my.jpg', '/../wysiwyg/my.jpg'], ['PATH/wysiwyg', '/../wysiwyg']]); $this->directoryWriteMock->expects($this->once())->method('isExist')->willReturn($isExist); $this->directoryWriteMock->expects($this->any())->method('create')->with($this->directoryWriteMock->getRelativePath($expectedPath)); $this->assertEquals($expectedPath, $this->imagesHelper->getCurrentPath()); }
/** * Open file and detect column names * * There must be column names in the first line * * @param string $file * @param \Magento\Framework\Filesystem\Directory\Write $directory * @param string $delimiter * @param string $enclosure * @throws \LogicException */ public function __construct($file, \Magento\Framework\Filesystem\Directory\Write $directory, $delimiter = ',', $enclosure = '"') { try { $this->_file = $directory->openFile($directory->getRelativePath($file), 'r'); } catch (\Magento\Framework\Filesystem\FilesystemException $e) { throw new \LogicException("Unable to open file: '{$file}'"); } $this->_delimiter = $delimiter; $this->_enclosure = $enclosure; parent::__construct($this->_getNextRow()); }
/** * Delete file * * @param string $file * @return \Magento\Theme\Model\Wysiwyg\Storage */ public function deleteFile($file) { $file = $this->urlDecoder->decode($file); $path = $this->mediaWriteDirectory->getRelativePath($this->_helper->getCurrentPath()); $filePath = $this->mediaWriteDirectory->getRelativePath($path . '/' . $file); $thumbnailPath = $this->_helper->getThumbnailDirectory($filePath) . '/' . $file; if (0 === strpos($filePath, $path) && 0 === strpos($filePath, $this->_helper->getStorageRoot())) { $this->mediaWriteDirectory->delete($filePath); $this->mediaWriteDirectory->delete($thumbnailPath); } return $this; }
/** * Get all folders as options array * * @return array */ public function collectFolders() { $collectFiles = $this->_collectFiles; $collectDirs = $this->_collectDirs; $this->setCollectFiles(false)->setCollectDirs(true); $this->_collectRecursive($this->connectDirectory->getAbsolutePath('connect')); $result = array('/' => '/'); foreach ($this->_collectedDirs as $dir) { $dir = substr($this->connectDirectory->getRelativePath($dir), strlen('connect/')) . '/'; $result[$dir] = $dir; } $this->setCollectFiles($collectFiles)->setCollectDirs($collectDirs); return $result; }
/** * Get current path * * @return string */ public function getCurrentPath() { if (!$this->_currentPath) { $currentPath = $this->getStorageRoot(); $path = $this->_getRequest()->getParam(self::PARAM_NODE); if ($path && $path !== self::NODE_ROOT) { $path = $this->convertIdToPath($path); if ($this->mediaDirectoryWrite->isDirectory($path) && 0 === strpos($path, $currentPath)) { $currentPath = $this->mediaDirectoryWrite->getRelativePath($path); } } $this->_currentPath = $currentPath; } return $this->_currentPath; }
/** * @param string $file * @param \Magento\Framework\Filesystem\Directory\Write $directory * @param string $options */ public function __construct($file, \Magento\Framework\Filesystem\Directory\Write $directory, $options) { $zip = new \Magento\Framework\Archive\Zip(); $file = $zip->unpack($directory->getRelativePath($file), $directory->getRelativePath(preg_replace('/\\.zip$/i', '.csv', $file))); parent::__construct($file, $directory, $options); }
/** * Create destination folder if not exists and return full file path * * @param string $destination * @param string $newName * @return string * @throws \Exception */ protected function _prepareDestination($destination = null, $newName = null) { if (empty($destination)) { $destination = $this->_fileSrcPath; } else { if (empty($newName)) { $info = pathinfo($destination); $newName = $info['basename']; $destination = $info['dirname']; } } if (empty($newName)) { $newFileName = $this->_fileSrcName; } else { $newFileName = $newName; } $fileName = $destination . '/' . $newFileName; if (!is_writable($destination)) { try { $this->directoryWrite->create($this->directoryWrite->getRelativePath($destination)); } catch (\Magento\Framework\Filesystem\FilesystemException $e) { $this->logger->addStreamLog(\Magento\Framework\Logger::LOGGER_SYSTEM); $this->logger->log($e->getMessage()); throw new \Exception('Unable to write file into directory ' . $destination . '. Access forbidden.'); } } return $fileName; }
public static function tearDownAfterClass() { self::$_varDirectory->delete(self::$_varDirectory->getRelativePath(self::$_tmpDir)); }