Skip to content

Commit 6ab597e

Browse files
committed
CLOUDINARY-413 [Feature] Add delete method for product catalog API
1 parent 9aa64ef commit 6ab597e

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

Api/ProductGalleryManagementInterface.php

+9
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,13 @@ public function getProductMedia($sku);
5252
* @return string
5353
*/
5454
public function getProductsMedia($skus);
55+
56+
57+
/** Remove Product images by publicIds
58+
* @param string $sku
59+
* @param mixed $urls
60+
* @param bool | int | null $delete_all_gallery
61+
* @return string
62+
*/
63+
public function removeProductMedia($sku, $urls, $delete_all_gallery = 0);
5564
}

Model/Api/ProductGalleryManagement.php

+86
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\Framework\App\ResourceConnection;
2020
use Magento\Framework\DataObject;
2121
use Magento\Framework\Exception\LocalizedException;
22+
use Magento\Framework\Exception\NoSuchEntityException;
2223
use Magento\Framework\Filesystem;
2324
use Magento\Framework\HTTP\Adapter\Curl;
2425
use Magento\Framework\Image\AdapterFactory as ImageAdapterFactory;
@@ -233,6 +234,88 @@ public function __construct(
233234
$this->resourceConnection = $resourceConnection;
234235
}
235236

237+
238+
/**
239+
* Remove Gallery Item
240+
* @param $product
241+
* @param $url
242+
* @param $removeAllGallery
243+
* @return int|void
244+
* @throws NoSuchEntityException .
245+
*/
246+
private function removeGalleryItem($product, $url, $removeAllGallery)
247+
{
248+
$unlinked = 0;
249+
if ($product) {
250+
$mediaGalleryEntries = $product->getMediaGalleryEntries();
251+
252+
if (is_array($mediaGalleryEntries)) {
253+
foreach ($mediaGalleryEntries as $key => $entry) {
254+
$image = $entry->getFile();
255+
$url = preg_replace('/\?.*/', '', $url);
256+
$image = $this->storeManager->getStore()->getBaseUrl() . 'pub/media/catalog/product' . $image;
257+
$basename = basename($image);
258+
$ext = pathinfo($image, PATHINFO_EXTENSION);
259+
// removing unique id from original filename
260+
$pattern = '/^' . $this->configuration::CLD_UNIQID_PREFIX . '.*?_/';
261+
$basename = preg_replace($pattern, '', $basename);
262+
$filename = basename($url);
263+
$filename = preg_replace($pattern, '', $filename);
264+
// $filename = $url . '.' . $ext;
265+
266+
if ($basename == $filename || $removeAllGallery) {
267+
unset($mediaGalleryEntries[$key]);
268+
$this->mediaGalleryProcessor->removeImage($product, $image);
269+
$unlinked++;
270+
}
271+
}
272+
}
273+
if ($unlinked) {
274+
$product->setMediaGalleryEntries($mediaGalleryEntries);
275+
try {
276+
$product = $this->productRepository->save($product);
277+
} catch (\Exception $e) {
278+
$message = ['type' => 'error', 'message' => 'Falied Delete Image Error: ' . $e->getMessage() . ' line ' . $e->getLine()];
279+
}
280+
}
281+
return $unlinked;
282+
}
283+
}
284+
285+
/**
286+
* {@inheritdoc}
287+
*/
288+
public function removeProductMedia($sku, $urls, $delete_all_gallery = 0)
289+
{
290+
$result = [
291+
"passed" => 0,
292+
"failed" => [
293+
"count" => 0,
294+
"urls" => []
295+
]
296+
];
297+
298+
$urls = (array) $urls;
299+
300+
try {
301+
$product = $this->productRepository->get($sku);
302+
$this->checkEnvHeader();
303+
$this->checkEnabled();
304+
305+
foreach ($urls as $i => $url) {
306+
$unlinked = $this->removeGalleryItem($product, $url, $delete_all_gallery);
307+
}
308+
309+
$result["passed"] = $unlinked;
310+
} catch (\Exception $e) {
311+
$result["failed"]["count"]++;
312+
$result["error"] = $e->getMessage();
313+
$result["failed"]["public_id"][] = $url;
314+
}
315+
316+
return $this->jsonHelper->jsonEncode($result);
317+
}
318+
236319
/**
237320
* {@inheritdoc}
238321
*/
@@ -487,6 +570,7 @@ public function addGalleryItem($url, $sku, $publicId = null, $roles = null, $lab
487570
);
488571

489572
$mediaGalleryData = $product->getMediaGallery();
573+
// last gallery value from array
490574
$galItem = array_pop($mediaGalleryData["images"]);
491575

492576
if ($this->parsedRemoteFileUrl["type"] === "video") {
@@ -812,4 +896,6 @@ private function emulateAdminhtmlArea($force = true)
812896
$this->startEnvironmentEmulation(0, Area::AREA_ADMINHTML, $force);
813897
return $this;
814898
}
899+
900+
815901
}

etc/webapi.xml

+9
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,13 @@
7474
</route>
7575
<!--/ cloudinary/products/:sku/media -->
7676

77+
<!-- cloudinary/products/:sku/mediaremove -->
78+
<route method="POST" url="/V1/cloudinary/products/:sku/mediaremove">
79+
<service class="Cloudinary\Cloudinary\Api\ProductGalleryManagementInterface" method="removeProductMedia"/>
80+
<resources>
81+
<resource ref="Magento_Catalog::products" />
82+
</resources>
83+
</route>
84+
<!--/ cloudinary/products/:sku/mediaremove -->
85+
7786
</routes>

0 commit comments

Comments
 (0)