Skip to content

Commit e15f9d6

Browse files
author
Pniel (Pini) Cohen
authored
Merge pull request #51 from cloudinary/v1.10.5
v1.10.5
2 parents e51f0ad + e042bb4 commit e15f9d6

22 files changed

+762
-31
lines changed

Controller/Adminhtml/Ajax/RetrieveImage.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ public function execute()
158158
$this->validateRemoteFile($this->remoteFileUrl);
159159
$this->parsedRemoteFileUrl = $this->configuration->parseCloudinaryUrl($this->remoteFileUrl);
160160
$this->parsedRemoteFileUrl["transformations_string"] = $this->getRequest()->getParam('asset')["free_transformation"];
161+
$assetParsedRemoteFileUrl = $this->configuration->parseCloudinaryUrl($this->getRequest()->getParam('asset')["asset_url"]);
162+
$this->parsedRemoteFileUrl["type"] = $assetParsedRemoteFileUrl['type'];
163+
$this->parsedRemoteFileUrl["thumbnail_url"] = $assetParsedRemoteFileUrl['thumbnail_url'];
161164
$baseTmpMediaPath = $this->getBaseTmpMediaPath();
162165
if ($this->configuration->isEnabledLocalMapping()) {
163166
$this->cldUniqid = $this->configuration->generateCLDuniqid();
@@ -325,7 +328,7 @@ private function saveCloudinaryMapping()
325328
{
326329
return $this->mediaLibraryMapFactory->create()
327330
->setCldUniqid($this->cldUniqid)
328-
->setCldPublicId($this->parsedRemoteFileUrl["publicId"] . '.' . $this->parsedRemoteFileUrl["extension"])
331+
->setCldPublicId(($this->parsedRemoteFileUrl["type"] === "video") ? $this->parsedRemoteFileUrl["thumbnail_url"] : $this->parsedRemoteFileUrl["publicId"] . '.' . $this->parsedRemoteFileUrl["extension"])
329332
->setFreeTransformation($this->parsedRemoteFileUrl["transformations_string"])
330333
->save();
331334
}

Controller/Adminhtml/Cms/Wysiwyg/Images/Upload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private function saveCloudinaryMapping()
308308
{
309309
return $this->mediaLibraryMapFactory->create()
310310
->setCldUniqid($this->cldUniqid)
311-
->setCldPublicId($this->parsedRemoteFileUrl["publicId"] . '.' . $this->parsedRemoteFileUrl["extension"])
311+
->setCldPublicId(($this->parsedRemoteFileUrl["type"] === "video") ? $this->parsedRemoteFileUrl["thumbnail_url"] : $this->parsedRemoteFileUrl["publicId"] . '.' . $this->parsedRemoteFileUrl["extension"])
312312
->setFreeTransformation($this->parsedRemoteFileUrl["transformations_string"])
313313
->save();
314314
}

Core/CloudinaryImageProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@ public function retrieveTransformed(Image $image, Transformation $transformation
110110

111111
if ($this->configuration->isEnabledLocalMapping()) {
112112
//Look for a match on the mapping table:
113-
preg_match('/(CLD_[A-Za-z0-9]{13}_).+$/', $imageId, $cldUniqid);
113+
preg_match('/(cld_[A-Za-z0-9]{13}_).+$/i', $imageId, $cldUniqid);
114114
if ($cldUniqid && isset($cldUniqid[1])) {
115115
$mapped = $this->mediaLibraryMapFactory->create()->getCollection()->addFieldToFilter("cld_uniqid", $cldUniqid[1])->setPageSize(1)->getFirstItem();
116116
if ($mapped && ($origPublicId = $mapped->getCldPublicId())) {
117+
if (preg_match('/http(s?)\:\/\//i', $origPublicId)) { // If the image is a thumbnail the publicId woud be the full URL
118+
return Image::fromPath($origPublicId);
119+
}
117120
if (($freeTransformation = $mapped->getFreeTransformation()) && \strpos($imageId, $this->productMediaConfig->getBaseMediaUrl()) === 0) {
118121
$transformation->withFreeform($freeTransformation, false);
119122
}

Cron/VideoDataImport.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Cloudinary\Cloudinary\Cron;
4+
5+
use Cloudinary\Cloudinary\Core\ConfigurationInterface;
6+
use Cloudinary\Cloudinary\Model\Api\ResourcesManagement;
7+
use Cloudinary\Cloudinary\Model\ProductVideoFactory;
8+
use Magento\Framework\DataObject;
9+
use Magento\Framework\Json\Helper\Data as JsonHelper;
10+
11+
class VideoDataImport
12+
{
13+
/**
14+
* @var ConfigurationInterface
15+
*/
16+
private $configuration;
17+
18+
/**
19+
* @var ResourcesManagement
20+
*/
21+
private $cloudinaryResourcesManagement;
22+
23+
/**
24+
* @var JsonHelper
25+
*/
26+
private $jsonHelper;
27+
28+
/**
29+
* @param ProductVideoFactory
30+
*/
31+
private $productVideoFactory;
32+
33+
/**
34+
* @method __construct
35+
* @param ConfigurationInterface $configuration
36+
* @param ResourcesManagement $cloudinaryResourcesManagement
37+
* @param JsonHelper $jsonHelper
38+
* @param ProductVideoFactory $productVideoFactory
39+
*/
40+
public function __construct(
41+
ConfigurationInterface $configuration,
42+
ResourcesManagement $cloudinaryResourcesManagement,
43+
JsonHelper $jsonHelper,
44+
ProductVideoFactory $productVideoFactory
45+
) {
46+
$this->configuration = $configuration;
47+
$this->cloudinaryResourcesManagement = $cloudinaryResourcesManagement;
48+
$this->jsonHelper = $jsonHelper;
49+
$this->productVideoFactory = $productVideoFactory;
50+
}
51+
52+
public function execute()
53+
{
54+
if ($this->configuration->isEnabled()) {
55+
$productVideosCollection = $this->productVideoFactory->create()->getCollection()
56+
->addFieldToSelect('*')
57+
->addFieldToFilter('provider', 'cloudinary')
58+
->addFieldToFilter('title', ['null' => true])
59+
->setOrder('value_id', 'asc')
60+
->setPageSize($this->configuration->getScheduledVideoDataImportLimit());
61+
foreach ($productVideosCollection as $video) {
62+
$parsedRemoteFileUrl = $this->configuration->parseCloudinaryUrl($video->getUrl());
63+
$title = $description = $parsedRemoteFileUrl["publicId"];
64+
65+
$videoData = (array) $this->jsonHelper->jsonDecode($this->cloudinaryResourcesManagement->setId($parsedRemoteFileUrl["publicId"])->getVideo());
66+
if (!$videoData["error"]) {
67+
$videoData["context"] = new DataObject((isset($videoData["data"]["context"])) ? (array)$videoData["data"]["context"] : []);
68+
$title = $videoData["context"]->getData('caption') ?: $videoData["context"]->getData('alt');
69+
$description = $videoData["context"]->getData('description') ?: $videoData["context"]->getData('alt');
70+
}
71+
$title = $title ?: $parsedRemoteFileUrl["publicId"];
72+
$description = preg_replace('/(&nbsp;|<([^>]+)>)/i', '', $description ?: $title);
73+
74+
$video->setTitle((string) $title)
75+
->setDescription((string) $description)
76+
->save();
77+
}
78+
}
79+
}
80+
}

Model/Api/ProductGalleryManagement.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private function addGalleryItem($url, $sku, $publicId = null, $roles = null)
330330
);
331331
}
332332

333-
$roles = ($roles) ? array_map('trim', explode(',', $roles)) : null;
333+
$roles = ($roles) ? array_map('trim', (is_string($roles) ? explode(',', $roles) : (array) $roles)) : null;
334334
$product = $this->productRepository->get($sku);
335335

336336
$result = $this->retrieveImage($this->parsedRemoteFileUrl['thumbnail_url'] ?: $this->parsedRemoteFileUrl['transformationless_url']);
@@ -372,9 +372,9 @@ private function addGalleryItem($url, $sku, $publicId = null, $roles = null)
372372
$mediaGalleryData = $product->getMediaGallery();
373373
$galItem = array_pop($mediaGalleryData["images"]);
374374

375-
foreach ($this->productImageFinder->findNewImages($product) as $image) {
375+
/*foreach ($this->productImageFinder->findNewImages($product) as $image) {
376376
$this->cloudinaryImageManager->uploadAndSynchronise($image);
377-
}
377+
}*/
378378

379379
if ($this->parsedRemoteFileUrl["type"] === "image" && $this->parsedRemoteFileUrl['transformations_string']) {
380380
$this->transformationFactory->create()
@@ -527,7 +527,7 @@ private function saveCloudinaryMapping()
527527
{
528528
return $this->mapped = $this->mediaLibraryMapFactory->create()
529529
->setCldUniqid($this->cldUniqid)
530-
->setCldPublicId($this->parsedRemoteFileUrl["publicId"] . '.' . $this->parsedRemoteFileUrl["extension"])
530+
->setCldPublicId(($this->parsedRemoteFileUrl["type"] === "video") ? $this->parsedRemoteFileUrl["thumbnail_url"] : $this->parsedRemoteFileUrl["publicId"] . '.' . $this->parsedRemoteFileUrl["extension"])
531531
->setFreeTransformation($this->parsedRemoteFileUrl["transformations_string"])
532532
->save();
533533
}

Model/Configuration.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Configuration implements ConfigurationInterface
4747
const CONFIG_PATH_USE_ROOT_PATH = 'cloudinary/advanced/use_root_path';
4848
const CONFIG_PATH_USE_SIGNED_URLS = 'cloudinary/advanced/use_signed_urls';
4949
const CONFIG_PATH_ENABLE_LOCAL_MAPPING = 'cloudinary/advanced/enable_local_mapping';
50+
const CONFIG_PATH_SCHEDULED_VIDEO_DATA_IMPORT_LIMIT = 'cloudinary/advanced/cloudinary_scheduled_video_data_import_limit';
5051

5152
//= Product Gallery
5253
const CONFIG_PATH_PG_ALL = 'cloudinary/product_gallery';
@@ -86,7 +87,7 @@ class Configuration implements ConfigurationInterface
8687
const OVERWRITE = false;
8788
const SCOPE_ID_ONE = 1;
8889
const SCOPE_ID_ZERO = 0;
89-
const CLD_UNIQID_PREFIX = 'CLD_';
90+
const CLD_UNIQID_PREFIX = 'cld_';
9091

9192
/**
9293
* @var ScopeConfigInterface
@@ -384,6 +385,14 @@ public function isEnabledLocalMapping()
384385
return (bool) $this->configReader->getValue(self::CONFIG_PATH_ENABLE_LOCAL_MAPPING);
385386
}
386387

388+
/**
389+
* @return bool
390+
*/
391+
public function getScheduledVideoDataImportLimit()
392+
{
393+
return (int) $this->configReader->getValue(self::CONFIG_PATH_SCHEDULED_VIDEO_DATA_IMPORT_LIMIT);
394+
}
395+
387396
/**
388397
* @method getMediaBaseUrl
389398
* @return string
@@ -519,7 +528,7 @@ function ($matches) {
519528

520529
public function generateCLDuniqid()
521530
{
522-
return uniqid(self::CLD_UNIQID_PREFIX) . '_';
531+
return strtolower(uniqid(self::CLD_UNIQID_PREFIX)) . '_';
523532
}
524533

525534
public function addUniquePrefixToBasename($filename, $uniqid = null)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Cloudinary\Cloudinary\Model\Observer;
4+
5+
use Cloudinary\Cloudinary\Core\AutoUploadMapping\RequestProcessor;
6+
use Magento\Framework\App\Cache\TypeListInterface;
7+
use Magento\Framework\App\Config\ReinitableConfigInterface;
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\Event\Observer;
11+
use Magento\Framework\Event\ObserverInterface;
12+
use Magento\Framework\Message\ManagerInterface;
13+
14+
class CatalogProductImportBunchSaveAfter implements ObserverInterface
15+
{
16+
17+
/**
18+
* @var RequestProcessor
19+
*/
20+
protected $requestProcessor;
21+
22+
/**
23+
* @var ManagerInterface
24+
*/
25+
protected $messageManager;
26+
27+
/**
28+
* @var \Cloudinary\Cloudinary\Model\Configuration
29+
*/
30+
protected $configuration;
31+
32+
/**
33+
* Application config
34+
*
35+
* @var ScopeConfigInterface
36+
*/
37+
protected $appConfig;
38+
39+
/**
40+
* @var TypeListInterface
41+
*/
42+
protected $cacheTypeList;
43+
44+
protected $changedPaths = [];
45+
46+
/**
47+
* @param RequestProcessor $requestProcessor
48+
* @param ManagerInterface $messageManager
49+
* @param \Cloudinary\Cloudinary\Model\Configuration $configuration
50+
* @param TypeListInterface $cacheTypeList
51+
* @param ReinitableConfigInterface $config
52+
*/
53+
public function __construct(
54+
RequestProcessor $requestProcessor,
55+
ManagerInterface $messageManager,
56+
\Cloudinary\Cloudinary\Model\Configuration $configuration,
57+
TypeListInterface $cacheTypeList,
58+
ReinitableConfigInterface $config
59+
) {
60+
$this->requestProcessor = $requestProcessor;
61+
$this->messageManager = $messageManager;
62+
$this->configuration = $configuration;
63+
$this->cacheTypeList = $cacheTypeList;
64+
$this->appConfig = $config;
65+
}
66+
67+
/**
68+
* @param Observer $observer
69+
*/
70+
public function execute(Observer $observer)
71+
{
72+
//Clear config cache if needed
73+
$this->changedPaths = (array) $observer->getEvent()->getChangedPaths();
74+
if (count(
75+
array_intersect(
76+
$this->changedPaths,
77+
[
78+
\Cloudinary\Cloudinary\Model\Configuration::CONFIG_PATH_ENABLED,
79+
\Cloudinary\Cloudinary\Model\Configuration::CONFIG_PATH_ENVIRONMENT_VARIABLE,
80+
\Cloudinary\Cloudinary\Model\AutoUploadMapping\AutoUploadConfiguration::REQUEST_PATH
81+
]
82+
)
83+
) > 0
84+
) {
85+
$this->cleanConfigCache();
86+
$this->appConfig->reinit();
87+
}
88+
89+
if (!$this->configuration->isEnabled()) {
90+
return $this;
91+
}
92+
93+
if (!$this->requestProcessor->handle(DirectoryList::MEDIA, $this->configuration->getMediaBaseUrl(), true)) {
94+
$this->messageManager->addErrorMessage(self::AUTO_UPLOAD_SETUP_FAIL_MESSAGE);
95+
}
96+
}
97+
98+
protected function cleanConfigCache()
99+
{
100+
$this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
101+
return $this;
102+
}
103+
}

Model/ProductVideo.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Cloudinary\Cloudinary\Model;
4+
5+
class ProductVideo extends \Magento\Framework\Model\AbstractModel
6+
{
7+
protected function _construct()
8+
{
9+
$this->_init(\Cloudinary\Cloudinary\Model\ResourceModel\ProductVideo::class);
10+
}
11+
}

Model/ResourceModel/ProductVideo.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Cloudinary\Cloudinary\Model\ResourceModel;
3+
4+
class ProductVideo extends \Magento\ProductVideo\Model\ResourceModel\Video
5+
{
6+
/**
7+
* Resource initialization
8+
*
9+
* @return void
10+
*/
11+
public function _construct()
12+
{
13+
parent::_construct();
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Cloudinary\Cloudinary\Model\ResourceModel\ProductVideo;
4+
5+
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
6+
{
7+
/**
8+
* Define model & resource model
9+
*/
10+
protected function _construct()
11+
{
12+
$this->_init(
13+
\Cloudinary\Cloudinary\Model\ProductVideo::class,
14+
\Cloudinary\Cloudinary\Model\ResourceModel\ProductVideo::class
15+
);
16+
}
17+
}

Model/SynchronisationChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function isSynchronized($imageName)
6464

6565
if ($this->configuration->isEnabledLocalMapping()) {
6666
//Look for a match on the mapping table:
67-
preg_match('/(CLD_[A-Za-z0-9]{13}_).+$/', $imageName, $cldUniqid);
67+
preg_match('/(cld_[A-Za-z0-9]{13}_).+$/i', $imageName, $cldUniqid);
6868
if ($cldUniqid && isset($cldUniqid[1])) {
6969
$mapped = $this->mediaLibraryMapFactory->create()->getCollection()->addFieldToFilter("cld_uniqid", $cldUniqid[1])->setPageSize(1)->getFirstItem();
7070
if ($mapped && ($origPublicId = $mapped->getCldPublicId())) {

0 commit comments

Comments
 (0)