コード例 #1
0
 /**
  * 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;
 }
コード例 #2
0
 /**
  * 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();
 }
コード例 #3
0
 public function testCompactValueTmpFile()
 {
     $value = ['tmp_name' => 'tmp.file', 'name' => 'new.file'];
     $expected = 'saved.file';
     $fileForm = $this->getClass(null, false);
     $mediaDirMock = $this->getMockForAbstractClass('\\Magento\\Framework\\Filesystem\\Directory\\WriteInterface');
     $this->fileSystemMock->expects($this->once())->method('getDirectoryWrite')->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->will($this->returnValue($mediaDirMock));
     $mediaDirMock->expects($this->any())->method('getAbsolutePath')->will($this->returnArgument(0));
     $uploaderMock = $this->getMock('\\Magento\\Framework\\File\\Uploader', [], [], '', false);
     $this->uploaderFactoryMock->expects($this->once())->method('create')->with(['fileId' => $value])->will($this->returnValue($uploaderMock));
     $uploaderMock->expects($this->once())->method('setFilesDispersion')->with(true);
     $uploaderMock->expects($this->once())->method('setFilenamesCaseSensitivity')->with(false);
     $uploaderMock->expects($this->once())->method('setAllowRenameFiles')->with(true);
     $uploaderMock->expects($this->once())->method('save')->with(self::ENTITY_TYPE, 'new.file');
     $uploaderMock->expects($this->once())->method('getUploadedFileName')->will($this->returnValue($expected));
     $this->assertSame($expected, $fileForm->compactValue($value));
 }
コード例 #4
0
ファイル: File.php プロジェクト: kidaa30/magento2-platformsh
 /**
  * {@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;
 }