Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 08f818b

Browse files
2 parents 431134e + 15060f4 commit 08f818b

File tree

8 files changed

+211
-83
lines changed

8 files changed

+211
-83
lines changed

‎Controller/Adminhtml/Ajax/System/Config/AutoUploadMapping.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
class AutoUploadMapping extends Action
2121
{
2222
const AUTO_UPLOAD_SETUP_FAIL_MESSAGE = 'Error. Unable to setup auto upload mapping.';
23+
const NON_AJAX_REQUEST = 'Rejected: Non-ajax request';
2324

2425
/**
2526
* @var JsonFactory

‎Controller/Adminhtml/Ajax/UpdateAdminImage.php‎

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,39 @@
77
use Cloudinary\Cloudinary\Core\Image\ImageFactory;
88
use Cloudinary\Cloudinary\Core\UrlGenerator;
99
use Cloudinary\Configuration\Configuration;
10-
use Magento\Framework\App\Filesystem\DirectoryList;
1110
use Magento\Store\Model\StoreManagerInterface;
1211
use Magento\Framework\UrlInterface;
1312
use Magento\Backend\App\Action;
1413
use Magento\Framework\Controller\Result\RawFactory as ResultRawFactory;
1514
use Magento\Backend\App\Action\Context;
16-
use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent;
17-
use Magento\Framework\Filesystem as FileSysten;
18-
use Magento\Catalog\Helper\Image as CatalogImageHelper;
19-
use Cloudinary\Cloudinary\Core\Image;
2015
use Cloudinary\Asset\Media;
2116
use Cloudinary\Cloudinary\Core\Image\Transformation;
2217

2318
class UpdateAdminImage extends Action
2419
{
25-
/**
26-
* @var ConfigurationInterface
27-
*/
20+
const ADMIN_RESOURCE = 'Cloudinary_Cloudinary::config';
21+
2822
protected $configuration;
29-
/**
30-
* @var UrlGenerator
31-
*/
3223
protected $urlGenerator;
33-
/**
34-
* @var ImageFactory
35-
*/
3624
protected $imageFactory;
37-
/**
38-
* @var StoreManagerInterface
39-
*/
4025
protected $storeManager;
41-
42-
/**
43-
* @var UrlInterface
44-
*/
4526
protected $urlInterface;
46-
4727
protected $resultFactory;
48-
49-
protected $imageContent;
50-
51-
protected $filesystem;
52-
53-
private $_authorised;
54-
5528
protected $configurationBuilder;
56-
5729
protected $transformation;
30+
private $_authorised;
31+
5832

5933
/**
34+
* @param Context $context
6035
* @param ImageFactory $imageFactory
6136
* @param UrlGenerator $urlGenerator
6237
* @param ConfigurationInterface $configuration
6338
* @param StoreManagerInterface $storeManager
6439
* @param UrlInterface $urlInterface
40+
* @param ResultRawFactory $resultFactory
41+
* @param ConfigurationBuilder $configurationBuilder
42+
* @param Transformation $transformation
6543
*/
6644
public function __construct(
6745
Context $context,
@@ -71,7 +49,6 @@ public function __construct(
7149
StoreManagerInterface $storeManager,
7250
UrlInterface $urlInterface,
7351
ResultRawFactory $resultFactory,
74-
FileSysten $filesystem,
7552
ConfigurationBuilder $configurationBuilder,
7653
Transformation $transformation
7754
) {
@@ -82,75 +59,83 @@ public function __construct(
8259
$this->storeManager = $storeManager;
8360
$this->urlInterface = $urlInterface;
8461
$this->resultFactory = $resultFactory;
85-
$this->filesystem = $filesystem;
8662
$this->configurationBuilder = $configurationBuilder;
8763
$this->transformation = $transformation;
8864
}
8965

66+
protected function _isAllowed()
67+
{
68+
return $this->_authorization->isAllowed(self::ADMIN_RESOURCE);
69+
}
70+
9071
private function authorise()
9172
{
9273
if (!$this->_authorised && $this->configuration->isEnabled()) {
9374
Configuration::instance($this->configurationBuilder->build());
94-
BaseApiClient::$userPlatform = $this->configuration->getUserPlatform();
75+
BaseApiClient::$userPlatform = $this->configuration->getUserPlatform();
9576
$this->_authorised = true;
9677
}
9778
}
9879

80+
/**
81+
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Raw|\Magento\Framework\Controller\ResultInterface
82+
*/
9983
public function execute()
10084
{
10185
$this->authorise();
102-
$result = [];
86+
$result = ['error' => 'Invalid configuration'];
87+
10388
if ($this->configuration->isEnabled()) {
104-
try{
89+
try{
10590
$remoteImageUrl = $this->getRequest()->getParam('remote_image');
91+
92+
// Validate URL
93+
if (!$remoteImageUrl) {
94+
throw new \InvalidArgumentException('Missing remote_image parameter');
95+
}
96+
10697
$parsedUrl = parse_url($remoteImageUrl);
98+
99+
if (!$parsedUrl || !isset($parsedUrl['scheme'], $parsedUrl['host'], $parsedUrl['path'])) {
100+
throw new \InvalidArgumentException('Invalid image URL');
101+
}
102+
107103
$cleanUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'];
108104
$baseUrl = $this->storeManager->getStore()->getBaseUrl();
109105
$relativePath = str_replace($baseUrl, '', $cleanUrl);
110106

111-
// file id can be media/wmpvownqus8xwvylswsr_1
112-
113107
// Check if this is a Cloudinary rendition path
114108
if (strpos($relativePath, '.renditions/cloudinary/') !== false) {
115-
// Extract the filename from the renditions path
116109
$parts = explode('.renditions/cloudinary/', $relativePath);
117110
$filename = end($parts);
118111

119112
// Remove the first cld_ prefix if there are multiple
120-
// e.g., cld_68b6aa57b4d59_cld_6458c4355ee79_cld-sample-2.jpg -> cld_6458c4355ee79_cld-sample-2.jpg
121-
if (preg_match('/^cld_[a-z0-9]+_/', $filename)) {
122-
$filename = preg_replace('/^cld_[a-z0-9]+_/', '', $filename);
113+
if (preg_match('/^cld_[a-zA-Z0-9]+_/', $filename)) {
114+
$filename = preg_replace('/^cld_[a-zA-Z0-9]+_/', '', $filename);
123115
}
124116

125-
// The public ID should be media/filename for Cloudinary
126-
$filedId = 'media/' . $filename;
117+
$fileId = 'media/' . $filename;
127118
} else {
128-
// For regular media files, use the relative path as is
129-
$filedId = $relativePath;
130-
131-
// Remove media/ prefix if present (it will be in the public ID already)
132-
if (strpos($filedId, 'media/') === 0) {
133-
$filedId = substr($filedId, 6);
134-
}
119+
$fileId = $relativePath;
135120
}
136121

137-
$result = Media::fromParams(
138-
$filedId,
139-
[ 'transformation' => $this->transformation->build(),
140-
'secure' => true,
141-
'sign_url' => $this->configuration->getUseSignedUrls(),
142-
'version' => 1
143-
]
144-
) . '?_i=AB';
122+
$result = Media::fromParams(
123+
$fileId,
124+
[
125+
'transformation' => $this->transformation->build(),
126+
'secure' => true,
127+
'sign_url' => $this->configuration->getUseSignedUrls(),
128+
'version' => 1
129+
]
130+
) . '?_i=AB';
145131

146132
} catch (\Exception $e) {
147133
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
148134
}
149135
}
150136

151-
152137
$response = $this->resultFactory->create();
153-
$response->setHeader('Content-type', 'text/plain');
138+
$response->setHeader('Content-type', 'application/json');
154139
$response->setContents(json_encode($result));
155140
return $response;
156141
}

‎Core/CloudinaryImageManager.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,38 @@ public function __construct(
5656
*/
5757
public function uploadAndSynchronise(Image $image, ?OutputInterface $output = null, $retryAttempt = 0)
5858
{
59+
$uploadResult = null;
5960
if (!$this->configuration->isEnabled() || !$this->configuration->hasEnvironmentVariable()) {
6061
return;
6162
}
6263

6364
try {
65+
6466
$this->report($output, sprintf(self::MESSAGE_UPLOADING_IMAGE, $image));
65-
$this->cloudinaryImageProvider->upload($image);
67+
$uploadResult = $this->cloudinaryImageProvider->upload($image);
6668
} catch (FileExists $e) {
6769
$this->report($output, sprintf(self::MESSAGE_UPLOADED_EXISTS, $image));
70+
$uploadResult['file_exist'] = preg_replace('/\.[^.]+$/', '', $image->getRelativePath());
71+
6872
} catch (\Exception $e) {
6973
if ($e->getMessage() === FileExists::DEFAULT_MESSAGE) {
7074
$this->report($output, sprintf(self::MESSAGE_UPLOADED_EXISTS, $image));
75+
$uploadResult['file_exist'] = preg_replace('/\.[^.]+$/', '', $image->getRelativePath());
7176
} else {
7277
if ($retryAttempt < self::MAXIMUM_RETRY_ATTEMPTS) {
7378
$retryAttempt++;
7479
$this->report($output, sprintf(self::MESSAGE_RETRY, $e->getMessage(), $retryAttempt));
7580
usleep(rand(10, 1000) * 1000);
7681
$this->uploadAndSynchronise($image, $output, $retryAttempt);
77-
return;
82+
return$uploadResult;
7883
}
7984

8085
throw $e;
8186
}
8287
}
8388

8489
$this->synchronisationRepository->saveAsSynchronized($image->getRelativePath());
90+
return $uploadResult;
8591
}
8692

8793
/**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /