Example #1
0
 /**
  * move image from tmp to catalog dir
  *
  * @param string $file
  * @return string path
  */
 public function moveImageFromTmp($file)
 {
     if (strrpos($file, '.tmp') == strlen($file) - 4) {
         $file = substr($file, 0, strlen($file) - 4);
     }
     $destinationFile = $this->getUniqueFileName($file);
     /** @var $storageHelper \Magento\MediaStorage\Helper\File\Storage\Database */
     $storageHelper = $this->fileStorageDb;
     if ($storageHelper->checkDbUsage()) {
         $storageHelper->renameFile($this->mediaConfig->getTmpMediaShortUrl($file), $this->mediaConfig->getMediaShortUrl($destinationFile));
         $this->mediaDirectory->delete($this->mediaConfig->getTmpMediaPath($file));
         $this->mediaDirectory->delete($this->getAttributeSwatchPath($destinationFile));
     } else {
         $this->mediaDirectory->renameFile($this->mediaConfig->getTmpMediaPath($file), $this->getAttributeSwatchPath($destinationFile));
     }
     return str_replace('\\', '/', $destinationFile);
 }
Example #2
0
 /**
  * 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));
     }
 }