Unfortunatly it did not work on my server (Magento 1.1.6) :
The function that adds annoying “_2” to image files is called 3 times in /app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php .
In order to find it, just look for :
Varien_File_Uploader::getNewFileName
You will see it is called from :
- addImage
- _moveImageFromTmp
- _copyImage
Therefore I had to make 3 changes.
addImage
public function addImage(Mage_Catalog_Model_Product $product, $file, $mediaAttribute=null, $move=false, $exclude=true) { $file = realpath($file);
if (!$file) { Mage::throwException(Mage::helper('catalog')->__('Image not exists')); }
// <HACK> just comment the two following lines : //$fileName = $dispretionPath . DS // . Varien_File_Uploader::getNewFileName($this->_getConfig()->getTmpMediaPath($fileName)); // </HACK>
$ioAdapter = new Varien_Io_File(); $ioAdapter->setAllowCreateFolders(true); $distanationDirectory = dirname($this->_getConfig()->getTmpMediaPath($fileName));
// <HACK> just comment the two following lines and add the third one : //$destFile = dirname($file) . $ioObject->dirsep() // . Varien_File_Uploader::getNewFileName($this->_getConfig()->getMediaPath($file)); $destFile = $file; //</HACK>
protected function _copyImage($file) { $ioObject = new Varien_Io_File(); $destDirectory = dirname($this->_getConfig()->getMediaPath($file)); $ioObject->open(array('path'=>$destDirectory));
// <HACK> just comment the two following lines and add the third one : //$destFile = dirname($file) . $ioObject->dirsep() // . Varien_File_Uploader::getNewFileName($this->_getConfig()->getMediaPath($file)); $destFile = $file;
// for a mysterious reason moving files does not work so let's copy it instead : //$ioObject->mv( $ioObject->cp( $this->_getConfig()->getMediaPath($file), $this->_getConfig()->getMediaPath($destFile) ); // </HACK>
Oh, I see, I forgot to mention a hack I did previously at the same time for multiple images import, in:
/www/suiclean/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php
The “true” value in the end is actually what will make the function _moveImageFromTmp to be called by addImageToMediaGallery, so without it Magento will only copy the file (function _copyImage), and since I didn’t hack this function each time you’ll launch the import a new file will be created in the temp folder, with an undesirable index appended and replicated in the final directory.
To make sure everything works fine with my hack you therefore need to enable moveImage instead of copyImage, thanks to the code above, and also delete everything in the /media/tmp folder (the advantage of moving the files is that you don’t have images remaining and getting duplicated in the tmp folder, which is not useful at all and takes space).
Anyway thanks Benjamin for your insight! Now things should be clearer to everybody.
It should be possible to write a module that extend the class, by just copy/pasting the hacked functions. However the module should be upgraded at each Magento upgrade, so that in case the original function is upgraded, the module doesn’t bypass it…
I may have a look at the module creation process, but I can’t promise anything since I am very busy right now so go ahead if you have more time, it should not be complicated and still better than hacking magento core files at each upgrade…
Very nice.
Is there a way/a place to put your modifications so that they don’t get written over by an upgrade?
Naturally, as you state, having your code as an option (or even the new default) would be the best!
Off course, one should never modify core files.
You have two solutions :
- the easy one : duplicate Media.php file in app/code/local/Mage/Catalog/Model/Product/Attribute/Backend/
But if this file is modified by an upgrade it won’t be on your server which may creates buggy behaviours…
- the hard one : create you own classe in app/code/local/MyCompany/Catalog/Model/Product/Attribute/Backend/
then only overload the functions you want to modify,
and at last you <u>may</u> have to edit app/etc/local.xml and add the proper items. </i>
Thank you for your reply. I’m new to all this, and trying to wrap my head around exactly what and where to made modifications so that they will not be overriden by updates. It’s supposed to be easy and transparent, but nothing is yets.
Does anybody know if this issue been resolved in version 1.2.0.2?
Thanks
To answer my own question, No, it is still exists in version 1.2.0.2. I’m not surprised, because now I’m used to see Magento only ADDS features and never fix them.
Any way, your hack worked like a charm. Thanks
So the original HACK you posted should appear where exactly? There are two places in the Media.php file where the code exists that you HACK in your post at _moveImageFromTmp($file) at or about line 471and _copyImage($file) at or about line 495 in version 1.3.2.2. Should both of these be hacked to work or just one?
Here’s a module that rewrites the core functionallity. It fixes these issues:
1) Image is added as excluded
2) Image with white space is not added
3) If an image is already assigned to a product it is added again.
Note: Replace [Namespace] and <Namespace> with your company name.
<?php class <Namespace>_ProductImport_Model_Product extends Mage_Catalog_Model_Convert_Adapter_Product { /** * Save product (import). Changed to add the image as not excluded. * * @param array $importData * @throws Mage_Core_Exception * @return bool */ public function saveRow(array $importData) { $product = $this->getProductModel() ->reset();
if (empty($importData['store'])) { if (!is_null($this->getBatchParams('store'))) { $store = $this->getStoreById($this->getBatchParams('store')); } else { $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'store'); Mage::throwException($message); } } else { $store = $this->getStoreByCode($importData['store']); }
if ($store === false) { $message = Mage::helper('catalog')->__('Skip import row, store "%s" field not exists', $importData['store']); Mage::throwException($message); }
if (empty($importData['sku'])) { $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'sku'); Mage::throwException($message); } $product->setStoreId($store->getId()); $productId = $product->getIdBySku($importData['sku']);
foreach ($imageData as $file => $fields) { // Make the first image the Base, Small and Thumbnail if there are no exising images and this is the only image for this record if(count($imageData) == 1 && !count($currentImages)) { $fields = array('image', 'thumbnail', 'small_image'); } if(!in_array(trim($file), $currentImages)) { try { $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . trim($file), $fields, false, false); } catch (Exception $e) {} } } /* End Modification */
nikola99, there are some bugs in that PHP. Looks like a find/replace gone wrong.
Replace curre[Namespace]mage with currentImage on lines 194-198 (case sensitive, for all you folks playing at home).
Replace co[Namespace]nue with continue on lines 119, 122, & 127.