/** * Check file in database storage if needed and place it on file system * * @param string $filePath * @param string $relativePath * @return bool */ protected function _processDatabaseFile($filePath, $relativePath) { if (!$this->_fileStorageDatabase->checkDbUsage()) { return false; } $file = $this->_storageDatabaseFactory->create()->loadByFilename($relativePath); if (!$file->getId()) { return false; } $stream = $this->_rootDir->openFile($relativePath, 'w+'); $stream->lock(); $stream->write($filePath, $file->getContent()); $stream->unlock(); $stream->close(); return true; }
/** * 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); }
/** * If DB file storage is on - find there, otherwise - just file_exists * * @param string $filename relative file path * @return bool */ protected function checkIsFile($filename) { if ($this->fileStorageDatabase->checkDbUsage() && !$this->mediaDirectory->isFile($filename)) { $this->fileStorageDatabase->saveFileToFilesystem($filename); } return $this->mediaDirectory->isFile($filename); }
/** * Check whether file to move exists. Getting unique name * * @param <type> $file * @return string */ protected function getUniqueFileName($file) { if ($this->fileStorageDb->checkDbUsage()) { $destFile = $this->fileStorageDb->getUniqueFilename($this->mediaConfig->getBaseMediaUrlAddition(), $file); } else { $destFile = dirname($file) . '/' . \Magento\MediaStorage\Model\File\Uploader::getNewFileName($this->mediaDirectory->getAbsolutePath($this->getAttributeSwatchPath($file))); } return $destFile; }
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); }
/** * Recursively delete directory from storage * * @param string $path Target dir * @return void * @throws \Magento\Framework\Exception\LocalizedException */ 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\Exception\FileSystemException $e) { throw new \Magento\Framework\Exception\LocalizedException(__('We cannot delete directory %1.', $path)); } }
/** * Copy image and return new filename. * * @param string $file * @return string * @throws \Magento\Framework\Exception\LocalizedException */ 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 LocalizedException(__('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 Filesystem $filesystem * @param bool $secure * @return string|bool */ protected function _getMediaScriptUrl(Filesystem $filesystem, $secure) { if (!$this->getConfig(self::XML_PATH_USE_REWRITES) && $this->_coreFileStorageDatabase->checkDbUsage()) { $baseUrl = $this->getBaseUrl(UrlInterface::URL_TYPE_WEB, $secure); return $baseUrl . $filesystem->getUri(DirectoryList::PUB) . '/' . 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\MediaStorage\Model\File\Storage::XML_PATH_STORAGE_MEDIA, 'default')->will($this->returnValue($storage)); $this->assertEquals($expected, $this->helper->checkDbUsage()); $this->assertEquals($expected, $this->helper->checkDbUsage()); }