/**
  * Upload the file
  *
  * @param $download
  *
  * @return array
  * @throws \Magento\Framework\Validator\Exception
  */
 public function uploadFile($download)
 {
     try {
         $uploader = $this->uploaderFactory->create(['fileId' => $download]);
         $uploader->setAllowedExtensions($this->getMimeTimes());
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(true);
         $uploader->setAllowCreateFolders(true);
         $result = $uploader->save($this->uploadPath);
         return $result;
     } catch (\Exception $e) {
         if ($e->getCode() != Uploader::TMP_NAME_EMPTY) {
             throw new \Magento\Framework\Validator\Exception(__('Disallowed file type, only these file types are allowed: %s.', implode(', ', $this->getMimeTimes())));
         }
     }
     return false;
 }
 /**
  * Upload preview image
  *
  * @param string $scope the request key for file
  * @param string $destinationPath path to upload directory
  * @return bool
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function uploadPreviewImage($scope, $destinationPath)
 {
     if (!$this->_transferAdapter->isUploaded($scope)) {
         return false;
     }
     if (!$this->_transferAdapter->isValid($scope)) {
         throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('Uploaded image is not valid'));
     }
     $upload = $this->_uploaderFactory->create(['fileId' => $scope]);
     $upload->setAllowCreateFolders(true);
     $upload->setAllowedExtensions($this->_allowedExtensions);
     $upload->setAllowRenameFiles(true);
     $upload->setFilesDispersion(false);
     if (!$upload->checkAllowedExtension($upload->getFileExtension())) {
         throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('Invalid image file type.'));
     }
     if (!$upload->save($destinationPath)) {
         throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('Image can not be saved.'));
     }
     return $destinationPath . '/' . $upload->getUploadedFileName();
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  *
  * @return $this|string
  */
 public function compactValue($value)
 {
     if ($this->getIsAjaxRequest()) {
         return $this;
     }
     $attribute = $this->getAttribute();
     $original = $this->_value;
     $toDelete = false;
     if ($original) {
         if (!$attribute->isRequired() && !empty($value['delete'])) {
             $toDelete = true;
         }
         if (!empty($value['tmp_name'])) {
             $toDelete = true;
         }
     }
     $mediaDir = $this->_fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
     $result = $original;
     // unlink entity file
     if ($toDelete) {
         $result = '';
         $mediaDir->delete($this->_entityTypeCode . $original);
     }
     if (!empty($value['tmp_name'])) {
         try {
             $uploader = $this->uploaderFactory->create(['fileId' => $value]);
             $uploader->setFilesDispersion(true);
             $uploader->setFilenamesCaseSensitivity(false);
             $uploader->setAllowRenameFiles(true);
             $uploader->save($mediaDir->getAbsolutePath($this->_entityTypeCode), $value['name']);
             $result = $uploader->getUploadedFileName();
         } catch (\Exception $e) {
             $this->_logger->critical($e);
         }
     }
     return $result;
 }