/** * @param int $storage * @param int $callNum * @dataProvider updateFileDataProvider */ public function testCopyFile($storage, $callNum) { $this->configMock->expects($this->once())->method('getValue')->with(\Magento\MediaStorage\Model\File\Storage::XML_PATH_STORAGE_MEDIA, 'default')->will($this->returnValue($storage)); $dbModelMock = $this->getMockBuilder('Magento\\MediaStorage\\Model\\File\\Storage\\Database')->disableOriginalConstructor()->getMock(); $this->dbStorageFactoryMock->expects($this->exactly($callNum))->method('create')->will($this->returnValue($dbModelMock)); $dbModelMock->expects($this->exactly($callNum))->method('copyFile')->with('oldName', 'newName'); $this->helper->copyFile('media-dir/oldName', 'media-dir/newName'); }
/** * 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\MediaStorage\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); }
/** * Duplicate temporary images * * @param string $file * @return string */ public function duplicateImageFromTmp($file) { $file = $this->getFilenameFromTmp($file); $destinationFile = $this->getUniqueFileName($file, true); if ($this->fileStorageDb->checkDbUsage()) { $this->fileStorageDb->copyFile($this->mediaDirectory->getAbsolutePath($this->mediaConfig->getTmpMediaShortUrl($file)), $this->mediaConfig->getTmpMediaShortUrl($destinationFile)); } else { $this->mediaDirectory->copyFile($this->mediaConfig->getTmpMediaPath($file), $this->mediaConfig->getTmpMediaPath($destinationFile)); } return str_replace('\\', '/', $destinationFile); }
/** * Checking file for moving and move it * * @param string $imageName * * @return string * * @throws \Magento\Framework\Exception\LocalizedException */ public function moveFileFromTmp($imageName) { $baseTmpPath = $this->getBaseTmpPath(); $basePath = $this->getBasePath(); $baseImagePath = $this->getFilePath($basePath, $imageName); $baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName); try { $this->coreFileStorageDatabase->copyFile($baseTmpImagePath, $baseImagePath); $this->mediaDirectory->renameFile($baseTmpImagePath, $baseImagePath); } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__('Something went wrong while saving the file(s).')); } return $imageName; }
/** * 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 \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)); } }