Skip to content

Commit bd9f2e8

Browse files
committed
integrate refactoring in #393
1 parent 38702a6 commit bd9f2e8

File tree

2 files changed

+70
-32
lines changed

2 files changed

+70
-32
lines changed

src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ class AvS_FastSimpleImport_Model_Import_Entity_Product extends AvS_FastSimpleImp
114114
'country_of_manufacture'
115115
);
116116

117+
/**
118+
* Downloadable files uploader
119+
*
120+
* @var AvS_FastSimpleImport_Model_Import_Uploader_Downloadable
121+
*/
122+
protected $_downloadableUploader;
123+
117124
public function __construct()
118125
{
119126
parent::__construct();
@@ -1618,8 +1625,8 @@ protected function _getUploader()
16181625
$this->_fileUploader->init();
16191626
$this->_fileUploader->removeValidateCallback('catalog_product_image');
16201627

1621-
$tmpDir = Mage::getConfig()->getOptions()->getMediaDir() . '/import';
1622-
$destDir = Mage::getConfig()->getOptions()->getMediaDir() . '/catalog/product';
1628+
$tmpDir = Mage::getConfig()->getOptions()->getMediaDir() . DS . 'import';
1629+
$destDir = Mage::getConfig()->getOptions()->getMediaDir() . DS . 'catalog' . DS . 'product';
16231630
if (!is_writable($destDir)) {
16241631
@mkdir($destDir, 0777, true);
16251632
}
@@ -1637,30 +1644,44 @@ protected function _getUploader()
16371644
return $this->_fileUploader;
16381645
}
16391646

1647+
/** Returns an object for upload downloadable files
1648+
*
1649+
* @return AvS_FastSimpleImport_Model_Import_Uploader_Downloadable
1650+
*/
1651+
protected function _getDownloadableUploader()
1652+
{
1653+
if (is_null($this->_downloadableUploader)) {
1654+
// make sure to pass null as a constructor argument, so that no upload file is set!
1655+
$this->_downloadableUploader = Mage::getModel('fastsimpleimport/import_uploader_downloadable', null);
1656+
$this->_downloadableUploader->init();
1657+
$tmpDir = Mage::getConfig()->getOptions()->getMediaDir() . DS . 'import';
1658+
$destDir = Mage::getModel('downloadable/link')->getBasePath();
1659+
if ( ! is_writable($destDir)) {
1660+
@mkdir($destDir, 0777, true);
1661+
}
1662+
if ( ! file_exists($tmpDir)) {
1663+
@mkdir($tmpDir, 0777, true);
1664+
}
1665+
if ( ! $this->_downloadableUploader->setTmpDir($tmpDir)) {
1666+
1667+
Mage::throwException("File directory '{$tmpDir}' is not readable.");
1668+
}
1669+
if ( ! $this->_downloadableUploader->setDestDir($destDir)) {
1670+
Mage::throwException("File directory '{$destDir}' is not writable.");
1671+
}
1672+
}
1673+
return $this->_downloadableUploader;
1674+
}
1675+
16401676
/**
16411677
* @param $fileName
1642-
* @return bool
1678+
*
1679+
* @return string
16431680
*/
1644-
protected function moveDownloadableFile($fileName)
1681+
protected function _moveDownloadableFile($fileName)
16451682
{
1646-
$filePath = $this->_getUploader()->getTmpDir() . $fileName;
1647-
$basePath = Mage::getModel('downloadable/link')->getBasePath();
1648-
$destDirectory = dirname(Mage::helper('downloadable/file')->getFilePath($basePath, $fileName));
1649-
// make sure that the destination directory exists!
1650-
$ioObject = new Varien_Io_File();
1651-
try {
1652-
$ioObject->open(array('path' => $destDirectory));
1653-
} catch (Exception $e) {
1654-
$ioObject->mkdir($destDirectory, 0777, true);
1655-
$ioObject->open(array('path' => $destDirectory));
1656-
}
1657-
$destFile = $basePath . DS . $fileName;
1658-
$sourceFile = realpath($filePath);
1659-
if ($sourceFile !== false) {
1660-
return copy($sourceFile, $destFile);
1661-
} else {
1662-
return false;
1663-
}
1683+
$res = $this->_getDownloadableUploader()->move($fileName);
1684+
return $res['file'];
16641685
}
16651686

16661687
/**
@@ -2066,6 +2087,15 @@ protected function _saveDownloadableLinks(array $downloadableData)
20662087
->where("$fieldToSelect = (?)", $insertValue[$linkType]));
20672088

20682089
if (!in_array($insertValue[$linkType], $insertedDownloadableLinks) && !$alreadyImported) {
2090+
2091+
if (array_key_exists($fieldToSelect, $insertValue)) {
2092+
$insertedDownloadableLinks[] = $insertValue[$fieldToSelect];
2093+
}
2094+
2095+
if ($linkType == 'file') {
2096+
$this->_moveDownloadableFile($insertValue[$fieldToSelect]);
2097+
}
2098+
20692099
$valueArr = array(
20702100
'product_id' => $productId,
20712101
'number_of_downloads' => $insertValue['number_of_downloads'],
@@ -2079,8 +2109,7 @@ protected function _saveDownloadableLinks(array $downloadableData)
20792109
$valueArr['link_type'] = 'url';
20802110
}
20812111

2082-
$this->_connection
2083-
->insertOnDuplicate($downloadableLinkTableName, $valueArr, array('product_id'));
2112+
$this->_connection->insertOnDuplicate($downloadableLinkTableName, $valueArr, array('product_id'));
20842113

20852114
$linkId = $this->_connection->fetchOne('SELECT MAX(`link_id`) FROM ' . $downloadableLinkTableName);
20862115

@@ -2090,15 +2119,8 @@ protected function _saveDownloadableLinks(array $downloadableData)
20902119
'link_id' => $linkId,
20912120
);
20922121

2093-
$this->_connection
2094-
->insertOnDuplicate($downloadableLinkTitleTableName, $valueArr, array('link_id'));
2122+
$this->_connection->insertOnDuplicate($downloadableLinkTitleTableName, $valueArr, array('link_id'));
20952123

2096-
if ($linkType == 'file') {
2097-
$this->moveDownloadableFile($insertValue[$fieldToSelect]);
2098-
}
2099-
if (array_key_exists($fieldToSelect, $insertValue)) {
2100-
$insertedDownloadableLinks[] = $insertValue[$fieldToSelect];
2101-
}
21022124
}
21032125
}
21042126
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
class AvS_FastSimpleImport_Model_Import_Uploader_Downloadable extends Mage_ImportExport_Model_Import_Uploader
4+
{
5+
6+
// set this to an empty array, so that all mime types and file extensions are allowed
7+
protected $_allowedMimeTypes = array();
8+
9+
public function init()
10+
{
11+
parent::init();
12+
// we want to support any file type, so remove the image specific validators
13+
$this->removeValidateCallback('catalog_product_image');
14+
$this->removeValidateCallback(Mage_Core_Model_File_Validator_Image::NAME);
15+
}
16+
}

0 commit comments

Comments
 (0)