/** * Clean files in database on cleaning merged assets * * @param \Magento\Framework\View\Asset\MergeService $subject * @param callable $proceed * * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundCleanMergedJsCss(\Magento\Framework\View\Asset\MergeService $subject, \Closure $proceed) { $proceed(); /** @var \Magento\Framework\Filesystem\Directory\ReadInterface $pubStaticDirectory */ $pubStaticDirectory = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem::STATIC_VIEW_DIR); $mergedDir = $pubStaticDirectory->getAbsolutePath() . '/' . \Magento\Framework\View\Asset\Merged::getRelativeDir(); $this->database->deleteFolder($mergedDir); }
/** * Save file to storage * * @param array $result * @return $this */ protected function _afterSave($result) { if (empty($result['path']) || empty($result['file'])) { return $this; } if ($this->_coreFileStorage->isInternalStorage() || $this->skipDbProcessing()) { return $this; } $this->_result['file'] = $this->_coreFileStorageDb->saveUploadedFile($result); return $this; }
public function testCopyQuoteToOrder() { $optionMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Configuration\\Item\\Option\\OptionInterface')->disableOriginalConstructor()->setMethods(['getValue'])->getMockForAbstractClass(); $quotePath = '/quote/path/path/uploaded.file'; $orderPath = '/order/path/path/uploaded.file'; $optionMock->expects($this->any())->method('getValue')->will($this->returnValue(['quote_path' => $quotePath, 'order_path' => $orderPath])); $this->rootDirectory->expects($this->any())->method('isFile')->with($this->equalTo($quotePath))->will($this->returnValue(true)); $this->rootDirectory->expects($this->any())->method('isReadable')->with($this->equalTo($quotePath))->will($this->returnValue(true)); $this->rootDirectory->expects($this->any())->method('getAbsolutePath')->will($this->returnValue('/file.path')); $this->coreFileStorageDatabase->expects($this->any())->method('copyFile')->will($this->returnValue('true')); $fileObject = $this->getFileObject(); $fileObject->setData('configuration_item_option', $optionMock); $this->assertInstanceOf('Magento\\Catalog\\Model\\Product\\Option\\Type\\File', $fileObject->copyQuoteToOrder()); }
/** * 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); }
/** * Retrieve media base directory path * * @return string */ public function getMediaBaseDirectory() { if (is_null($this->_mediaBaseDirectory)) { $this->_mediaBaseDirectory = $this->_storageHelper->getMediaBaseDir(); } return $this->_mediaBaseDirectory; }
/** * Move file from tmp path to base path * * @param string $baseTmpPath * @param string $basePath * @param string $file * @return string */ protected function _moveFileFromTmp($baseTmpPath, $basePath, $file) { if (strrpos($file, '.tmp') == strlen($file) - 4) { $file = substr($file, 0, strlen($file) - 4); } $destFile = dirname($file) . '/' . \Magento\Core\Model\File\Uploader::getNewFileName($this->getFilePath($basePath, $file)); $this->_coreFileStorageDatabase->copyFile($this->getFilePath($baseTmpPath, $file), $this->getFilePath($basePath, $destFile)); $this->_mediaDirectory->renameFile($this->getFilePath($baseTmpPath, $file), $this->getFilePath($basePath, $destFile)); return str_replace('\\', '/', $destFile); }
public function testGetMediaBaseDir() { $mediaDirMock = $this->getMockForAbstractClass('\\Magento\\Framework\\Filesystem\\Directory\\ReadInterface'); $mediaDirMock->expects($this->any())->method('getAbsolutePath')->will($this->returnValue('media-dir')); $filesystemMock = $this->getMockBuilder('Magento\\Framework\\Filesystem')->disableOriginalConstructor()->getMock(); $filesystemMock->expects($this->once())->method('getDirectoryRead')->with(DirectoryList::MEDIA)->will($this->returnValue($mediaDirMock)); $this->helper = $this->objectManager->getObject('Magento\\Core\\Helper\\File\\Storage\\Database', ['filesystem' => $filesystemMock, 'fileStorage' => $this->fileStorageMock, 'dbStorageFactory' => $this->dbStorageFactoryMock, 'config' => $this->configMock]); $this->assertEquals('media-dir', $this->helper->getMediaBaseDir()); $this->assertEquals('media-dir', $this->helper->getMediaBaseDir()); }
/** * Delete file (and its thumbnail if exists) from storage * * @param string $target File path to be deleted * @return $this */ public function deleteFile($target) { $relativePath = $this->_directory->getRelativePath($target); if ($this->_directory->isFile($relativePath)) { $this->_directory->delete($relativePath); } $this->_coreFileStorageDb->deleteFile($target); $thumb = $this->getThumbnailPath($target, true); $relativePathThumb = $this->_directory->getRelativePath($thumb); if ($thumb) { if ($this->_directory->isFile($relativePathThumb)) { $this->_directory->delete($relativePathThumb); } $this->_coreFileStorageDb->deleteFile($thumb); } 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; }
/** * Quote item to order item copy process * * @return $this */ public function copyQuoteToOrder() { $quoteOption = $this->getConfigurationItemOption(); try { $value = unserialize($quoteOption->getValue()); if (!isset($value['quote_path'])) { throw new \Exception(); } $quotePath = $value['quote_path']; $orderPath = $value['order_path']; if (!$this->_rootDirectory->isFile($quotePath) || !$this->_rootDirectory->isReadable($quotePath)) { throw new \Exception(); } $this->_coreFileStorageDatabase->copyFile($this->_rootDirectory->getAbsolutePath($quotePath), $this->_rootDirectory->getAbsolutePath($orderPath)); } catch (\Exception $e) { return $this; } return $this; }
/** * 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)); } }
/** * First check this file on FS * If it doesn't exist - try to download it from DB * * @param string $filename * @return bool */ protected function _fileExists($filename) { if ($this->_mediaDirectory->isFile($filename)) { return true; } else { return $this->_coreFileStorageDatabase->saveFileToFilesystem($this->_mediaDirectory->getAbsolutePath($filename)); } }
/** * 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; }
/** * Process File Queue * * @return $this * @throws \Magento\Framework\Model\Exception */ public function processFileQueue() { if (empty($this->_fileQueue)) { return $this; } foreach ($this->_fileQueue as &$queueOptions) { if (isset($queueOptions['operation']) && ($operation = $queueOptions['operation'])) { switch ($operation) { case 'receive_uploaded_file': $src = isset($queueOptions['src_name']) ? $queueOptions['src_name'] : ''; $dst = isset($queueOptions['dst_name']) ? $queueOptions['dst_name'] : ''; /** @var $uploader \Zend_File_Transfer_Adapter_Http */ $uploader = isset($queueOptions['uploader']) ? $queueOptions['uploader'] : null; $path = dirname($dst); try { $rootDir = $this->_filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::ROOT_DIR); $rootDir->create($rootDir->getRelativePath($path)); } catch (\Magento\Framework\Filesystem\FilesystemException $e) { throw new \Magento\Framework\Model\Exception(__('We can\'t create writeable directory "%1".', $path)); } $uploader->setDestination($path); if (empty($src) || empty($dst) || !$uploader->receive($src)) { /** * @todo: show invalid option */ if (isset($queueOptions['option'])) { $queueOptions['option']->setIsValid(false); } throw new \Magento\Framework\Model\Exception(__("The file upload failed.")); } $this->_fileStorageDb->saveFile($dst); break; default: break; } } $queueOptions = null; } return $this; }
public function testClearCache() { $this->coreFileHelper->expects($this->once())->method('deleteFolder')->will($this->returnValue(true)); $this->image->clearCache(); }