/** * If DB file storage is on - find there, otherwise - just file_exists * * @param string $filename relative path * @return bool */ protected function _isFile($filename) { if ($this->_fileStorageHelper->checkDbUsage() && !$this->getMediaDirectory()->isFile($filename)) { $this->_fileStorageHelper->saveFileToFilesystem($filename); } return $this->getMediaDirectory()->isFile($filename); }
/** * Recursively delete directory from storage * * @param string $path Target dir * @return void * @throws \Magento\Framework\Model\Exception */ public function deleteDirectory($path) { if ($this->_coreFileStorageDb->checkDbUsage()) { $this->_directoryDatabaseFactory->create()->deleteDirectory($path); } try { $this->_deleteByPath($path); $path = $this->getThumbnailRoot() . $this->_getRelativePathToRoot($path); $this->_deleteByPath($path); } catch (\Magento\Framework\Filesystem\FilesystemException $e) { throw new \Magento\Framework\Model\Exception(__('We cannot delete directory %1.', $path)); } }
/** * 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; }
/** * Copy image and return new filename. * * @param string $file * @return string * @throws Exception */ protected function _copyImage($file) { try { $destinationFile = $this->_getUniqueFileName($file); if (!$this->_mediaDirectory->isFile($this->_mediaConfig->getMediaPath($file))) { throw new \Exception(); } if ($this->_fileStorageDb->checkDbUsage()) { $this->_fileStorageDb->copyFile($this->_mediaDirectory->getAbsolutePath($this->_mediaConfig->getMediaShortUrl($file)), $this->_mediaConfig->getMediaShortUrl($destinationFile)); $this->_mediaDirectory->delete($this->_mediaConfig->getMediaPath($destinationFile)); } else { $this->_mediaDirectory->copyFile($this->_mediaConfig->getMediaPath($file), $this->_mediaConfig->getMediaPath($destinationFile)); } return str_replace('\\', '/', $destinationFile); } catch (\Exception $e) { $file = $this->_mediaConfig->getMediaPath($file); throw new Exception(__('We couldn\'t copy file %1. Please delete media with non-existing images and try again.', $file)); } }
/** * Retrieve URL for media catalog * * If we use Database file storage and server doesn't support rewrites (.htaccess in media folder) * we have to put name of fetching media script exactly into URL * * @param \Magento\Framework\App\Filesystem $filesystem * @param bool $secure * @return string|bool */ protected function _getMediaScriptUrl(\Magento\Framework\App\Filesystem $filesystem, $secure) { if (!$this->_getConfig(self::XML_PATH_USE_REWRITES) && $this->_coreFileStorageDatabase->checkDbUsage()) { return $this->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB, $secure) . $filesystem->getUri(\Magento\Framework\App\Filesystem::PUB_DIR) . '/' . self::MEDIA_REWRITE_SCRIPT; } return false; }
/** * @param int $storage * @param bool $expected * @dataProvider checkDbUsageDataProvider */ public function testCheckDbUsage($storage, $expected) { $this->configMock->expects($this->once())->method('getValue')->with(\Magento\Core\Model\File\Storage::XML_PATH_STORAGE_MEDIA, 'default')->will($this->returnValue($storage)); $this->assertEquals($expected, $this->helper->checkDbUsage()); $this->assertEquals($expected, $this->helper->checkDbUsage()); }