Пример #1
0
 /**
  * Upload file controller action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function executeInternal()
 {
     $type = $this->getRequest()->getParam('type');
     $tmpPath = '';
     if ($type == 'samples') {
         $tmpPath = $this->_sample->getBaseTmpPath();
     } elseif ($type == 'links') {
         $tmpPath = $this->_link->getBaseTmpPath();
     } elseif ($type == 'link_samples') {
         $tmpPath = $this->_link->getBaseSampleTmpPath();
     }
     try {
         $uploader = $this->uploaderFactory->create(['fileId' => $type]);
         $result = $this->_fileHelper->uploadFromTmp($tmpPath, $uploader);
         if (!$result) {
             throw new \Exception('File can not be moved from temporary folder to the destination folder.');
         }
         /**
          * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS
          */
         $result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']);
         $result['path'] = str_replace('\\', '/', $result['path']);
         if (isset($result['file'])) {
             $relativePath = rtrim($tmpPath, '/') . '/' . ltrim($result['file'], '/');
             $this->storageDatabase->saveFile($relativePath);
         }
         $result['cookie'] = ['name' => $this->_getSession()->getName(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain()];
     } catch (\Exception $e) {
         $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
     }
     return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
 }
 /**
  * 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();
     $uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
     $uploader->setAllowedExtensions($this->getAllowedExtensions());
     $uploader->setAllowRenameFiles(true);
     $result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
     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['path'] = str_replace('\\', '/', $result['path']);
     $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;
 }
Пример #3
0
 /**
  * @param int $storage
  * @param int $callNum
  * @dataProvider updateFileDataProvider
  */
 public function testSaveFile($storage, $callNum)
 {
     $this->configMock->expects($this->once())->method('getValue')->with(\Magento\MediaStorage\Model\File\Storage::XML_PATH_STORAGE_MEDIA, 'default')->will($this->returnValue($storage));
     $dbModelMock = $this->getMockBuilder('Magento\\MediaStorage\\Model\\File\\Storage\\Database')->disableOriginalConstructor()->getMock();
     $this->dbStorageFactoryMock->expects($this->exactly($callNum))->method('create')->will($this->returnValue($dbModelMock));
     $dbModelMock->expects($this->exactly($callNum))->method('saveFile')->with('filename');
     $this->helper->saveFile('media-dir/filename');
 }
Пример #4
0
 /**
  * Process File Queue
  *
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function processFileQueue()
 {
     if (empty($this->_fileQueue)) {
         return $this;
     }
     foreach ($this->_fileQueue as &$queueOptions) {
         if (isset($queueOptions['operation']) && ($operation = $queueOptions['operation'])) {
             switch ($operation) {
                 case 'receive_uploaded_file':
                     $src = isset($queueOptions['src_name']) ? $queueOptions['src_name'] : '';
                     $dst = isset($queueOptions['dst_name']) ? $queueOptions['dst_name'] : '';
                     /** @var $uploader \Zend_File_Transfer_Adapter_Http */
                     $uploader = isset($queueOptions['uploader']) ? $queueOptions['uploader'] : null;
                     $path = dirname($dst);
                     try {
                         $rootDir = $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT);
                         $rootDir->create($rootDir->getRelativePath($path));
                     } catch (\Magento\Framework\Exception\FileSystemException $e) {
                         throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t create writeable directory "%1".', $path));
                     }
                     $uploader->setDestination($path);
                     if (empty($src) || empty($dst) || !$uploader->receive($src)) {
                         /**
                          * @todo: show invalid option
                          */
                         if (isset($queueOptions['option'])) {
                             $queueOptions['option']->setIsValid(false);
                         }
                         throw new \Magento\Framework\Exception\LocalizedException(__('The file upload failed.'));
                     }
                     $this->_fileStorageDb->saveFile($dst);
                     break;
                 default:
                     break;
             }
         }
         $queueOptions = null;
     }
     return $this;
 }
Пример #5
0
 /**
  * @return $this
  */
 public function saveFile()
 {
     if ($this->_isBaseFilePlaceholder && $this->_newFile === true) {
         return $this;
     }
     $filename = $this->_mediaDirectory->getAbsolutePath($this->getNewFile());
     $this->getImageProcessor()->save($filename);
     $this->_coreFileStorageDatabase->saveFile($filename);
     return $this;
 }