77use Cloudinary \Cloudinary \Core \Image \ImageFactory ;
88use Cloudinary \Cloudinary \Core \UrlGenerator ;
99use Cloudinary \Configuration \Configuration ;
10- use Magento \Framework \App \Filesystem \DirectoryList ;
1110use Magento \Store \Model \StoreManagerInterface ;
1211use Magento \Framework \UrlInterface ;
1312use Magento \Backend \App \Action ;
1413use Magento \Framework \Controller \Result \RawFactory as ResultRawFactory ;
1514use 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 ;
2015use Cloudinary \Asset \Media ;
2116use Cloudinary \Cloudinary \Core \Image \Transformation ;
2217
2318class 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 }
0 commit comments