Exemple #1
0
 /**
  * After save
  *
  * @param \Magento\Framework\Object $object
  * @return $this|void
  */
 public function afterSave($object)
 {
     $value = $object->getData($this->getAttribute()->getName());
     if (is_array($value) && !empty($value['delete'])) {
         $object->setData($this->getAttribute()->getName(), '');
         $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
         return;
     }
     try {
         /** @var $uploader \Magento\Core\Model\File\Uploader */
         $uploader = $this->_fileUploaderFactory->create(array('fileId' => $this->getAttribute()->getName()));
         $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(true);
     } catch (\Exception $e) {
         return $this;
     }
     $path = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem::MEDIA_DIR)->getAbsolutePath('catalog/product/');
     $uploader->save($path);
     $fileName = $uploader->getUploadedFileName();
     if ($fileName) {
         $object->setData($this->getAttribute()->getName(), $fileName);
         $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
     }
     return $this;
 }
Exemple #2
0
 /**
  * upload file
  *
  * @param $input
  * @param $destinationFolder
  * @param $data
  * @param array $allowedExtensions
  * @return string
  * @throws \Magento\Framework\Model\Exception
  */
 public function uploadFileAndGetName($input, $destinationFolder, $data, $allowedExtensions = [])
 {
     try {
         if (isset($data[$input]['delete'])) {
             return '';
         } else {
             $uploader = $this->uploaderFactory->create(['fileId' => $input]);
             $uploader->setAllowRenameFiles(true);
             $uploader->setFilesDispersion(true);
             $uploader->setAllowCreateFolders(true);
             $uploader->setAllowedExtensions($allowedExtensions);
             $result = $uploader->save($destinationFolder);
             return $result['file'];
         }
     } catch (\Exception $e) {
         if ($e->getCode() != Uploader::TMP_NAME_EMPTY) {
             throw new \Exception($e->getMessage());
         } else {
             if (isset($data[$input]['value'])) {
                 return $data[$input]['value'];
             }
         }
     }
     return '';
 }
Exemple #3
0
 /**
  * Save uploaded file and set its name to category
  *
  * @param \Magento\Framework\Object $object
  * @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
  */
 public function afterSave($object)
 {
     $value = $object->getData($this->getAttribute()->getName() . '_additional_data');
     // if no image was set - nothing to do
     if (empty($value) && empty($_FILES)) {
         return $this;
     }
     if (is_array($value) && !empty($value['delete'])) {
         $object->setData($this->getAttribute()->getName(), '');
         $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
         return $this;
     }
     $path = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem::MEDIA_DIR)->getAbsolutePath('catalog/category/');
     try {
         /** @var $uploader \Magento\Core\Model\File\Uploader */
         $uploader = $this->_fileUploaderFactory->create(array('fileId' => $this->getAttribute()->getName()));
         $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
         $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\Core\Model\File\Uploader::TMP_NAME_EMPTY) {
             $this->_logger->logException($e);
         }
     }
     return $this;
 }
Exemple #4
0
 protected function setUp()
 {
     $this->_uploader = $this->getMock('Magento\\Core\\Model\\File\\Uploader', [], [], '', false);
     $this->_uploaderFactory = $this->getMock('Magento\\Core\\Model\\File\\UploaderFactory', ['create'], [], '', false);
     $this->_uploaderFactory->expects($this->any())->method('create')->will($this->returnValue($this->_uploader));
     $this->_directoryMock = $this->getMock('Magento\\Framework\\Filesystem\\Directory\\Read', [], [], '', false);
     $this->_filesystemMock = $this->getMock('Magento\\Framework\\Filesystem', [], [], '', false);
     $this->_filesystemMock->expects($this->any())->method('getDirectoryRead')->will($this->returnValue($this->_directoryMock));
     /** @var $service \Magento\Theme\Model\Uploader\Service */
     $this->_fileSizeMock = $this->getMockBuilder('Magento\\Framework\\File\\Size')->setMethods(['getMaxFileSize'])->disableOriginalConstructor()->getMock();
     $this->_fileSizeMock->expects($this->any())->method('getMaxFileSize')->will($this->returnValue(600 * self::MB_MULTIPLIER));
 }
Exemple #5
0
 /**
  * Save uploaded file before saving config value
  *
  * @return $this
  * @throws \Magento\Framework\Model\Exception
  */
 public function beforeSave()
 {
     $value = $this->getValue();
     $tmpName = $this->_requestData->getTmpName($this->getPath());
     $file = [];
     if ($tmpName) {
         $file['tmp_name'] = $tmpName;
         $file['name'] = $this->_requestData->getName($this->getPath());
     } elseif (!empty($value['tmp_name'])) {
         $file['tmp_name'] = $value['tmp_name'];
         $file['name'] = $value['value'];
     }
     if (!empty($file)) {
         $uploadDir = $this->_getUploadDir();
         try {
             $uploader = $this->_uploaderFactory->create(['fileId' => $file]);
             $uploader->setAllowedExtensions($this->_getAllowedExtensions());
             $uploader->setAllowRenameFiles(true);
             $uploader->addValidateCallback('size', $this, 'validateMaxSize');
             $result = $uploader->save($uploadDir);
         } catch (\Exception $e) {
             throw new \Magento\Framework\Model\Exception($e->getMessage());
         }
         $filename = $result['file'];
         if ($filename) {
             if ($this->_addWhetherScopeInfo()) {
                 $filename = $this->_prependScopeInfo($filename);
             }
             $this->setValue($filename);
         }
     } else {
         if (is_array($value) && !empty($value['delete'])) {
             $this->setValue('');
         } else {
             $this->unsValue();
         }
     }
     return $this;
 }
