From ffaeffbf0ad9691c087eee2db04271a9cf59f515 Mon Sep 17 00:00:00 2001 From: Simon Sprankel Date: Thu, 20 Jul 2017 12:28:15 +0200 Subject: [PATCH 1/4] refactored downloadable files import - files are created in subfolders --- .../Model/Import/Entity/Product.php | 97 +++++++++++-------- .../Model/Import/Uploader/Downloadable.php | 18 ++++ 2 files changed, 76 insertions(+), 39 deletions(-) create mode 100644 src/app/code/community/AvS/FastSimpleImport/Model/Import/Uploader/Downloadable.php diff --git a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php index d9d6154e..134caa6c 100644 --- a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php +++ b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php @@ -114,6 +114,13 @@ class AvS_FastSimpleImport_Model_Import_Entity_Product extends AvS_FastSimpleImp 'country_of_manufacture' ); + /** + * Downloadable files uploader + * + * @var Mage_ImportExport_Model_Import_Uploader + */ + protected $_downloadableUploader; + public function __construct() { parent::__construct(); @@ -1612,29 +1619,46 @@ protected function _getUploader() } /** - * @param $fileName - * @return bool + * Returns an object for upload downloadable files + * + * @return Mage_ImportExport_Model_Import_Uploader */ - protected function moveDownloadableFile($fileName) + protected function _getDownloadableUploader() { - $filePath = $this->_getUploader()->getTmpDir() . $fileName; - $basePath = Mage::getModel('downloadable/link')->getBasePath(); - $destDirectory = dirname(Mage::helper('downloadable/file')->getFilePath($basePath, $fileName)); - // make sure that the destination directory exists! - $ioObject = new Varien_Io_File(); - try { - $ioObject->open(array('path' => $destDirectory)); - } catch (Exception $e) { - $ioObject->mkdir($destDirectory, 0777, true); - $ioObject->open(array('path' => $destDirectory)); - } - $destFile = $basePath . DS . $fileName; - $sourceFile = realpath($filePath); - if ($sourceFile !== false) { - return copy($sourceFile, $destFile); - } else { - return false; + if (is_null($this->_downloadableUploader)) { + // make sure to pass null as a constructor argument, so that no upload file is set! + $this->_downloadableUploader = Mage::getModel('fastsimpleimport/import_uploader_downloadable', null); + $this->_downloadableUploader->init(); + + $tmpDir = Mage::getConfig()->getOptions()->getMediaDir() . '/import'; + $destDir = Mage::getModel('downloadable/link')->getBasePath(); + if ( ! is_writable($destDir)) { + @mkdir($destDir, 0777, true); + } + if ( ! file_exists($tmpDir)) { + @mkdir($tmpDir, 0777, true); + } + if ( ! $this->_downloadableUploader->setTmpDir($tmpDir)) { + Mage::throwException("File directory '{$tmpDir}' is not readable."); + } + if ( ! $this->_downloadableUploader->setDestDir($destDir)) { + Mage::throwException("File directory '{$destDir}' is not writable."); + } } + + return $this->_downloadableUploader; + } + + /** + * @param $fileName + * + * @return string + */ + protected function _moveDownloadableFile($fileName) + { + $res = $this->_getDownloadableUploader()->move($fileName); + + return $res['file']; } /** @@ -2012,11 +2036,11 @@ protected function _saveDownloadableLinks(array $downloadableData) return $this; } - $downloadableLinkTableName = $this->_getDownloadableLinksTableName(); + $downloadableLinkTableName = $this->_getDownloadableLinksTableName(); $downloadableLinkTitleTableName = $this->_getDownloadableLinksTitleTableName(); foreach ($downloadableData as $productSku => $downloadableLink) { - $productId = $this->_newSku[$productSku]['entity_id']; + $productId = $this->_newSku[$productSku]['entity_id']; $insertedDownloadableLinks = []; if (Mage_ImportExport_Model_Import::BEHAVIOR_APPEND != $this->getBehavior()) { @@ -2032,32 +2056,27 @@ protected function _saveDownloadableLinks(array $downloadableData) ->where('product_id IN (?)', $productId) ->where('link_file = (?)', $insertValue['file'])); - if (!in_array($insertValue['file'], $insertedDownloadableLinks) && !$alreadyImported) { - $valueArr = array( - 'product_id' => $productId, - 'link_file' => $insertValue['file'], - 'link_type' => 'file', + if ( ! in_array($insertValue['file'], $insertedDownloadableLinks) && ! $alreadyImported) { + $insertedDownloadableLinks[] = $insertValue['file']; + $linkFile = $this->_moveDownloadableFile($insertValue['file']); + $valueArr = array( + 'product_id' => $productId, + 'link_file' => $linkFile, + 'link_type' => 'file', 'number_of_downloads' => $insertValue['number_of_downloads'], ); - $this->_connection - ->insertOnDuplicate($downloadableLinkTableName, $valueArr, array('product_id')); + $this->_connection->insertOnDuplicate($downloadableLinkTableName, $valueArr, array('product_id')); $linkId = $this->_connection->fetchOne('SELECT MAX(`link_id`) FROM ' . $downloadableLinkTableName); $valueArr = array( - 'store_id' => $insertValue['store_id'], - 'title' => $insertValue['title'], - 'link_id' => $linkId, + 'store_id' => $insertValue['store_id'], + 'title' => $insertValue['title'], + 'link_id' => $linkId, ); - $this->_connection - ->insertOnDuplicate($downloadableLinkTitleTableName, $valueArr, array('link_id')); - - $this->moveDownloadableFile($insertValue['file']); - $insertedDownloadableLinks[] = $insertValue['file']; - - + $this->_connection->insertOnDuplicate($downloadableLinkTitleTableName, $valueArr, array('link_id')); } } } diff --git a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Uploader/Downloadable.php b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Uploader/Downloadable.php new file mode 100644 index 00000000..48cb8b04 --- /dev/null +++ b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Uploader/Downloadable.php @@ -0,0 +1,18 @@ +removeValidateCallback('catalog_product_image'); + $this->removeValidateCallback(Mage_Core_Model_File_Validator_Image::NAME); + } + +} From 7c114746d71cbc96859fc316e8dfd85f0af56a9f Mon Sep 17 00:00:00 2001 From: Simon Sprankel Date: Thu, 20 Jul 2017 12:29:28 +0200 Subject: [PATCH 2/4] fixed class hint --- .../AvS/FastSimpleImport/Model/Import/Entity/Product.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php index 134caa6c..b8c0a43c 100644 --- a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php +++ b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php @@ -117,7 +117,7 @@ class AvS_FastSimpleImport_Model_Import_Entity_Product extends AvS_FastSimpleImp /** * Downloadable files uploader * - * @var Mage_ImportExport_Model_Import_Uploader + * @var AvS_FastSimpleImport_Model_Import_Uploader_Downloadable */ protected $_downloadableUploader; From 3bd93f53c744c046376bb9e925998f74900b4489 Mon Sep 17 00:00:00 2001 From: Simon Sprankel Date: Fri, 21 Jul 2017 08:11:07 +0200 Subject: [PATCH 3/4] fixed another type hint --- .../AvS/FastSimpleImport/Model/Import/Entity/Product.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php index b8c0a43c..f214fc7f 100644 --- a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php +++ b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php @@ -1621,7 +1621,7 @@ protected function _getUploader() /** * Returns an object for upload downloadable files * - * @return Mage_ImportExport_Model_Import_Uploader + * @return AvS_FastSimpleImport_Model_Import_Uploader_Downloadable */ protected function _getDownloadableUploader() { From b700b516d23d6db86d0346c75aecfa660e6df6e6 Mon Sep 17 00:00:00 2001 From: Simon Sprankel Date: Fri, 21 Jul 2017 08:12:45 +0200 Subject: [PATCH 4/4] replaced slashes in paths with DS constant --- .../AvS/FastSimpleImport/Model/Import/Entity/Product.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php index f214fc7f..e9256947 100644 --- a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php +++ b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php @@ -1599,8 +1599,8 @@ protected function _getUploader() $this->_fileUploader->init(); $this->_fileUploader->removeValidateCallback('catalog_product_image'); - $tmpDir = Mage::getConfig()->getOptions()->getMediaDir() . '/import'; - $destDir = Mage::getConfig()->getOptions()->getMediaDir() . '/catalog/product'; + $tmpDir = Mage::getConfig()->getOptions()->getMediaDir() . DS . 'import'; + $destDir = Mage::getConfig()->getOptions()->getMediaDir() . DS . 'catalog' . DS . 'product'; if (!is_writable($destDir)) { @mkdir($destDir, 0777, true); } @@ -1630,7 +1630,7 @@ protected function _getDownloadableUploader() $this->_downloadableUploader = Mage::getModel('fastsimpleimport/import_uploader_downloadable', null); $this->_downloadableUploader->init(); - $tmpDir = Mage::getConfig()->getOptions()->getMediaDir() . '/import'; + $tmpDir = Mage::getConfig()->getOptions()->getMediaDir() . DS . 'import'; $destDir = Mage::getModel('downloadable/link')->getBasePath(); if ( ! is_writable($destDir)) { @mkdir($destDir, 0777, true);