123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Catalog\Model;
- /**
- * Catalog image uploader
- */
- class ImageUploader
- {
- /**
- * Core file storage database
- *
- * @var \Magento\MediaStorage\Helper\File\Storage\Database
- */
- protected $coreFileStorageDatabase;
- /**
- * Media directory object (writable).
- *
- * @var \Magento\Framework\Filesystem\Directory\WriteInterface
- */
- protected $mediaDirectory;
- /**
- * Uploader factory
- *
- * @var \Magento\MediaStorage\Model\File\UploaderFactory
- */
- private $uploaderFactory;
- /**
- * Store manager
- *
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $storeManager;
- /**
- * @var \Psr\Log\LoggerInterface
- */
- protected $logger;
- /**
- * Base tmp path
- *
- * @var string
- */
- protected $baseTmpPath;
- /**
- * Base path
- *
- * @var string
- */
- protected $basePath;
- /**
- * Allowed extensions
- *
- * @var string
- */
- protected $allowedExtensions;
- /**
- * List of allowed image mime types
- *
- * @var string[]
- */
- private $allowedMimeTypes;
- /**
- * ImageUploader constructor
- *
- * @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase
- * @param \Magento\Framework\Filesystem $filesystem
- * @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Psr\Log\LoggerInterface $logger
- * @param string $baseTmpPath
- * @param string $basePath
- * @param string[] $allowedExtensions
- * @param string[] $allowedMimeTypes
- */
- public function __construct(
- \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase,
- \Magento\Framework\Filesystem $filesystem,
- \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Psr\Log\LoggerInterface $logger,
- $baseTmpPath,
- $basePath,
- $allowedExtensions,
- $allowedMimeTypes = []
- ) {
- $this->coreFileStorageDatabase = $coreFileStorageDatabase;
- $this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
- $this->uploaderFactory = $uploaderFactory;
- $this->storeManager = $storeManager;
- $this->logger = $logger;
- $this->baseTmpPath = $baseTmpPath;
- $this->basePath = $basePath;
- $this->allowedExtensions = $allowedExtensions;
- $this->allowedMimeTypes = $allowedMimeTypes;
- }
- /**
- * Set base tmp path
- *
- * @param string $baseTmpPath
- *
- * @return void
- */
- public function setBaseTmpPath($baseTmpPath)
- {
- $this->baseTmpPath = $baseTmpPath;
- }
- /**
- * Set base path
- *
- * @param string $basePath
- *
- * @return void
- */
- public function setBasePath($basePath)
- {
- $this->basePath = $basePath;
- }
- /**
- * Set allowed extensions
- *
- * @param string[] $allowedExtensions
- *
- * @return void
- */
- public function setAllowedExtensions($allowedExtensions)
- {
- $this->allowedExtensions = $allowedExtensions;
- }
- /**
- * Retrieve base tmp path
- *
- * @return string
- */
- public function getBaseTmpPath()
- {
- return $this->baseTmpPath;
- }
- /**
- * Retrieve base path
- *
- * @return string
- */
- public function getBasePath()
- {
- return $this->basePath;
- }
- /**
- * Retrieve allowed extensions
- *
- * @return string[]
- */
- public function getAllowedExtensions()
- {
- return $this->allowedExtensions;
- }
- /**
- * Retrieve path
- *
- * @param string $path
- * @param string $imageName
- *
- * @return string
- */
- public function getFilePath($path, $imageName)
- {
- return rtrim($path, '/') . '/' . ltrim($imageName, '/');
- }
- /**
- * 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;
- }
- /**
- * Checking file for save and save it to tmp dir
- *
- * @param string $fileId
- *
- * @return string[]
- *
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function saveFileToTmpDir($fileId)
- {
- $baseTmpPath = $this->getBaseTmpPath();
- /** @var \Magento\MediaStorage\Model\File\Uploader $uploader */
- $uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
- $uploader->setAllowedExtensions($this->getAllowedExtensions());
- $uploader->setAllowRenameFiles(true);
- if (!$uploader->checkMimeType($this->allowedMimeTypes)) {
- throw new \Magento\Framework\Exception\LocalizedException(__('File validation failed.'));
- }
- $result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
- unset($result['path']);
- if (!$result) {
- throw new \Magento\Framework\Exception\LocalizedException(
- __('File can not be saved to the destination folder.')
- );
- }
- /**
- * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS
- */
- $result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']);
- $result['url'] = $this->storeManager
- ->getStore()
- ->getBaseUrl(
- \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
- ) . $this->getFilePath($baseTmpPath, $result['file']);
- $result['name'] = $result['file'];
- if (isset($result['file'])) {
- try {
- $relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
- $this->coreFileStorageDatabase->saveFile($relativePath);
- } catch (\Exception $e) {
- $this->logger->critical($e);
- throw new \Magento\Framework\Exception\LocalizedException(
- __('Something went wrong while saving the file(s).')
- );
- }
- }
- return $result;
- }
- }
|