Esempio n. 1
0
 /**
  * Test that isObjectAvailable() works if object name contains spaces
  *
  * @depends testCreateBucket
  * @depends testObjectPath
  *
  * ZF-10017
  */
 public function testIsObjectAvailableWithSpacesInKey()
 {
     $this->_amazon->createBucket($this->_bucket);
     $filedir = dirname(__FILE__) . "/_files/";
     $key = $this->_bucket . '/subdir/another dir with spaces/zftestfile.html';
     $this->_amazon->putFile($filedir . "testdata.html", $key);
     $this->assertTrue($this->_amazon->isObjectAvailable($key));
 }
Esempio n. 2
0
 /**
  * @depends testCopyObject
  * @depends testRemoveObject
  */
 public function testMoveObject()
 {
     $this->_amazon->createBucket($this->_bucket);
     $data = "testdata";
     $this->_amazon->putObject($this->_bucket . "/zftest", $data);
     $info1 = $this->_amazon->getInfo($this->_bucket . "/zftest");
     $this->_amazon->moveObject($this->_bucket . "/zftest", $this->_bucket . "/zftest2");
     $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket . "/zftest"));
     $this->assertTrue($this->_amazon->isObjectAvailable($this->_bucket . "/zftest2"));
     $info2 = $this->_amazon->getInfo($this->_bucket . "/zftest2");
     $this->assertEquals($info1['etag'], $info2['etag']);
 }
Esempio n. 3
0
 public function testPutNoFile()
 {
     $filedir = dirname(__FILE__) . "/_files/";
     try {
         $this->_amazon->putFile($filedir . "nosuchfile", $this->_bucket . "/zftestfile");
         $this->fail("Expected exception not thrown");
     } catch (Zend_Service_Amazon_S3_Exception $e) {
         $this->assertContains("Cannot read", $e->getMessage());
         $this->assertContains("nosuchfile", $e->getMessage());
     }
     $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket . "/zftestfile"));
 }
Esempio n. 4
0
 /**
  * Remove a "stored" file.
  *
  * @param string $path
  */
 public function delete($path)
 {
     $objectName = $this->_getObjectName($path);
     $status = $this->_s3->removeObject($objectName);
     if (!$status) {
         if ($this->_s3->isObjectAvailable($objectName)) {
             throw new Omeka_Storage_Exception('Unable to delete file.');
         } else {
             _log("Omeka_Storage_Adapter_ZendS3: Tried to delete missing object '{$objectName}'.", Zend_Log::WARN);
         }
     } else {
         _log("Omeka_Storage_Adapter_ZendS3: Removed object '{$objectName}'.");
     }
 }
