![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Catalog/Model/Product/Attribute/Backend/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Radosław Stępień <[email protected]> <[email protected]> */ namespace Cnc\Catalog\Model\Product\Attribute\Backend; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\DataObject; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Filesystem; use Magento\Framework\Filesystem\Driver\File; use Magento\MediaStorage\Model\File\UploaderFactory; use Psr\Log\LoggerInterface; class BannerImage extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend { /** * @var File */ protected $_file; /** * @var UploaderFactory * * @deprecated 101.0.0 */ protected $_uploaderFactory; /** * @var Filesystem * * @deprecated 101.0.0 */ protected $_filesystem; /** * @var UploaderFactory * * @deprecated 101.0.0 */ protected $_fileUploaderFactory; /** * @var LoggerInterface * * @deprecated 101.0.0 */ protected $_logger; /** * @param File $file * @param LoggerInterface $logger * @param Filesystem $filesystem * @param UploaderFactory $fileUploaderFactory */ public function __construct( File $file, LoggerInterface $logger, Filesystem $filesystem, UploaderFactory $fileUploaderFactory ) { $this->_file = $file; $this->_filesystem = $filesystem; $this->_fileUploaderFactory = $fileUploaderFactory; $this->_logger = $logger; } /** * Save uploaded file and set its name to category * * @param DataObject $object * @return BannerImage * @throws FileSystemException */ public function afterSave($object) { $path = $this->_filesystem->getDirectoryRead( DirectoryList::MEDIA )->getAbsolutePath( 'catalog/product/file/' ); $delete = $object->getData($this->getAttribute()->getName() . '_delete'); if ($delete) { $fileName = $object->getData($this->getAttribute()->getName()); $object->setData($this->getAttribute()->getName(), ''); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); if ($this->_file->isExists($path . $fileName)) { $this->_file->deleteFile($path . $fileName); } } if (empty($_FILES)) { return $this;// if no image is set then nothing to do } try { $uploader = $this->_fileUploaderFactory->create( ['fileId' => 'product[' . $this->getAttribute()->getName() . ']'] ); $uploader->setAllowedExtensions(['jpg', 'png', 'jpeg', 'gif', 'bmp', 'svg']); $uploader->setAllowRenameFiles(true); $result = $uploader->save($path); $object->setData($this->getAttribute()->getName(), $result['file']); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); } catch (\Exception $e) { if ($e->getCode() != \Magento\MediaStorage\Model\File\Uploader::TMP_NAME_EMPTY) { $this->_logger->critical($e); } } return $this; } }