/** * Clear temporary directories * * @param \Magento\Install\Controller\Index\Index $subject * @param \Magento\Framework\App\RequestInterface $request * * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeDispatch(\Magento\Install\Controller\Index\Index $subject, \Magento\Framework\App\RequestInterface $request) { if (!$this->appState->isInstalled()) { foreach ($this->varDirectory->read() as $dir) { if ($this->varDirectory->isDirectory($dir)) { try { $this->varDirectory->delete($dir); } catch (FilesystemException $exception) { $this->logger->log($exception->getMessage()); } } } } }
/** * 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 new directory in storage * * @param string $name New directory name * @param string $path Parent directory path * @return array New directory info * @throws \Magento\Framework\Exception\LocalizedException */ public function createDirectory($name, $path) { if (!preg_match(self::DIRECTORY_NAME_REGEXP, $name)) { throw new \Magento\Framework\Exception\LocalizedException(__('Please rename the folder using only letters, numbers, underscores and dashes.')); } $relativePath = $this->_directory->getRelativePath($path); if (!$this->_directory->isDirectory($relativePath) || !$this->_directory->isWritable($relativePath)) { $path = $this->_cmsWysiwygImages->getStorageRoot(); } $newPath = $path . '/' . $name; $relativeNewPath = $this->_directory->getRelativePath($newPath); if ($this->_directory->isDirectory($relativeNewPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('We found a directory with the same name. Please try another folder name.')); } $this->_directory->create($relativeNewPath); try { if ($this->_coreFileStorageDb->checkDbUsage()) { $relativePath = $this->_coreFileStorageDb->getMediaRelativePath($newPath); $this->_directoryDatabaseFactory->create()->createRecursive($relativePath); } $result = ['name' => $name, 'short_name' => $this->_cmsWysiwygImages->getShortFilename($name), 'path' => $newPath, 'id' => $this->_cmsWysiwygImages->convertPathToId($newPath)]; return $result; } catch (\Magento\Framework\Exception\FileSystemException $e) { throw new \Magento\Framework\Exception\LocalizedException(__('We cannot create a new directory.')); } }
/** * Copies all files in a directory recursively * * @param string $baseDir * @param string $sourceDir * @param string $targetDir * @return void */ protected function _copyFilesRecursively($baseDir, $sourceDir, $targetDir) { foreach ($this->_directory->read($sourceDir) as $path) { if ($this->_directory->isDirectory($path)) { $this->_copyFilesRecursively($baseDir, $path, $targetDir); } else { $filePath = substr($path, strlen($baseDir) + 1); $this->_directory->copyFile($path, $targetDir . '/' . $filePath); } } }
/** * Get directory collection * * @param string $currentPath * @return array * @throws \Magento\Framework\Exception\LocalizedException */ public function getDirsCollection($currentPath) { if (!$this->mediaWriteDirectory->isExist($currentPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('We cannot find a directory with this name.')); } $paths = $this->mediaWriteDirectory->search('.*', $currentPath); $directories = []; foreach ($paths as $path) { if ($this->mediaWriteDirectory->isDirectory($path)) { $directories[] = $path; } } return $directories; }
/** * 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; }
/** * 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; }