/** * 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); }
/** * Move image from temporary directory to normal * * @param string $file * @return string */ protected function moveImageFromTmp($file) { $file = $this->getFilenameFromTmp($file); $destinationFile = $this->getUniqueFileName($file); if ($this->fileStorageDb->checkDbUsage()) { $this->fileStorageDb->renameFile($this->mediaConfig->getTmpMediaShortUrl($file), $this->mediaConfig->getMediaShortUrl($destinationFile)); $this->mediaDirectory->delete($this->mediaConfig->getTmpMediaPath($file)); $this->mediaDirectory->delete($this->mediaConfig->getMediaPath($destinationFile)); } else { $this->mediaDirectory->renameFile($this->mediaConfig->getTmpMediaPath($file), $this->mediaConfig->getMediaPath($destinationFile)); } return str_replace('\\', '/', $destinationFile); }
/** * Add image to media gallery and return new filename * * @param \Magento\Catalog\Model\Product $product * @param string $file file path of image in file system * @param string|string[] $mediaAttribute code of attribute with type 'media_image', * leave blank if image should be only in gallery * @param boolean $move if true, it will move source file * @param boolean $exclude mark image as disabled in product page view * @return string * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function addImage(\Magento\Catalog\Model\Product $product, $file, $mediaAttribute = null, $move = false, $exclude = true) { $file = $this->mediaDirectory->getRelativePath($file); if (!$this->mediaDirectory->isFile($file)) { throw new LocalizedException(__('The image does not exist.')); } $pathinfo = pathinfo($file); $imgExtensions = ['jpg', 'jpeg', 'gif', 'png']; if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) { throw new LocalizedException(__('Please correct the image file type.')); } $fileName = \Magento\MediaStorage\Model\File\Uploader::getCorrectFileName($pathinfo['basename']); $dispretionPath = \Magento\MediaStorage\Model\File\Uploader::getDispretionPath($fileName); $fileName = $dispretionPath . '/' . $fileName; $fileName = $this->getNotDuplicatedFilename($fileName, $dispretionPath); $destinationFile = $this->mediaConfig->getTmpMediaPath($fileName); try { /** @var $storageHelper \Magento\MediaStorage\Helper\File\Storage\Database */ $storageHelper = $this->fileStorageDb; if ($move) { $this->mediaDirectory->renameFile($file, $destinationFile); //If this is used, filesystem should be configured properly $storageHelper->saveFile($this->mediaConfig->getTmpMediaShortUrl($fileName)); } else { $this->mediaDirectory->copyFile($file, $destinationFile); $storageHelper->saveFile($this->mediaConfig->getTmpMediaShortUrl($fileName)); $this->mediaDirectory->changePermissions($destinationFile, DriverInterface::WRITEABLE_FILE_MODE); } } catch (\Exception $e) { throw new LocalizedException(__('We couldn\'t move this file: %1.', $e->getMessage())); } $fileName = str_replace('\\', '/', $fileName); $attrCode = $this->getAttribute()->getAttributeCode(); $mediaGalleryData = $product->getData($attrCode); $position = 0; if (!is_array($mediaGalleryData)) { $mediaGalleryData = ['images' => []]; } foreach ($mediaGalleryData['images'] as &$image) { if (isset($image['position']) && $image['position'] > $position) { $position = $image['position']; } } $position++; $mediaGalleryData['images'][] = ['file' => $fileName, 'position' => $position, 'label' => '', 'disabled' => (int) $exclude]; $product->setData($attrCode, $mediaGalleryData); if ($mediaAttribute !== null) { $this->setMediaAttribute($product, $mediaAttribute, $fileName); } return $fileName; }
/** * 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; }
/** * Move image from temporary directory to normal * * @param string $file * @return string */ protected function _moveImageFromTmp($file) { $file = $this->getFilenameFromTmp($file); $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->_mediaConfig->getMediaPath($destinationFile)); } else { $this->_mediaDirectory->renameFile($this->_mediaConfig->getTmpMediaPath($file), $this->_mediaConfig->getMediaPath($destinationFile)); } return str_replace('\\', '/', $destinationFile); }
/** * 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); }