/** * 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.')); } }
public function deleteDirectory($path) { $mediaRelativePath = $this->storageHelper->getMediaRelativePath($path); $prefix = rtrim($mediaRelativePath, '/') . '/'; $this->client->deleteMatchingObjects($this->getBucket(), $prefix); return $this; }
/** * Check file in database storage if needed and place it on file system * * @param string $filePath * @return bool */ protected function _processDatabaseFile($filePath) { if (!$this->_fileStorageDatabase->checkDbUsage()) { return false; } $relativePath = $this->_fileStorageDatabase->getMediaRelativePath($filePath); $file = $this->_storageDatabaseFactory->create()->loadByFilename($relativePath); if (!$file->getId()) { return false; } $stream = $this->_rootDir->openFile($filePath, 'w+'); $stream->lock(); $stream->write($filePath, $file->getContent()); $stream->unlock(); $stream->close(); return true; }
public function aroundSaveFileToFilesystem(Database $subject, $proceed, $filename) { if ($subject->checkDbUsage() && $this->helper->checkS3Usage()) { $file = $subject->getStorageDatabaseModel()->loadByFilename($subject->getMediaRelativePath($filename)); if (!$file->getId()) { return false; } return $subject->getStorageFileModel()->saveFile($file->getData(), true); } return $proceed($filename); }
public function testGetMediaRelativePath() { $this->assertEquals('fullPath', $this->helper->getMediaRelativePath('media-dir/fullPath')); }