Exemple #6
0
 /**
  * Upload image file
  *
  * @param string $key
  * @return array
  */
 public function uploadFile($key)
 {
     $result = array();
     /** @var $uploader \Magento\Core\Model\File\Uploader */
     $uploader = $this->_uploaderFactory->create(array('fileId' => $key));
     $uploader->setAllowedExtensions($this->_allowedExtensions);
     $uploader->setAllowRenameFiles(true);
     $uploader->setAllowCreateFolders(true);
     if (!$uploader->save($this->getStoragePath())) {
         /** @todo add translator */
         throw new \Magento\Framework\Model\Exception('Cannot upload file.');
     }
     $result['css_path'] = implode('/', array('..', self::PATH_PREFIX_QUICK_STYLE, $uploader->getUploadedFileName()));
     $result['name'] = $uploader->getUploadedFileName();
     return $result;
 }
 /**
  * Upload image and return uploaded image file name or false
  *
  * @throws Mage_Core_Exception
  * @param string $scope the request key for file
  * @return bool|string
  */
 public function uploadImage($scope)
 {
     $adapter = $this->httpFactory->create();
     $adapter->addValidator(new \Zend_Validate_File_FilesSize(['max' => self::MAX_FILE_SIZE]));
     if ($adapter->isUploaded($scope)) {
         // validate image
         if (!$adapter->isValid($scope)) {
             $this->messageManager->addException__('Uploaded image is not valid.');
         }
         $uploader = $this->_fileUploaderFactory->create(['fileId' => $scope]);
         $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(false);
         $uploader->setAllowCreateFolders(true);
         if ($uploader->save($this->getBaseDir())) {
             return $uploader->getUploadedFileName();
         }
     }
     return false;
 }
Exemple #8
0
 /**
  * Upload and resize new file
  *
  * @param string $targetPath Target directory
  * @param string $type Type of storage, e.g. image, media etc.
  * @return array File info Array
  * @throws \Magento\Framework\Model\Exception
  */
 public function uploadFile($targetPath, $type = null)
 {
     /** @var \Magento\Core\Model\File\Uploader $uploader */
     $uploader = $this->_uploaderFactory->create(array('fileId' => 'image'));
     $allowed = $this->getAllowedExtensions($type);
     if ($allowed) {
         $uploader->setAllowedExtensions($allowed);
     }
     $uploader->setAllowRenameFiles(true);
     $uploader->setFilesDispersion(false);
     $result = $uploader->save($targetPath);
     if (!$result) {
         throw new \Magento\Framework\Model\Exception(__('We cannot upload the file.'));
     }
     // create thumbnail
     $this->resizeFile($targetPath . '/' . $uploader->getUploadedFileName(), true);
     $result['cookie'] = array('name' => $this->getSession()->getName(), 'value' => $this->getSession()->getSessionId(), 'lifetime' => $this->getSession()->getCookieLifetime(), 'path' => $this->getSession()->getCookiePath(), 'domain' => $this->getSession()->getCookieDomain());
     return $result;
 }
Exemple #9
0
 /**
  * Move uploaded file and create source adapter instance.
  *
  * @throws \Magento\Framework\Model\Exception
  * @return string Source file path
  */
 public function uploadSource()
 {
     /** @var $adapter \Zend_File_Transfer_Adapter_Http */
     $adapter = $this->_httpFactory->create();
     if (!$adapter->isValid(self::FIELD_NAME_SOURCE_FILE)) {
         $errors = $adapter->getErrors();
         if ($errors[0] == \Zend_Validate_File_Upload::INI_SIZE) {
             $errorMessage = $this->_importExportData->getMaxUploadSizeMessage();
         } else {
             $errorMessage = __('File was not uploaded.');
         }
         throw new \Magento\Framework\Model\Exception($errorMessage);
     }
     $entity = $this->getEntity();
     /** @var $uploader \Magento\Core\Model\File\Uploader */
     $uploader = $this->_uploaderFactory->create(array('fileId' => self::FIELD_NAME_SOURCE_FILE));
     $uploader->skipDbProcessing(true);
     $result = $uploader->save($this->getWorkingDir());
     $extension = pathinfo($result['file'], PATHINFO_EXTENSION);
     $uploadedFile = $result['path'] . $result['file'];
     if (!$extension) {
         $this->_varDirectory->delete($uploadedFile);
         throw new \Magento\Framework\Model\Exception(__('Uploaded file has no extension'));
     }
     $sourceFile = $this->getWorkingDir() . $entity;
     $sourceFile .= '.' . $extension;
     $sourceFileRelative = $this->_varDirectory->getRelativePath($sourceFile);
     if (strtolower($uploadedFile) != strtolower($sourceFile)) {
         if ($this->_varDirectory->isExist($sourceFileRelative)) {
             $this->_varDirectory->delete($sourceFileRelative);
         }
         try {
             $this->_varDirectory->renameFile($this->_varDirectory->getRelativePath($uploadedFile), $sourceFileRelative);
         } catch (\Magento\Framework\Filesystem\FilesystemException $e) {
             throw new \Magento\Framework\Model\Exception(__('Source file moving failed'));
         }
     }
     $this->_removeBom($sourceFile);
     // trying to create source adapter for file and catch possible exception to be convinced in its adequacy
     try {
         $this->_getSourceAdapter($sourceFile);
     } catch (\Exception $e) {
         $this->_varDirectory->delete($sourceFileRelative);
         throw new \Magento\Framework\Model\Exception($e->getMessage());
     }
     return $sourceFile;
 }