/** * Check sitemap file location and permissions * * @return \Magento\Framework\Model\AbstractModel * @throws \Magento\Framework\Exception\LocalizedException */ public function beforeSave() { $path = $this->getSitemapPath(); /** * Check path is allow */ if ($path && preg_match('#\\.\\.[\\\\/]#', $path)) { throw new \Magento\Framework\Exception\LocalizedException(__('Please define a correct path.')); } /** * Check exists and writable path */ if (!$this->_directory->isExist($path)) { throw new \Magento\Framework\Exception\LocalizedException(__('Please create the specified folder "%1" before saving the sitemap.', $this->_escaper->escapeHtml($this->getSitemapPath()))); } if (!$this->_directory->isWritable($path)) { throw new \Magento\Framework\Exception\LocalizedException(__('Please make sure that "%1" is writable by the web-server.', $this->getSitemapPath())); } /** * Check allow filename */ if (!preg_match('#^[a-zA-Z0-9_\\.]+$#', $this->getSitemapFilename())) { throw new \Magento\Framework\Exception\LocalizedException(__('Please use only letters (a-z or A-Z), numbers (0-9) or underscores (_) in the filename. No spaces or other characters are allowed.')); } if (!preg_match('#\\.xml$#', $this->getSitemapFilename())) { $this->setSitemapFilename($this->getSitemapFilename() . '.xml'); } $this->setSitemapPath(rtrim(str_replace(str_replace('\\', '/', $this->_getBaseDir()), '', $path), '/') . '/'); return parent::beforeSave(); }
/** * 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.')); } }
/** * Constructor * * @param \Magento\Framework\App\Filesystem $filesystem * @param string|null $destination * @throws \Magento\Framework\Model\Exception */ public function __construct(\Magento\Framework\App\Filesystem $filesystem, $destination = null) { $this->_directoryHandle = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::SYS_TMP_DIR); if (!$destination) { $destination = uniqid('importexport_'); $this->_directoryHandle->touch($destination); } if (!is_string($destination)) { throw new \Magento\Framework\Model\Exception(__('Destination file path must be a string')); } if (!$this->_directoryHandle->isWritable()) { throw new \Magento\Framework\Model\Exception(__('Destination directory is not writable')); } if ($this->_directoryHandle->isFile($destination) && !$this->_directoryHandle->isWritable($destination)) { throw new \Magento\Framework\Model\Exception(__('Destination file is not writable')); } $this->_destination = $destination; $this->_init(); }
/** * Constructor * * @param Filesystem $filesystem * @param string|null $destination * @param string $destinationDirectoryCode * @throws \Magento\Framework\Exception\LocalizedException */ public function __construct(\Magento\Framework\Filesystem $filesystem, $destination = null, $destinationDirectoryCode = DirectoryList::SYS_TMP) { $this->_directoryHandle = $filesystem->getDirectoryWrite($destinationDirectoryCode); if (!$destination) { $destination = uniqid('importexport_'); $this->_directoryHandle->touch($destination); } if (!is_string($destination)) { throw new \Magento\Framework\Exception\LocalizedException(__('The destination file path must be a string.')); } if (!$this->_directoryHandle->isWritable()) { throw new \Magento\Framework\Exception\LocalizedException(__('The destination directory is not writable.')); } if ($this->_directoryHandle->isFile($destination) && !$this->_directoryHandle->isWritable($destination)) { throw new \Magento\Framework\Exception\LocalizedException(__('Destination file is not writable')); } $this->_destination = $destination; $this->_init(); }
/** * 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; }
public function testIsWritable() { $this->driver->expects($this->once())->method('isWritable')->will($this->returnValue(true)); $this->assertTrue($this->write->isWritable('correct-path')); }