public static function setUpBeforeClass() { /** @var \Magento\Framework\App\Filesystem $filesystem */ $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\App\\Filesystem'); self::$_varDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::VAR_DIR); self::$_tmpDir = self::$_varDirectory->getAbsolutePath('ConfigTest'); self::$_varDirectory->create(self::$_varDirectory->getRelativePath(self::$_tmpDir)); }
/** * Set base dir * * @param \Magento\Core\Model\EntityFactory $entityFactory * @param \Magento\Framework\App\Filesystem $filesystem */ public function __construct(\Magento\Core\Model\EntityFactory $entityFactory, \Magento\Framework\App\Filesystem $filesystem) { parent::__construct($entityFactory); $this->filesystem = $filesystem; $this->connectDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::VAR_DIR); $this->connectDirectory->create('connect'); $this->addTargetDir($this->connectDirectory->getAbsolutePath('connect')); }
/** * @param \Magento\Framework\App\Helper\Context $context * @param \Magento\Framework\Filesystem $filesystem * @param \Magento\Backend\Model\Session $session * @param \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory */ public function __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\Filesystem $filesystem, \Magento\Backend\Model\Session $session, \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory) { parent::__construct($context); $this->filesystem = $filesystem; $this->_session = $session; $this->_themeFactory = $themeFactory; $this->mediaDirectoryWrite = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA); $this->mediaDirectoryWrite->create($this->mediaDirectoryWrite->getRelativePath($this->getStorageRoot())); }
/** * Log information about fatal error. * * @param string $reportData * @return string */ protected function _saveFatalErrorReport($reportData) { $this->directoryWrite->create('report/api'); $reportId = abs(intval(microtime(true) * rand(100, 1000))); $this->directoryWrite->writeFile('report/api/' . $reportId, serialize($reportData)); return $reportId; }
/** * Check var/generation read and write access * * @return bool */ public function check() { $initParams = $this->serviceManager->get(InitParamListener::BOOTSTRAP_PARAM); $filesystemDirPaths = isset($initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]) ? $initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] : []; $directoryList = new DirectoryList(BP, $filesystemDirPaths); $generationDirectoryPath = $directoryList->getPath(DirectoryList::GENERATION); $driverPool = new DriverPool(); $fileWriteFactory = new WriteFactory($driverPool); /** @var \Magento\Framework\Filesystem\DriverInterface $driver */ $driver = $driverPool->getDriver(DriverPool::FILE); $directoryWrite = new Write($fileWriteFactory, $driver, $generationDirectoryPath); if ($directoryWrite->isExist()) { if ($directoryWrite->isDirectory() || $directoryWrite->isReadable()) { try { $probeFilePath = $generationDirectoryPath . DIRECTORY_SEPARATOR . uniqid(mt_rand()) . 'tmp'; $fileWriteFactory->create($probeFilePath, DriverPool::FILE, 'w'); $driver->deleteFile($probeFilePath); } catch (\Exception $e) { return false; } } else { return false; } } else { try { $directoryWrite->create(); } catch (\Exception $e) { return false; } } return true; }
/** * 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; }
/** * Create package file compatible with previous version of Magento Connect Manager * * @return boolean */ public function createPackageV1x() { try { $this->writeDirectory->create('pear/'); } catch (\Magento\Framework\Filesystem\FilesystemException $e) { $this->logger->addStreamLog(\Magento\Framework\Logger::LOGGER_EXCEPTION); $this->logger->log($e->getMessage()); return false; } if (!$this->getPackageXml()) { $this->generatePackageXml(); } $this->getPackage()->saveV1x($this->writeDirectory->getAbsolutePath('pear/')); return true; }
/** * Create folder * * @param string $name * @param string $path * @return array * @throws \Magento\Framework\Exception\LocalizedException */ public function createFolder($name, $path) { if (!preg_match(self::DIRECTORY_NAME_REGEXP, $name)) { throw new \Magento\Framework\Exception\LocalizedException(__('Use only standard alphanumeric, dashes and underscores.')); } if (!$this->mediaWriteDirectory->isWritable($path)) { $path = $this->_helper->getStorageRoot(); } $newPath = $path . '/' . $name; if ($this->mediaWriteDirectory->isExist($newPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('We found a directory with the same name.')); } $this->mediaWriteDirectory->create($newPath); $result = ['name' => $name, 'short_name' => $this->_helper->getShortFilename($name), 'path' => str_replace($this->_helper->getStorageRoot(), '', $newPath), 'id' => $this->_helper->convertPathToId($newPath)]; return $result; }
/** * Return path of the current selected directory or root directory for startup * Try to create target directory if it doesn't exist * * @return string * @throws \Magento\Framework\Model\Exception */ public function getCurrentPath() { if (!$this->_currentPath) { $currentPath = $this->_directory->getAbsolutePath() . \Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY; $path = $this->_getRequest()->getParam($this->getTreeNodeName()); if ($path) { $path = $this->convertIdToPath($path); if ($this->_directory->isDirectory($this->_directory->getRelativePath($path))) { $currentPath = $path; } } try { $currentDir = $this->_directory->getRelativePath($currentPath); if (!$this->_directory->isExist($currentDir)) { $this->_directory->create($currentDir); } } catch (\Magento\Framework\Filesystem\FilesystemException $e) { $message = __('The directory %1 is not writable by server.', $currentPath); throw new \Magento\Framework\Model\Exception($message); } $this->_currentPath = $currentPath; } return $this->_currentPath; }
/** * 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 function testCreate() { $this->driver->expects($this->once())->method('isDirectory')->will($this->returnValue(false)); $this->driver->expects($this->once())->method('createDirectory')->will($this->returnValue(true)); $this->assertTrue($this->write->create('correct-path')); }