/** * Get filename which is not duplicated with other files in media temporary and media directories * * @param string $fileName * @param string $dispretionPath * @return string */ protected function _getNotDuplicatedFilename($fileName, $dispretionPath) { $fileMediaName = $dispretionPath . '/' . \Magento\MediaStorage\Model\File\Uploader::getNewFileName($this->_mediaConfig->getMediaPath($fileName)); $fileTmpMediaName = $dispretionPath . '/' . \Magento\MediaStorage\Model\File\Uploader::getNewFileName($this->_mediaConfig->getTmpMediaPath($fileName)); if ($fileMediaName != $fileTmpMediaName) { if ($fileMediaName != $fileName) { return $this->_getNotDuplicatedFileName($fileMediaName, $dispretionPath); } elseif ($fileTmpMediaName != $fileName) { return $this->_getNotDuplicatedFilename($fileTmpMediaName, $dispretionPath); } } return $fileMediaName; }
/** * @return string */ public function getImagesJson() { $value = $this->getElement()->getImages(); if (is_array($value) && array_key_exists('images', $value) && is_array($value['images']) && count($value['images'])) { $directory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA); $images = $this->sortImagesByPosition($value['images']); foreach ($images as &$image) { $image['url'] = $this->_mediaConfig->getMediaUrl($image['file']); $fileHandler = $directory->stat($this->_mediaConfig->getMediaPath($image['file'])); $image['size'] = $fileHandler['size']; } return $this->_jsonEncoder->encode($images); } return '[]'; }
/** * 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 \Magento\Framework\Exception\LocalizedException(__('We couldn\'t copy file %1. Please delete media with non-existing images and try again.', $file)); } }