Skip to content

Commit

Permalink
Merge pull request #47 from cloudinary/v2.9.7
Browse files Browse the repository at this point in the history
V2.9.7
  • Loading branch information
Pniel (Pini) Cohen authored Mar 17, 2019
2 parents 8e47b2c + a3f0430 commit 4d7cc09
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 54 deletions.
14 changes: 11 additions & 3 deletions app/code/community/Cloudinary/Cloudinary/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public function init(Mage_Catalog_Model_Product $product, $attributeName, $image
*/
public function resize($width, $height = null)
{
$this->_dimensions = Dimensions::fromWidthAndHeight($width, $height);
if ($this->_configuration->isEnabled()) {
$this->_dimensions = Dimensions::fromWidthAndHeight($width, $height);
}

return parent::resize($width, $height);
}
Expand All @@ -104,16 +106,22 @@ public function getImageUrlForCategory(Mage_Catalog_Model_Category $category)
*/
public function __toString()
{
if (!$this->_configuration->isEnabled()) {
return parent::__toString();
}
$image = $this->_imageFactory->build(
$this->_getRequestedImageFile(),
function() { return parent::__toString();}
function () {
return parent::__toString();
}
);

return $this->_urlGenerator->generateFor(
$image,
$this->_transformation->addFreeformTransformationForImage(
$this->createTransformation(),
$this->_getRequestedImageFile()
$this->_getRequestedImageFile(),
$this->getProduct()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private function filterUpdatedImages(array $imageData, array $imageUpdated)
{
return array_filter(
$imageData,
function($id) use ($imageUpdated) {
function ($id) use ($imageUpdated) {
return $imageUpdated[$id];
},
ARRAY_FILTER_USE_KEY
Expand Down Expand Up @@ -71,12 +71,32 @@ private function storeFreeTransformFields(array $imageData, Mage_Catalog_Model_P
{
$mediaImages = $this->getMediaGalleryImages($product);

// TODO: Should be removed on future releases
foreach ($imageData as $id => $freeTransform) {
Mage::getModel('cloudinary_cloudinary/transformation')
->setImageName($this->getImageNameForId($id, $mediaImages))
->setFreeTransformation($freeTransform)
->save();

$cloudinaryData = json_decode((string)$product->getCloudinaryData(), true) ?: array();
$cloudinaryData['transformation'] = (isset($cloudinaryData['transformation']))? (array) $cloudinaryData['transformation'] : array();
$cloudinaryData['transformation'][md5($this->getImageNameForId($id, $mediaImages))] = (string) $freeTransform;
$product->setCloudinaryData(json_encode($cloudinaryData));
}


/*foreach ($mediaImages as &$image) {
if (isset($imageData[$image["value_id"]])) {
$image['cloudinary_transformation'] = $imageData[$image["value_id"]];
}
}
$productPost = Mage::app()->getRequest()->getPost("product");
$productPost['media_gallery']['images'] = json_encode($mediaImages);
Mage::app()->getRequest()->setPost("product", $productPost);
$mediaGallery = $product->getMediaGallery();
$mediaGallery['images'] = $productPost['media_gallery']['images'];
$product->setData('media_gallery', $mediaGallery);*/
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public function __construct()
public function getUrl()
{
return (string) $this->_imageFactory->build(
$this->_newFile, function() { return parent::getUrl();}
$this->_newFile,
function () {
return parent::getUrl();
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

class Cloudinary_Cloudinary_Model_Catalog_Product_Media extends Mage_Core_Model_Abstract
{

private $_newImages;

public function newImagesForProduct(Mage_Catalog_Model_Product $product)
{
$this->_setNewImages($product->getData('media_gallery'));
$this->_setNewImages((array)$product->getData('media_gallery'));
return $this->_getNewImages($product);
}

Expand Down Expand Up @@ -52,4 +51,4 @@ private function _isImageRemoved($toFilter)
{
return is_array($toFilter) && array_key_exists('removed', $toFilter) && $toFilter['removed'] === 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ class Cloudinary_Cloudinary_Model_Catalog_Product_Media_Config extends Mage_Cata
*/
private $_urlGenerator;

/**
* @var Configuration
*/
private $_configuration;

public function __construct()
{
$configuration = Mage::getModel('cloudinary_cloudinary/configuration');
$this->_configuration = Mage::getModel('cloudinary_cloudinary/configuration');

$this->_imageFactory = new ImageFactory(
$configuration,
Mage::getModel('cloudinary_cloudinary/synchronizationChecker')
);
if ($this->_configuration->isEnabled()) {
$this->_imageFactory = new ImageFactory(
$this->_configuration,
Mage::getModel('cloudinary_cloudinary/synchronizationChecker')
);

$this->_urlGenerator = new UrlGenerator(
$configuration,
CloudinaryImageProvider::fromConfiguration($configuration)
);
$this->_urlGenerator = new UrlGenerator(
$this->_configuration,
CloudinaryImageProvider::fromConfiguration($this->_configuration)
);
}
}

/**
Expand All @@ -37,7 +44,13 @@ public function __construct()
*/
public function getMediaUrl($file)
{
$image = $this->_imageFactory->build($file, function() use($file) { return parent::getMediaUrl($file); });
if (!$this->_configuration->isEnabled()) {
return parent::getMediaUrl($file);
}

$image = $this->_imageFactory->build($file, function () use ($file) {
return parent::getMediaUrl($file);
});

return $this->_urlGenerator->generateFor(
$image,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

use CloudinaryExtension\CloudinaryImageProvider;
use CloudinaryExtension\Image;
use CloudinaryExtension\Image\ImageFactory;
use CloudinaryExtension\UrlGenerator;

/**
* Catalog Template Filter Model
*
* @category Mage
* @package Mage_Catalog
* @author Magento Core Team <core@magentocommerce.com>
* @todo Needs to be reimplemented to get rid of the copypasted methods
*/
class Cloudinary_Cloudinary_Model_Catalog_Template_Filter extends Mage_Catalog_Model_Template_Filter
{
/**
* @var ImageFactory
*/
private $imageFactory;

/**
* @var UrlGenerator
*/
private $urlGenerator;

/**
* @var Configuration
*/
private $configuration;

public function __construct()
{
$this->configuration = Mage::getModel('cloudinary_cloudinary/configuration');
if ($this->configuration->isEnabled()) {
$this->imageFactory = new ImageFactory(
$this->configuration,
Mage::getModel('cloudinary_cloudinary/synchronizationChecker')
);

$this->urlGenerator = new UrlGenerator(
$this->configuration,
CloudinaryImageProvider::fromConfiguration($this->configuration)
);
}
}

/**
* Retrieve media file URL directive
*
* @param array $construction
* @return string
* @see Mage_Core_Model_Email_Template_Filter::mediaDirective() method has been copypasted
*/
public function mediaDirective($construction)
{
if (!$this->configuration->isEnabled()) {
return parent::mediaDirective($construction);
}

$imagePath = $this->getImagePath($construction[2]);

$image = $this->imageFactory->build(
$imagePath,
function () use ($construction) {
return parent::mediaDirective($construction);
}
);

return $this->urlGenerator->generateFor($image);
}

private function getImagePath($directiveParams)
{
$params = $this->_getIncludeParameters($directiveParams);
return $params['url'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Cloudinary_Cloudinary_Model_System_Config_Free extends Mage_Core_Model_Config_Data
{
const ERROR_FORMAT = 'Incorrect custom transform - %s';
const ERROR_FORMAT = 'Incorrect Cloudinary Transformation - %s';
const ERROR_DEFAULT = 'please update';

/**
Expand Down Expand Up @@ -43,25 +43,41 @@ protected function _beforeSave()
->getDefaultTransformation()
->withFreeform(Freeform::fromString($this->getValue()));

$this->validateImageUrl($this->sampleImageUrl($transform));
$this->validateImageUrl($this->sampleImageUrl($transform), false);

return $this;
}

/**
* @param string $url
* @param bool $strict Throw exception on errors
* @return bool
*/
public function validateImageUrl($url)
public function validateImageUrl($url, $strict = true)
{
try {
$response = $this->httpRequest($url);
} catch (Exception $e) {
throw new Mage_Core_Exception(sprintf(self::ERROR_FORMAT, self::ERROR_DEFAULT));
$this->setValue(null);
if ($strict) {
throw new Mage_Core_Exception(sprintf(self::ERROR_FORMAT, self::ERROR_DEFAULT));
} else {
Mage::getSingleton('adminhtml/session')->addError(sprintf(self::ERROR_FORMAT, self::ERROR_DEFAULT));
}
return false;
}

if ($response->isError()) {
throw new Mage_Core_Exception($this->formatError($response));
if (is_object($response) && ($response->error || !in_array($response->code, [200,301,302]))) {
$this->setValue(null);
if ($strict) {
throw new Mage_Core_Exception($this->formatError($response));
} else {
Mage::getSingleton('adminhtml/session')->addError($this->formatError($response));
}
return false;
}

return true;
}

/**
Expand All @@ -79,11 +95,11 @@ public function defaultTransform($freeTransform)
* @param Zend_Http_Response $response
* @return string
*/
public function formatError(Zend_Http_Response $response)
public function formatError($response)
{
return sprintf(
self::ERROR_FORMAT,
$response->getStatus() == 400 ? $response->getHeader('x-cld-error') : self::ERROR_DEFAULT
(is_object($response) && isset($response->headers['x-cld-error']) && $response->headers['x-cld-error']) ? $response->headers['x-cld-error'] : self::ERROR_DEFAULT
);
}

Expand All @@ -93,9 +109,16 @@ public function formatError(Zend_Http_Response $response)
*/
public function httpRequest($url)
{
$client = new Varien_Http_Client($url);
$client->setMethod(Varien_Http_Client::GET);
return $client->request();
$curl = new Varien_Http_Adapter_Curl();
$curl->write(Zend_Http_Client::GET, $url);
$response = $curl->read();
$response = (object)[
"code" => Zend_Http_Response::extractCode($response),
"body" => Zend_Http_Response::extractBody($response),
"headers" => (array) Zend_Http_Response::extractHeaders($response),
"error" => $curl->getError()
];
return $response;
}

/**
Expand Down
Loading

0 comments on commit 4d7cc09

Please sign in to comment.