Skip to content

Commit

Permalink
v1.14.13: Added API endpoints for getting product CLD media URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
pini-girit committed Jun 9, 2021
1 parent 5f5106d commit ea3c2cf
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 13 deletions.
16 changes: 16 additions & 0 deletions Api/ProductGalleryManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,20 @@ public function addItems($items);
* @return string
*/
public function addProductMedia($sku, $urls);

/**
* Get product gallery items as Cloudinary URLs.
* @method getProductMedia
* @param string $sku
* @return string
*/
public function getProductMedia($sku);

/**
* Get products gallery items as Cloudinary URLs.
* @method getProductsMedia
* @param mixed $skus
* @return string
*/
public function getProductsMedia($skus);
}
106 changes: 96 additions & 10 deletions Model/Api/ProductGalleryManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,7 @@ public function addProductMedia($sku, $urls)
];
try {
$this->checkEnvHeader();
if (!$this->configuration->isEnabled()) {
throw new LocalizedException(
__("Cloudinary module is disabled. Please enable it first in order to use this API.")
);
}
$this->checkEnabled();
$urls = (array)$urls;
foreach ($urls as $i => $url) {
try {
Expand Down Expand Up @@ -284,6 +280,50 @@ public function addProductMedia($sku, $urls)
return $this->jsonHelper->jsonEncode($result);
}

/**
* {@inheritdoc}
*/
public function getProductMedia($sku)
{
return $this->_getProductMedia($sku);
}

/**
* {@inheritdoc}
*/
public function getProductsMedia($skus)
{
return $this->_getProductMedia($skus);
}

/**
* [_getProductMedia description]
* @method _getProductMedia
* @param mixed $sku
* @return string (json result)
*/
private function _getProductMedia($sku)
{
$result = ["data" => []];

try {
$this->checkEnvHeader();
$this->checkEnabled();
if (is_array($sku) || is_object($sku)) {
foreach ($sku as $key => $_sku) {
$result['data'][$_sku] = $this->getProductCldUrlsBySku($_sku);
}
} else {
$result['data'] = $this->getProductCldUrlsBySku($sku);
}
} catch (\Exception $e) {
$result["error"] = 1;
$result["message"] = $e->getMessage();
}

return $this->jsonHelper->jsonEncode($result);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -312,11 +352,7 @@ public function addItems($items)
];
try {
$this->checkEnvHeader();
if (!$this->configuration->isEnabled()) {
throw new LocalizedException(
__("Cloudinary module is disabled. Please enable it first in order to use this API.")
);
}
$this->checkEnabled();
$items = (array)$items;
foreach ($items as $i => $item) {
try {
Expand Down Expand Up @@ -568,6 +604,20 @@ private function checkEnvHeader()
return $this;
}

/**
* @method checkEnabled
* @return $this
*/
private function checkEnabled()
{
if (!$this->configuration->isEnabled()) {
throw new LocalizedException(
__("Cloudinary module is disabled. Please enable it first in order to use this API.")
);
}
return $this;
}

private function getLocalTmpFileName($remoteFileUrl)
{
$localFileName = Uploader::getCorrectFileName(basename($remoteFileUrl));
Expand Down Expand Up @@ -682,6 +732,42 @@ private function saveCloudinaryMapping()
->save();
}

/**
* @method getProductMediaBySku
* @param string $sku
* @return array
*/
private function getProductCldUrlsBySku($sku)
{
$urls = [
'image' => null,
'small_image' => null,
'thumbnail' => null,
'media_gallery' => [],
];
try {
$product = $this->productRepository->get($sku);
foreach ($product->getMediaGalleryImages() as $gallItem) {
$urls['media_gallery'][] = $gallItem->getUrl();
if ($product->getData('image') === $gallItem->getFile()) {
$urls['image'] = $gallItem->getUrl();
}
if ($product->getData('small_image') === $gallItem->getFile()) {
$urls['small_image'] = $gallItem->getUrl();
}
if ($product->getData('thumbnail') === $gallItem->getFile()) {
$urls['thumbnail'] = $gallItem->getUrl();
}
}
} catch (\Exception $e) {
$urls = [
'error' => 1,
'message' => $e->getMessage(),
];
}
return $urls;
}

///////////////////////////////
// App Environment Emulation //
///////////////////////////////
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.14.12",
"version": "1.14.13",
"license": "MIT",
"require": {
"cloudinary/cloudinary_php": "^1.20.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Cloudinary_Cloudinary" setup_version="1.14.12">
<module name="Cloudinary_Cloudinary" setup_version="1.14.13">
<sequence>
<module name="Magento_ProductVideo"/>
<module name="Magento_PageBuilder"/>
Expand Down
18 changes: 18 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@
</route>
<!--/ cloudinary/productGallery/addItems -->

<!-- cloudinary/products/media -->
<route method="POST" url="/V1/cloudinary/products/media/get">
<service class="Cloudinary\Cloudinary\Api\ProductGalleryManagementInterface" method="getProductsMedia"/>
<resources>
<resource ref="Magento_Catalog::products" />
</resources>
</route>
<!--/ cloudinary/products/media -->

<!-- cloudinary/products/:sku/media -->
<route method="GET" url="/V1/cloudinary/products/:sku/media">
<service class="Cloudinary\Cloudinary\Api\ProductGalleryManagementInterface" method="getProductMedia"/>
<resources>
<resource ref="Magento_Catalog::products" />
</resources>
</route>
<!--/ cloudinary/products/:sku/media -->

<!-- cloudinary/products/:sku/media -->
<route method="POST" url="/V1/cloudinary/products/:sku/media">
<service class="Cloudinary\Cloudinary\Api\ProductGalleryManagementInterface" method="addProductMedia"/>
Expand Down
2 changes: 1 addition & 1 deletion marketplace.composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cloudinary/cloudinary",
"description": "Cloudinary Magento 2 Integration.",
"type": "magento2-module",
"version": "1.14.12",
"version": "1.14.13",
"license": "MIT",
"require": {
"cloudinary/cloudinary_php": "^1.20.0"
Expand Down

0 comments on commit ea3c2cf

Please sign in to comment.