Skip to content

Commit

Permalink
Merge pull request #11 from cloudinary/v1.6.8
Browse files Browse the repository at this point in the history
V1.6.8
  • Loading branch information
Pini authored Dec 19, 2018
2 parents 9169ad3 + 9846d73 commit 313a5f1
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 42 deletions.
5 changes: 3 additions & 2 deletions Core/AutoUploadMapping/RequestProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public function __construct(
/**
* @param string $folder
* @param string $url
* @param bool $force
* @return bool
*/
public function handle($folder, $url)
public function handle($folder, $url, $force = false)
{
if ($this->configuration->isActive() == $this->configuration->getRequestState()) {
if ($this->configuration->isActive() == $this->configuration->getRequestState() && !$force) {
return true;
}

Expand Down
6 changes: 5 additions & 1 deletion Core/CloudinaryImageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ public function retrieveTransformed(Image $image, Transformation $transformation
]);

if ($this->configuration->getUseRootPath()) {
$imagePath = str_replace(".com/{$this->configuration->getCloud()}/image/upload/", ".com/{$this->configuration->getCloud()}/", $imagePath);
if (strpos($imagePath, "cloudinary.com/{$this->configuration->getCloud()}/image/upload/") !== false) {
$imagePath = str_replace("cloudinary.com/{$this->configuration->getCloud()}/image/upload/", "cloudinary.com/{$this->configuration->getCloud()}/", $imagePath);
} elseif (strpos($imagePath, "cloudinary.com/image/upload/") !== false) {
$imagePath = str_replace("cloudinary.com/image/upload/", "cloudinary.com/", $imagePath);
}
}

if ($this->configuration->getRemoveVersionNumber()) {
Expand Down
1 change: 0 additions & 1 deletion Core/Image/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Cloudinary\Cloudinary\Core\Image;

use Cloudinary\Cloudinary\Core\ConfigurationInterface;
use Cloudinary\Cloudinary\Core\Image\SynchronizationCheck;
use Cloudinary\Cloudinary\Core\Image;

class ImageFactory
Expand Down
4 changes: 2 additions & 2 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function getCdnSubdomainStatus()
*/
public function getUserPlatform()
{
return sprintf(self::USER_PLATFORM_TEMPLATE, '1.6.5', '2.0.0');
return sprintf(self::USER_PLATFORM_TEMPLATE, '1.6.8', '2.0.0');
}

/**
Expand Down Expand Up @@ -277,7 +277,7 @@ public function getRemoveVersionNumber()
*/
public function getUseRootPath()
{
return (bool) $this->configReader->getValue(self::CONFIG_PATH_REMOVE_VERSION_NUMBER);
return (bool) $this->configReader->getValue(self::CONFIG_PATH_USE_ROOT_PATH);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions Model/Observer/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,26 @@ public function __construct(
*/
public function execute(Observer $observer)
{
$force = false;
//Clear config cache if needed
$this->changedPaths = (array) $observer->getEvent()->getChangedPaths();
if (in_array($this->changedPaths, [
if (count(array_intersect($this->changedPaths, [
\Cloudinary\Cloudinary\Model\Configuration::CONFIG_PATH_ENABLED,
\Cloudinary\Cloudinary\Model\Configuration::CONFIG_PATH_ENVIRONMENT_VARIABLE,
\Cloudinary\Cloudinary\Model\AutoUploadMapping\AutoUploadConfiguration::REQUEST_PATH
])) {
])) > 0) {
$this->cleanConfigCache();
$force = true;
}

if (!$this->requestProcessor->handle('media', $this->configuration->getMediaBaseUrl())) {
if (!$this->requestProcessor->handle('media', $this->configuration->getMediaBaseUrl(), $force)) {
$this->messageManager->addErrorMessage(self::AUTO_UPLOAD_SETUP_FAIL_MESSAGE);
}
}

protected function cleanConfigCache()
{
$this->_cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
$this->_cacheTypeList->cleanType(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER);
$this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
return $this;
}
}
144 changes: 144 additions & 0 deletions Plugin/Catalog/Block/Product/ImageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

declare(strict_types=1);

namespace Cloudinary\Cloudinary\Plugin\Catalog\Block\Product;

use Cloudinary\Cloudinary\Core\ConfigurationInterface;
use Cloudinary\Cloudinary\Core\Image\ImageFactory as CloudinaryImageFactory;
use Cloudinary\Cloudinary\Core\Image\Transformation;
use Cloudinary\Cloudinary\Core\Image\Transformation\Crop;
use Cloudinary\Cloudinary\Core\Image\Transformation\Dimensions;
use Cloudinary\Cloudinary\Core\UrlGenerator;
use Cloudinary\Cloudinary\Model\Transformation as TransformationModel;
use Cloudinary\Cloudinary\Model\TransformationFactory;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Block\Product\Image as ImageBlock;
use Magento\Catalog\Block\Product\ImageFactory as CatalogImageFactory;
use Magento\Catalog\Model\Product;

class ImageFactory
{
/**
* @var CloudinaryImageFactory
*/
private $cloudinaryImageFactory;

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

/**
* @var ProductInterface
*/
private $product;

/**
* @var Dimensions
*/
private $dimensions;

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

/**
* @var string
*/
private $imageFile;

/**
* @var bool
*/
private $keepFrame;

/**
* @var TransformationModel
*/
private $transformationModel;

/**
* @param CloudinaryImageFactory $cloudinaryImageFactory
* @param UrlGenerator $urlGenerator
* @param ConfigurationInterface $configuration
*/
public function __construct(
CloudinaryImageFactory $cloudinaryImageFactory,
UrlGenerator $urlGenerator,
ConfigurationInterface $configuration,
TransformationFactory $transformationFactory
) {
$this->cloudinaryImageFactory = $cloudinaryImageFactory;
$this->urlGenerator = $urlGenerator;
$this->configuration = $configuration;
$this->transformationModel = $transformationFactory->create();
$this->dimensions = null;
$this->imageFile = null;
$this->keepFrame = true;
}

/**
* Create image block from product
* @param CatalogImageFactory $catalogImageFactory
* @param callable $proceed
* @param Product $product
* @param string $imageId
* @param array|null $attributes
* @return ImageBlock
*/
public function aroundCreate(CatalogImageFactory $catalogImageFactory, callable $proceed, Product $product, string $imageId, array $attributes = null): ImageBlock
{
$imageBlock = $proceed($product, $imageId, $attributes);
if (!$this->configuration->isEnabled()) {
return $imageBlock;
}

try {
if (strpos($imageBlock->getImageUrl(), $this->configuration->getMediaBaseUrl()) === 0) {
$imagePath = preg_replace('/^' . preg_quote($this->configuration->getMediaBaseUrl(), '/') . '/', '', $imageBlock->getImageUrl());
$imagePath = preg_replace('/\/cache\/[a-f0-9]{32}\//', '/', $imagePath);
$image = $this->cloudinaryImageFactory->build(
$imagePath,
function () use ($imageBlock) {
return $imageBlock->getImageUrl();
}
);
$generatedImageUrl = $this->urlGenerator->generateFor(
$image,
$this->transformationModel->addFreeformTransformationForImage(
$this->createTransformation($imageBlock),
$imagePath
)
);
$imageBlock->setOriginalImageUrl($imageBlock->setImageUrl());
$imageBlock->setImageUrl($generatedImageUrl);
}
} catch (\Exception $e) {
$imageBlock = $proceed($product, $imageId, $attributes);
}

return $imageBlock;
}

/**
* @param ImageBlock $imageBlock
* @return Transformation
*/
private function createTransformation(ImageBlock $imageBlock)
{
$dimensions = $this->dimensions ?: Dimensions::fromWidthAndHeight($imageBlock->getWidth(), $imageBlock->getHeight());

$transform = $this->configuration->getDefaultTransformation()->withDimensions($dimensions);

if ($this->keepFrame) {
$transform->withCrop(Crop::fromString('lpad'))
->withDimensions(Dimensions::squareMissingDimension($dimensions));
} else {
$transform->withCrop(Crop::fromString('fit'));
}

return $transform;
}
}
2 changes: 1 addition & 1 deletion Plugin/FileRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Cloudinary\Cloudinary\Plugin;

use Cloudinary\Cloudinary\Core\Image;
use Cloudinary\Cloudinary\Core\CloudinaryImageManager;
use Cloudinary\Cloudinary\Core\Image;
use Magento\Cms\Model\Wysiwyg\Images\Storage;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
Expand Down
4 changes: 1 addition & 3 deletions Plugin/FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Cloudinary\Cloudinary\Plugin;

use Cloudinary\Cloudinary\Core\Image;
use Cloudinary\Cloudinary\Core\CloudinaryImageManager;
use Cloudinary\Cloudinary\Core\Image;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\File\Uploader;

Expand Down Expand Up @@ -41,11 +41,9 @@ public function afterSave(Uploader $uploader, $result)
$filepath = $this->absoluteFilePath($result);

if ($this->isMediaFilePath($filepath) && !$this->isMediaTmpFilePath($filepath)) {

$this->cloudinaryImageManager->uploadAndSynchronise(
Image::fromPath($filepath, $this->mediaRelativePath($filepath))
);

}

return $result;
Expand Down
3 changes: 3 additions & 0 deletions Plugin/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public function beforeKeepFrame(CatalogImageHelper $helper, $flag)
*/
public function aroundGetUrl(CatalogImageHelper $helper, \Closure $originalMethod)
{
if (!$this->configuration->isEnabled()) {
return $originalMethod();
}
$imagePath = $this->imageFile ?: $this->product->getData($helper->getType());

$image = $this->imageFactory->build(sprintf('catalog/product%s', $imagePath), $originalMethod);
Expand Down
6 changes: 4 additions & 2 deletions Plugin/MediaConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ public function aroundGetMediaUrl(CatalogMediaConfig $mediaConfig, \Closure $ori
{
$image = $this->imageFactory->build(
$mediaConfig->getBaseMediaPath() . $file,
function() use ($originalMethod, $file) { return $originalMethod($file); }
function () use ($originalMethod, $file) {
return $originalMethod($file);
}
);

return $this->urlGenerator->generateFor($image);
return $this->urlGenerator->generateFor($image);
}
}
30 changes: 15 additions & 15 deletions Plugin/Widget/Model/Template/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cloudinary\Cloudinary\Plugin\Widget\Model\Template;

use Cloudinary\Cloudinary\Core\ConfigurationInterface;
use Cloudinary\Cloudinary\Core\Image\ImageFactory;
use Cloudinary\Cloudinary\Core\UrlGenerator;
use Cloudinary\Cloudinary\Model\Template\Filter as CloudinaryWidgetFilter;
Expand All @@ -12,10 +13,6 @@
*/
class Filter
{
/**
* @var StoreManagerInterface
*/
protected $_storeManager;
/**
* @var ImageFactory
*/
Expand All @@ -26,13 +23,17 @@ class Filter
*/
protected $_urlGenerator;

/**
* @var ConfigurationInterface
*/
protected $_configuration;

/**
* @var CloudinaryWidgetFilter
*/
protected $_cloudinaryWidgetFilter;

/**
* @param StoreManagerInterface $storeManager
* @param ImageFactory $imageFactory
* @param UrlGenerator $urlGenerator
* @param CloudinaryWidgetFilter $cloudinaryWidgetFilter
Expand All @@ -41,11 +42,12 @@ public function __construct(
StoreManagerInterface $storeManager,
ImageFactory $imageFactory,
UrlGenerator $urlGenerator,
ConfigurationInterface $configuration,
CloudinaryWidgetFilter $cloudinaryWidgetFilter
) {
$this->_storeManager = $storeManager;
$this->_imageFactory = $imageFactory;
$this->_urlGenerator = $urlGenerator;
$this->_configuration = $configuration;
$this->_cloudinaryWidgetFilter = $cloudinaryWidgetFilter;
}

Expand All @@ -59,21 +61,19 @@ public function __construct(
*/
public function aroundMediaDirective(\Magento\Widget\Model\Template\Filter $widgetFilter, callable $proceed, $construction)
{
if (!$this->_configuration->isEnabled()) {
return $proceed($construction);
}
$params = $this->_cloudinaryWidgetFilter->getParams($construction[2]);
if (!isset($params['url'])) {
return $proceed($construction);
}

$storeManager = $this->_storeManager;
$url = (preg_match('/^&quot;.+&quot;$/', $params['url'])) ? preg_replace('/(^&quot;)|(&quot;$)/', '', $params['url']) : $params['url'];

$image = $this->_imageFactory->build(
$params['url'],
function () use ($storeManager, $params) {
return sprintf(
'%s%s',
$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA),
$params['url']
);
$url,
function () use ($proceed, $construction) {
return $proceed($construction);
}
);

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ Run the following command under your Magento 2 root dir:

```
composer require cloudinary/cloudinary-magento2
php bin/magento maintenance:enable
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy
php bin/magento maintenance:disable
php bin/magento cache:flush
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cloudinary/cloudinary-magento2",
"description": "Cloudinary Magento 2 Integration.",
"type": "magento2-module",
"version": "1.6.5",
"version": "1.6.8",
"minimum-stability": "dev",
"license": "MIT",
"repositories": {
Expand Down
4 changes: 4 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<plugin name="cloudinary_image_helper_plugin" type="Cloudinary\Cloudinary\Plugin\ImageHelper" />
</type>

<type name="Magento\Catalog\Block\Product\ImageFactory">
<plugin name="cloudinary_plugin_catalog_block_product_imagefactory" type="Cloudinary\Cloudinary\Plugin\Catalog\Block\Product\ImageFactory" />
</type>

<type name="Magento\Catalog\Model\Product\Media\Config">
<plugin name="cloudinary_product_media_config_plugin" type="Cloudinary\Cloudinary\Plugin\MediaConfig" />
</type>
Expand Down
Loading

0 comments on commit 313a5f1

Please sign in to comment.