Esempio n. 5
0
 public function upload($observer)
 {
     $product = $observer->getEvent()->getProduct();
     //There's nothing to process because we're using images
     //from original product in duplicate
     if ($product->getIsDuplicate() || $product->getData('mventory_update_duplicate')) {
         return;
     }
     $images = $observer->getEvent()->getImages();
     //Use product helper from MVentory_API if it's installed and is activated
     //The helper is used to get correct store for the product when MVentory_API
     //extension is used
     //Change current store if product's store is different for correct
     //file name of images
     if (Mage::helper('core')->isModuleEnabled('MVentory_API')) {
         $store = Mage::helper('mventory/product')->getWebsite($product)->getDefaultStore();
         $changeStore = $store->getId() != Mage::app()->getStore()->getId();
     } else {
         $store = Mage::app()->getStore();
         $changeStore = false;
     }
     //Get settings for S3
     $accessKey = $store->getConfig(MVentory_CDN_Model_Config::ACCESS_KEY);
     $secretKey = $store->getConfig(MVentory_CDN_Model_Config::SECRET_KEY);
     $bucket = $store->getConfig(MVentory_CDN_Model_Config::BUCKET);
     $prefix = $store->getConfig(MVentory_CDN_Model_Config::PREFIX);
     $dimensions = $store->getConfig(MVentory_CDN_Model_Config::DIMENSIONS);
     $cacheTime = (int) $store->getConfig(MVentory_CDN_Model_Config::CACHE_TIME);
     //Return if S3 settings are empty
     if (!($accessKey && $secretKey && $bucket && $prefix)) {
         return;
     }
     //Build prefix for all files on S3
     $cdnPrefix = $bucket . '/' . $prefix . '/';
     //Parse dimension. Split string to pairs of width and height
     $dimensions = str_replace(', ', ',', $dimensions);
     $dimensions = explode(',', $dimensions);
     //Prepare meta data for uploading. All uploaded images are public
     $meta = array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ);
     if ($cacheTime > 0) {
         $meta[MVentory_CDN_Model_Config::AMAZON_CACHE_CONTROL] = 'max-age=' . $cacheTime;
     }
     if ($changeStore) {
         $emu = Mage::getModel('core/app_emulation');
         $origEnv = $emu->startEnvironmentEmulation($store);
     }
     $config = Mage::getSingleton('catalog/product_media_config');
     $s3 = new Zend_Service_Amazon_S3($accessKey, $secretKey);
     foreach ($images['images'] as &$image) {
         //Process new images only
         if (isset($image['value_id'])) {
             continue;
         }
         //Get name of the image and create its key on S3
         $fileName = $image['file'];
         $cdnPath = $cdnPrefix . 'full' . $fileName;
         //Full path to uploaded image
         $file = $config->getMediaPath($fileName);
         //Check if object with the key exists
         if ($s3->isObjectAvailable($cdnPath)) {
             $position = strrpos($fileName, '.');
             //Split file name and extension
             $name = substr($fileName, 0, $position);
             $ext = substr($fileName, $position);
             //Search key
             $_key = $prefix . '/full' . $name . '_';
             //Get all objects which is started with the search key
             $keys = $s3->getObjectsByBucket($bucket, array('prefix' => $_key));
             $index = 1;
             //If there're objects which names begin with the search key then...
             if (count($keys)) {
                 $extLength = strlen($ext);
                 $_keys = array();
                 //... store object names without extension as indeces of the array
                 //for fast searching
                 foreach ($keys as $key) {
                     $_keys[substr($key, 0, -$extLength)] = true;
                 }
                 //Find next unused object name
                 while (isset($_keys[$_key . $index])) {
                     ++$index;
                 }
                 unset($_keys);
             }
             //Build new name and path with selected index
             $fileName = $name . '_' . $index . $ext;
             $cdnPath = $cdnPrefix . 'full' . $fileName;
             //Get new name for uploaded file
             $_file = $config->getMediaPath($fileName);
             //Rename file uploaded to Magento
             rename($file, $_file);
             //Update values of media attribute in the product after renaming
             //uploaded image if the image was marked as 'image', 'small_image'
             //or 'thumbnail' in the product
             foreach ($product->getMediaAttributes() as $mediaAttribute) {
                 $code = $mediaAttribute->getAttributeCode();
                 if ($product->getData($code) == $image['file']) {
                     $product->setData($code, $fileName);
                 }
             }
             //Save its new name in Magento
             $image['file'] = $fileName;
             $file = $_file;
             unset($_file);
         }
         //Upload original image
         if (!$s3->putFile($file, $cdnPath, $meta)) {
             $msg = 'Can\'t upload original image (' . $file . ') to S3 with ' . $cdnPath . ' key';
             if ($changeStore) {
                 $emu->stopEnvironmentEmulation($origEnv);
             }
             throw new Mage_Core_Exception($msg);
         }
         //Go to next newly uploaded image if image dimensions for resizing
         //were not set
         if (!count($dimensions)) {
             continue;
         }
         //For every dimension...
         foreach ($dimensions as $dimension) {
             //... resize original image and get path to resized image
             $newFile = Mage::getModel('catalog/product_image')->setDestinationSubdir('image')->setSize($dimension)->setKeepFrame(false)->setConstrainOnly(true)->setBaseFile($fileName)->resize()->saveFile()->getNewFile();
             //Build S3 path for the resized image
             $newCdnPath = $cdnPrefix . $dimension . $fileName;
             //Upload resized images
             if (!$s3->putFile($newFile, $newCdnPath, $meta)) {
                 $msg = 'Can\'t upload resized (' . $dimension . ') image (' . $file . ') to S3 with ' . $cdnPath . ' key';
                 if ($changeStore) {
                     $emu->stopEnvironmentEmulation($origEnv);
                 }
                 throw new Mage_Core_Exception($msg);
             }
         }
     }
     if ($changeStore) {
         $emu->stopEnvironmentEmulation($origEnv);
     }
 }
Esempio n. 6
0
 public function exists($filename)
 {
     $this->_initApi();
     return $this->_api->isObjectAvailable($this->_config['bucket'] . $this->_getUri($filename));
 }
 foreach ($destSubdirs as $destSubdir) {
     $placeholder = Mage::getModel('catalog/product_image')->setDestinationSubdir($destSubdir)->setBaseFile(null)->getBaseFile();
     $result = copy($placeholder, $config->getMediaPath(basename($placeholder)));
     if ($result === true) {
         $images[] = '/' . basename($placeholder);
     } else {
         Mage::log('Error on copy ' . $placeholder . ' to media folder', null, 's3.log');
     }
 }
 $s3 = new Zend_Service_Amazon_S3($accessKey, $secretKey);
 $imageNumber = 1;
 foreach ($images as $fileName) {
     Mage::log('Processing image ' . $imageNumber++ . ' of ' . $totalImages, null, 's3.log');
     $cdnPath = $cdnPrefix . 'full' . $fileName;
     $file = $config->getMediaPath($fileName);
     if (!$s3->isObjectAvailable($cdnPath)) {
         Mage::log('Trying to upload original file ' . $file . ' as ' . $cdnPath, null, 's3.log');
         try {
             $s3->putFile($file, $cdnPath, $meta);
         } catch (Exception $e) {
             Mage::log($e->getMessage(), null, 's3.log');
             continue;
         }
     } else {
         Mage::log('File ' . $file . ' has been already uploaded', null, 's3.log');
     }
     if (!count($dimensions)) {
         continue;
     }
     foreach ($dimensions as $dimension) {
         $newCdnPath = $cdnPrefix . $dimension . $fileName;