Skip to content

Commit

Permalink
Merge branch 'v1.19.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronen-Michaeli-22 committed May 18, 2023
2 parents 884f4c4 + c28daa2 commit 32b6e1d
Show file tree
Hide file tree
Showing 25 changed files with 513 additions and 102 deletions.
15 changes: 13 additions & 2 deletions Block/Adminhtml/Cms/Wysiwyg/Images/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cloudinary\Cloudinary\Helper\MediaLibraryHelper;
use Magento\Backend\Block\Widget\Context;
use Magento\Framework\Json\EncoderInterface;
use Magento\Framework\App\ProductMetadataInterface;

/**
* Wysiwyg Images content block
Expand All @@ -20,6 +21,9 @@ class Content extends \Magento\Cms\Block\Adminhtml\Wysiwyg\Images\Content
*/
protected $mediaLibraryHelper;


protected $productMetadata;

/**
* @param Context $context
* @param EncoderInterface $jsonEncoder
Expand All @@ -30,10 +34,12 @@ public function __construct(
Context $context,
EncoderInterface $jsonEncoder,
MediaLibraryHelper $mediaLibraryHelper,
ProductMetadataInterface $productMetadata,
array $data = []
) {
parent::__construct($context, $jsonEncoder, $data);
$this->mediaLibraryHelper = $mediaLibraryHelper;
$this->productMetadata = $productMetadata;
}

/**
Expand All @@ -50,12 +56,17 @@ public function getCloudinaryMediaLibraryWidgetOptions($multiple = false, $refre
}

try {
if (version_compare($this->productMetadata->getVersion(), '2.3.5', '<=')) {
$imageUploadUrl = $this->_urlBuilder->addSessionParam()->getUrl('cloudinary/cms_wysiwyg_images/upload', ['type' => $this->_getMediaType()]);
} else {
$imageUploadUrl = $this->_urlBuilder->getUrl('cloudinary/cms_wysiwyg_images/upload', ['type' => $this->_getMediaType()]);
}

//Try to add session param on Magento versions prior to 2.3.5
$imageUploadUrl = $this->_urlBuilder->addSessionParam()->getUrl('cloudinary/cms_wysiwyg_images/upload', ['type' => $this->_getMediaType()]);

} catch (\Exception $e) {
//Catch deprecation error on Magento 2.3.5 and above
$imageUploadUrl = $this->_urlBuilder->getUrl('cloudinary/cms_wysiwyg_images/upload', ['type' => $this->_getMediaType()]);
throw new \Exception($e->getMessage());
}

return $this->_jsonEncoder->encode(
Expand Down
4 changes: 4 additions & 0 deletions Block/Adminhtml/Product/Edit/NewVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public function getWidgetOptions()
*/
protected function getNoteVideoUrl()
{
if (!$this->configuration->isModuleEnabled()) {
return parent::getNoteVideoUrl();
}

$result = __('Supported: Vimeo');
$messages = "";
if ($this->mediaHelper->getYouTubeApiKey() === null) {
Expand Down
32 changes: 32 additions & 0 deletions Block/Scripts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Cloudinary\Cloudinary\Block;

use Cloudinary\Cloudinary\Helper\MediaLibraryHelper;
use Magento\Framework\View\Element\Template;

class Scripts extends Template
{

protected $_helper;

public function __construct(
Template\Context $context,
MediaLibraryHelper $mediaHelper,
array $data = []
)
{
parent::__construct($context, $data);
$this->_helper = $mediaHelper;
}

/**
* @return void
*/
public function getCname()
{
return $this->_helper->getCname();
}


}
44 changes: 42 additions & 2 deletions Controller/Adminhtml/Cms/Wysiwyg/Images/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
use Magento\Framework\Validator\AllowedProtocols;
use Magento\MediaStorage\Model\File\Validator\NotProtectedExtension;
use Magento\MediaStorage\Model\ResourceModel\File\Storage\File;
use Magento\MediaGalleryUi\Model\UploadImage as MediaGalleryUploader;
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
use Magento\MediaGalleryApi\Api\SaveAssetsInterface;

/**
* Upload image.
Expand Down Expand Up @@ -96,6 +99,13 @@ class Upload extends \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\Upload
*/
private $mediaLibraryMapFactory;


private $mediaGalleryUploader;

protected $mediaAsset;

protected $mediaAssetSave;

/**
* @method __construct
* @param Context $context
Expand Down Expand Up @@ -127,7 +137,11 @@ public function __construct(
AllowedProtocols $protocolValidator,
NotProtectedExtension $extensionValidator,
ConfigurationInterface $configuration,
MediaLibraryMapFactory $mediaLibraryMapFactory
MediaLibraryMapFactory $mediaLibraryMapFactory,
MediaGalleryUploader $mediaGalleryUploader,
AssetInterfaceFactory $mediaAsset,
SaveAssetsInterface $mediaAssetSave

) {
parent::__construct($context, $coreRegistry, $resultJsonFactory, $directoryResolver);
$this->directoryList = $directoryList;
Expand All @@ -140,6 +154,9 @@ public function __construct(
$this->protocolValidator = $protocolValidator;
$this->configuration = $configuration;
$this->mediaLibraryMapFactory = $mediaLibraryMapFactory;
$this->mediaGalleryUploader = $mediaGalleryUploader;
$this->mediaAsset = $mediaAsset;
$this->mediaAssetSave = $mediaAssetSave;
}

/**
Expand All @@ -152,7 +169,12 @@ public function execute()
{
try {
$this->_initAction();
$path = $this->getStorage()->getSession()->getCurrentPath();
$path = ($this->getStorage()->getSession()->getCurrentPath()) ?? null;

if (!$path){
$path = $this->directoryList->getRoot() .'/pub/'. DirectoryList::MEDIA .'/cloudinary';
}

if (!$this->validatePath($path, DirectoryList::MEDIA)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Directory %1 is not under storage root path.', $path)
Expand All @@ -174,6 +196,24 @@ public function execute()
$this->getStorage()->resizeFile($localFilePath, true);
$this->imageAdapter->validateUploadFile($localFilePath);
$result = $this->appendResultSaveRemoteImage($localFilePath);
$asset = $this->getRequest()->getParam('asset');
$reg = preg_match('/^(.*)\/media\//',$localFilePath,$substruct);
$newPath = str_replace($substruct[0],'',$localFilePath);
$ma = $this->mediaAsset->create(
[
//'id' => 2020,
'path' => $newPath,
'description' => $localFileName,
'contentType' => $asset['resource_type'],
'title' => $localFileName,
'source' => 'Cloudinary',
'width' => $asset['width'],
'height' => $asset['height'],
'size' => $asset['bytes']
]
);
$this->mediaAssetSave->execute([$ma]);
// $mgu = $this->mediaGalleryUploader->execute($path, $type);
if ($this->configuration->isEnabledLocalMapping()) {
$this->saveCloudinaryMapping();
}
Expand Down
58 changes: 58 additions & 0 deletions Controller/Adminhtml/PageBuilder/MediaGallery/Upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Cloudinary\Cloudinary\Controller\Adminhtml\PageBuilder\MediaGallery;

use Magento\Backend\App\Action;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Psr\Log\LoggerInterface;
use Magento\Framework\Exception\NoSuchEntityException;

class Upload extends Action implements HttpPostActionInterface
{

private const HTTP_OK = 200;
private const HTTP_INTERNAL_ERROR = 500;
private const HTTP_BAD_REQUEST = 400;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param Action\Context $context
* @param LoggerInterface $logger
*/
public function __construct(
Action\Context $context,
LoggerInterface $logger
) {
parent::__construct($context);
$this->logger = $logger;
}

public function execute()
{
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
try{
$params = $this->getRequest()->getParams();

} catch (NoSuchEntityException $e) {
$responseCode = self::HTTP_OK;
$responseContent = [];
} catch (\Exception $e) {
$responseCode = self::HTTP_INTERNAL_ERROR;
$this->logger->critical($e);
$responseContent = [
'success' => false,
'message' => __('An error occurred on attempt to retrieve asset information.'),
];
}

$resultJson->setHttpResponseCode($responseCode);
$resultJson->setData($responseContent);

return $resultJson;
}
}
15 changes: 13 additions & 2 deletions Core/ConfigurationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@ public function __construct(ConfigurationInterface $configuration)

/**
* @return Configuration instance
* CLOUDINARY_URL=cloudinary://667623344116464:cKLIAxkU1_iPW8OCjumZ4E2i51A@m2clduat
*/
public function build()
{

$reg = $this->configuration->getCoreRegistry();
$credentials = $this->configuration->getCredentials();

$cloud = [
'cloud_name' => $credentials['cloud_name'],
'api_key' => $credentials['api_key'],
'api_secret' => $credentials['api_secret']
];

$config = array('cloud' => $this->configuration->getCredentials());
$url = array_diff($credentials, $cloud);

$config = array('cloud' => $cloud);

if ($url && is_array($url)) {
$config['url'] = $url;
}

if ($this->configuration->getCdnSubdomainStatus()) {
$config['cloud']['cdn_subdomain'] = true;
Expand Down
9 changes: 9 additions & 0 deletions Helper/MediaLibraryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ public function getCloudinaryMLshowOptions($resourceType = null, $path = "")
}
return $options;
}

/**
* @return null
*/
public function getCname()
{
$cname = isset($this->configuration->getCredentials()['cname']) ? $this->configuration->getCredentials()['cname'] : null;
return $cname;
}
}
11 changes: 11 additions & 0 deletions Helper/ProductGalleryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function getCloudinaryPGOptions($refresh = false, $ignoreDisabled = false
unset($this->cloudinaryPGoptions['custom_free_params']);
}
$this->cloudinaryPGoptions['cloudName'] = $this->getCloudName();
$this->cloudinaryPGoptions['cname'] = $this->getCname();
$this->cloudinaryPGoptions['queryParam'] = 'AB';
}

