Example #1
0
 /**
  * Move uploaded file and create source adapter instance.
  *
  * @throws \Magento\Framework\Exception\LocalizedException
  * @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 = __('The file was not uploaded.');
         }
         throw new \Magento\Framework\Exception\LocalizedException($errorMessage);
     }
     $entity = $this->getEntity();
     /** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
     $uploader = $this->_uploaderFactory->create(['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\Exception\LocalizedException(__('The file you uploaded 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\Exception\FileSystemException $e) {
             throw new \Magento\Framework\Exception\LocalizedException(__('The source file moving process failed.'));
         }
     }
     $this->_removeBom($sourceFile);
     $this->createHistoryReport($sourceFileRelative, $entity, $extension, $result);
     // 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\Exception\LocalizedException(__($e->getMessage()));
     }
     return $sourceFile;
 }
Example #2
0
 /**
  * Validate data rows and save bunches to DB.
  *
  * @return $this|void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _saveValidatedBunches()
 {
     $source = $this->_getSource();
     $currentDataSize = 0;
     $bunchRows = [];
     $startNewBunch = false;
     $nextRowBackup = [];
     $maxDataSize = $this->_resourceHelper->getMaxDataSize();
     $bunchSize = $this->_importExportData->getBunchSize();
     $source->rewind();
     $this->_dataSourceModel->cleanBunches();
     while ($source->valid() || $bunchRows) {
         if ($startNewBunch || !$source->valid()) {
             $this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows);
             $bunchRows = $nextRowBackup;
             $currentDataSize = strlen(serialize($bunchRows));
             $startNewBunch = false;
             $nextRowBackup = [];
         }
         if ($source->valid()) {
             try {
                 $rowData = $source->current();
             } catch (\InvalidArgumentException $e) {
                 $this->addRowError($e->getMessage(), $this->_processedRowsCount);
                 $this->_processedRowsCount++;
                 $source->next();
                 continue;
             }
             $this->_processedRowsCount++;
             if ($this->validateRow($rowData, $source->key())) {
                 // add row to bunch for save
                 $rowData = $this->_prepareRowForDb($rowData);
                 $rowSize = strlen($this->jsonHelper->jsonEncode($rowData));
                 $isBunchSizeExceeded = $bunchSize > 0 && count($bunchRows) >= $bunchSize;
                 if ($currentDataSize + $rowSize >= $maxDataSize || $isBunchSizeExceeded) {
                     $startNewBunch = true;
                     $nextRowBackup = [$source->key() => $rowData];
                 } else {
                     $bunchRows[$source->key()] = $rowData;
                     $currentDataSize += $rowSize;
                 }
             }
             $source->next();
         }
     }
     return $this;
 }