Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Block/Adminhtml/Product/Edit/NewVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getWidgetOptions()
'htmlId' => $this->getHtmlId(),
'youTubeApiKey' => $this->mediaHelper->getYouTubeApiKey(),
'videoSelector' => $this->videoSelector,
'cloudinaryPlaceholder' => $this->getViewFileUrl('Cloudinary_Cloudinary::images/cloudinary_logo_for_white_bg.jpg')
'cloudinaryPlaceholder' => $this->getViewFileUrl('Cloudinary_Cloudinary::images/cloudinary_vertical_logo_for_white_bg.svg')
]
);
}
Expand Down
24 changes: 15 additions & 9 deletions Core/CloudinaryImageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
namespace Cloudinary\Cloudinary\Core;

use Cloudinary;
use Cloudinary\Uploader;
use Cloudinary\Cloudinary\Core\Exception\ApiError;
use Cloudinary\Cloudinary\Core\Image\Transformation;
use Cloudinary\Cloudinary\Core\Security;
use Cloudinary\Cloudinary\Core\Image\Transformation\Format;
use Cloudinary\Cloudinary\Core\Image\Transformation\FetchFormat;
use Cloudinary\Uploader;

class CloudinaryImageProvider implements ImageProvider
{
Expand Down Expand Up @@ -57,7 +54,8 @@ public function __construct(
* @param ConfigurationInterface $configuration
* @return CloudinaryImageProvider
*/
public static function fromConfiguration(ConfigurationInterface $configuration){
public static function fromConfiguration(ConfigurationInterface $configuration)
{
return new CloudinaryImageProvider(
$configuration,
new ConfigurationBuilder($configuration),
Expand Down Expand Up @@ -94,10 +92,18 @@ public function upload(Image $image)
*/
public function retrieveTransformed(Image $image, Transformation $transformation)
{
return Image::fromPath(
\cloudinary_url($image->getId(), ['transformation' => $transformation->build(), 'secure' => true]),
$image->getRelativePath()
);
$imagePath = \cloudinary_url($image->getId(), ['transformation' => $transformation->build(), 'secure' => true]);

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

if ($this->configuration->getRemoveVersionNumber()) {
$regex = '/\/v[0-9]+\/' . preg_quote(ltrim($image->getId(), '/'), '/') . '$/';
$imagePath = preg_replace($regex, '/' . ltrim($image->getId(), '/'), $imagePath);
}

return Image::fromPath($imagePath, $image->getRelativePath());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Model/AutoUploadMapping/AutoUploadConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Cloudinary\Cloudinary\Model\AutoUploadMapping;

use Cloudinary\Cloudinary\Core\AutoUploadMapping\AutoUploadConfigurationInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Cloudinary\Cloudinary\Core\AutoUploadMapping\AutoUploadConfigurationInterface;

class AutoUploadConfiguration implements AutoUploadConfigurationInterface
{
Expand Down
47 changes: 45 additions & 2 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,28 @@
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Store\Model\StoreManagerInterface;

class Configuration implements ConfigurationInterface
{
const CONFIG_PATH_ENABLED = 'cloudinary/cloud/cloudinary_enabled';
const USER_PLATFORM_TEMPLATE = 'CloudinaryMagento/%s (Magento %s)';
const CONFIG_PATH_ENVIRONMENT_VARIABLE = 'cloudinary/setup/cloudinary_environment_variable';
const CONFIG_CDN_SUBDOMAIN = 'cloudinary/configuration/cloudinary_cdn_subdomain';
//= Transformations
const CONFIG_DEFAULT_GRAVITY = 'cloudinary/transformations/cloudinary_gravity';
const CONFIG_DEFAULT_QUALITY = 'cloudinary/transformations/cloudinary_image_quality';
const CONFIG_DEFAULT_DPR = 'cloudinary/transformations/cloudinary_image_dpr';
const CONFIG_DEFAULT_FETCH_FORMAT = 'cloudinary/transformations/cloudinary_fetch_format';
const CONFIG_GLOBAL_FREEFORM = 'cloudinary/transformations/cloudinary_free_transform_global';
//= Advanced
const CONFIG_PATH_REMOVE_VERSION_NUMBER = 'cloudinary/advanced/remove_version_number';
const CONFIG_PATH_USE_ROOT_PATH = 'cloudinary/advanced/use_root_path';
//= Others
const CONFIG_PATH_SECURE_BASE_URL = "web/secure/base_url";
const CONFIG_PATH_UNSECURE_BASE_URL = "web/unsecure/base_url";
const CONFIG_PATH_USE_SECURE_IN_FRONTEND = "web/secure/use_in_frontend";

const USE_FILENAME = true;
const UNIQUE_FILENAME = false;
const OVERWRITE = false;
Expand Down Expand Up @@ -62,24 +72,32 @@ class Configuration implements ConfigurationInterface
*/
private $autoUploadConfiguration;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param ScopeConfigInterface $configReader
* @param WriterInterface $configWriter
* @param EncryptorInterface $decryptor
* @param AutoUploadConfigurationInterface $autoUploadConfiguration
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ScopeConfigInterface $configReader,
WriterInterface $configWriter,
EncryptorInterface $decryptor,
AutoUploadConfigurationInterface $autoUploadConfiguration,
\Psr\Log\LoggerInterface $logger
\Psr\Log\LoggerInterface $logger,
StoreManagerInterface $storeManager
) {
$this->configReader = $configReader;
$this->configWriter = $configWriter;
$this->decryptor = $decryptor;
$this->autoUploadConfiguration = $autoUploadConfiguration;
$this->logger = $logger;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -132,7 +150,7 @@ public function getCdnSubdomainStatus()
*/
public function getUserPlatform()
{
return sprintf(self::USER_PLATFORM_TEMPLATE, '1.6.0', '2.0.0');
return sprintf(self::USER_PLATFORM_TEMPLATE, '1.6.3', '2.0.0');
}

/**
Expand Down Expand Up @@ -236,4 +254,29 @@ private function getEnvironmentVariable()
}
return $this->environmentVariable;
}

/**
* @return bool
*/
public function getRemoveVersionNumber()
{
return (bool) $this->configReader->getValue(self::CONFIG_PATH_REMOVE_VERSION_NUMBER);
}

/**
* @return bool
*/
public function getUseRootPath()
{
return (bool) $this->configReader->getValue(self::CONFIG_PATH_REMOVE_VERSION_NUMBER);
}

/**
* @method getMediaBaseUrl
* @return string
*/
public function getMediaBaseUrl()
{
return $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}
}
61 changes: 39 additions & 22 deletions Model/Observer/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Cloudinary\Cloudinary\Model\Observer;

use Cloudinary\Cloudinary\Core\CloudinaryImageManager;
use Magento\Framework\App\ObjectManager;
use Cloudinary\Cloudinary\Core\AutoUploadMapping\RequestProcessor;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Cloudinary\Cloudinary\Core\AutoUploadMapping\RequestProcessor;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\UrlInterface;

class Configuration implements ObserverInterface
{
Expand All @@ -17,48 +15,67 @@ class Configuration implements ObserverInterface
/**
* @var RequestProcessor
*/
private $requestProcessor;
protected $requestProcessor;

/**
* @var ManagerInterface
*/
private $messageManager;
protected $messageManager;

/**
* @var \Cloudinary\Cloudinary\Model\Configuration
*/
protected $configuration;

/**
* @var TypeListInterface
*/
protected $cacheTypeList;

protected $changedPaths = [];

/**
* @param RequestProcessor $requestProcessor
* @param ManagerInterface $messageManager
* @param \Cloudinary\Cloudinary\Model\Configuration $configuration
* @param TypeListInterface $cacheTypeList
*/
public function __construct(
RequestProcessor $requestProcessor,
ManagerInterface $messageManager
ManagerInterface $messageManager,
\Cloudinary\Cloudinary\Model\Configuration $configuration,
TypeListInterface $cacheTypeList
) {
$this->requestProcessor = $requestProcessor;
$this->messageManager = $messageManager;
$this->configuration = $configuration;
$this->cacheTypeList = $cacheTypeList;
}

/**
* @param Observer $observer
*/
public function execute(Observer $observer)
{
if (!$this->requestProcessor->handle('media', $this->getMediaBaseUrl())) {
//Clear config cache if needed
$this->changedPaths = (array) $observer->getEvent()->getChangedPaths();
if (in_array($this->changedPaths, [
\Cloudinary\Cloudinary\Model\Configuration::CONFIG_PATH_ENABLED,
\Cloudinary\Cloudinary\Model\Configuration::CONFIG_PATH_ENVIRONMENT_VARIABLE,
\Cloudinary\Cloudinary\Model\AutoUploadMapping\AutoUploadConfiguration::REQUEST_PATH
])) {
$this->cleanConfigCache();
}

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

/**
* @return string
*/
function getMediaBaseUrl() {
/** @var \Magento\Framework\ObjectManagerInterface $om */
$om = ObjectManager::getInstance();

/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
$storeManager = $om->get('Magento\Store\Model\StoreManagerInterface');

/** @var \Magento\Store\Api\Data\StoreInterface|\Magento\Store\Model\Store $currentStore */
$currentStore = $storeManager->getStore();

return $currentStore->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
protected function cleanConfigCache()
{
$this->_cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
$this->_cacheTypeList->cleanType(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER);
return $this;
}
}
4 changes: 2 additions & 2 deletions Model/Template/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Cloudinary\Cloudinary\Model\Template;

use Magento\Widget\Model\Template\Filter as WidgetFilter;
use Cloudinary\Cloudinary\Core\Image\ImageFactory;
use Cloudinary\Cloudinary\Core\UrlGenerator;
use Magento\Widget\Model\Template\Filter as WidgetFilter;

class Filter extends WidgetFilter
{
Expand Down Expand Up @@ -92,7 +92,7 @@ public function mediaDirective($construction)

$image = $this->imageFactory->build(
$params['url'],
function() use ($storeManager, $params) {
function () use ($storeManager, $params) {
return sprintf(
'%s%s',
$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA),
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@ Magento 2 module for integration with Cloudinary.

---

## ✓ Installation (using git & composer)
Run the following command under your Magento 2 root dir:

```
git clone https://github.com/cloudinary/cloudinary_magento2 app/code/Cloudinary/Cloudinary
composer require cloudinary/cloudinary_php:1.8.0
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy
php bin/magento cache:flush
```

---

https://www.cloudinary.com/

Copyright © 2018 Cloudinary. All rights reserved.

![Cloudinary Logo](https://cloudinary-res.cloudinary.com/image/upload/v1538583988/cloudinary_logo_for_white_bg.svg)
![Cloudinary Logo](https://cloudinary-res.cloudinary.com/image/upload/c_scale,w_300/v1/logo/for_white_bg/cloudinary_logo_for_white_bg.svg)
52 changes: 52 additions & 0 deletions Setup/UpgradeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Cloudinary\Cloudinary\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

/**
* Upgrade Data script
* @codeCoverageIgnore
*/
class UpgradeData implements UpgradeDataInterface
{

/**
* @param ResourceConnection
*/
protected $_resourceConnection;

/**
* Init
* @method __construct
* @param EavSetupFactory $eavSetupFactory
* @param ResourceConnection $resourceConnection
*/
public function __construct(
ResourceConnection $resourceConnection
) {
$this->_resourceConnection = $resourceConnection;
}

/**
* {@inheritdoc}
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

if (version_compare($context->getVersion(), '0.6.3') < 0) {
echo "- Reseting configurations for 'website' & 'store' scopes (only supports 'default' at the moment).\n";
$this->_resourceConnection->getConnection()->delete(
$this->_resourceConnection->getTableName('core_config_data'),
"path LIKE 'cloudinary/%' AND scope != 'default'"
);
}

$setup->endSetup();
}
}
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/magento2-module-cloudinary",
"description": "Cloudinary Magento 2 Integration.",
"type": "magento2-module",
"version": "1.6.0",
"version": "1.6.3",
"minimum-stability": "dev",
"license": "MIT",
"repositories": {
Expand Down
Loading