Expand All @@ -109,6 +110,16 @@ public function getCloudName()
return (string)$this->configuration->getCloud();
}


public function getCname()
{
$config = $this->configuration->getCredentials();
return ($config['cname']) ?? '';
}




/**
* @return bool
*/
Expand Down
19 changes: 7 additions & 12 deletions Model/Config/Backend/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ private function getCredentialsFromEnvironmentVariable($environmentVariable)
$environmentVariable = str_replace('CLOUDINARY_URL=', '', $environmentVariable);
$uri = parse_url($environmentVariable);
if (!isset($uri["scheme"]) || strtolower($uri["scheme"]) !== "cloudinary") {
throw new InvalidArgumentException("Invalid CLOUDINARY_URL scheme. Expecting to start with 'cloudinary://'");
throw new \InvalidArgumentException("Invalid CLOUDINARY_URL scheme. Expecting to start with 'cloudinary://'");
}
$q_params = [];
if (isset($uri["query"])) {
parse_str($uri["query"], $q_params);
}
$private_cdn = isset($uri["path"]) && $uri["path"] != "/";
$config = array_merge(
$credentials = array_merge(
$q_params,
[
"cloud_name" => $uri["host"],
Expand All @@ -172,19 +172,14 @@ private function getCredentialsFromEnvironmentVariable($environmentVariable)
"private_cdn" => $private_cdn,
]
);
if ($private_cdn) {
$config["secure_distribution"] = substr($uri["path"], 1);
}
$credentials = [
"cloud_name" => $config['cloud_name'],
"api_key" => $config['api_key'],
"api_secret" => $config['api_secret']
];
if (isset($config['private_cdn'])) {
$credentials["private_cdn"] = $config['private_cdn'];

if (isset($credentials['cname'])) {
$credentials['secure'] = true;
$credentials['secure_distribution'] = $credentials['cname'];
}

return $credentials;

} catch (\Exception $e) {
throw new ValidatorException(__(self::CREDENTIALS_CHECK_FAILED));
}
Expand Down
Loading

0 comments on commit 32b6e1d

Please sign in to comment.