1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2019 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
7
7
declare (strict_types=1 );
8
8
9
9
namespace Magento \PageBuilder \Controller \Adminhtml \Template ;
10
10
11
+ use function preg_replace ;
12
+ use function str_replace ;
13
+ use function strpos ;
14
+ use function strtolower ;
15
+ use function substr ;
16
+ use function uniqid ;
17
+ use Exception ;
11
18
use Magento \Backend \App \Action ;
12
19
use Magento \Backend \App \Action \Context ;
13
20
use Magento \Framework \Api \ImageContent ;
14
21
use Magento \Framework \Api \ImageContentFactory ;
15
22
use Magento \Framework \Api \ImageContentValidator ;
16
23
use Magento \Framework \Api \SearchCriteriaBuilder ;
17
24
use Magento \Framework \App \Action \HttpPostActionInterface ;
25
+ use Magento \Framework \App \Filesystem \DirectoryList ;
18
26
use Magento \Framework \App \RequestInterface ;
19
27
use Magento \Framework \Controller \ResultFactory ;
28
+ use Magento \Framework \Exception \FileSystemException ;
29
+ use Magento \Framework \Exception \InputException ;
20
30
use Magento \Framework \Exception \LocalizedException ;
21
31
use Magento \Framework \Filesystem ;
32
+ use Magento \Framework \Image \AdapterFactory ;
22
33
use Magento \MediaStorage \Helper \File \Storage \Database ;
23
34
use Magento \PageBuilder \Api \Data \TemplateInterface ;
24
35
use Magento \PageBuilder \Api \TemplateRepositoryInterface ;
25
36
use Magento \PageBuilder \Model \TemplateFactory ;
26
37
use Psr \Log \LoggerInterface ;
27
- use Magento \Framework \Image \AdapterFactory ;
28
38
29
39
/**
30
40
* Save a template within template manager
33
43
*/
34
44
class Save extends Action implements HttpPostActionInterface
35
45
{
36
- const ADMIN_RESOURCE = 'Magento_PageBuilder::template_save ' ;
46
+ public const ADMIN_RESOURCE = 'Magento_PageBuilder::template_save ' ;
37
47
38
48
/**
39
49
* @var LoggerInterface
@@ -151,7 +161,7 @@ public function execute()
151
161
$ filePath = $ this ->storePreviewImage ($ request );
152
162
// Store the preview image within the new entity
153
163
$ template ->setPreviewImage ($ filePath );
154
- } catch (\ Exception $ e ) {
164
+ } catch (Exception $ e ) {
155
165
$ this ->logger ->critical ($ e );
156
166
157
167
return $ this ->resultFactory ->create (ResultFactory::TYPE_JSON )->setData (
@@ -176,7 +186,7 @@ public function execute()
176
186
'status ' => 'error ' ,
177
187
'message ' => $ e ->getMessage ()
178
188
];
179
- } catch (\ Exception $ e ) {
189
+ } catch (Exception $ e ) {
180
190
$ this ->logger ->critical ($ e );
181
191
182
192
$ result = [
@@ -215,26 +225,28 @@ private function validate(RequestInterface $request)
215
225
* Handle storing the preview image
216
226
*
217
227
* @param RequestInterface $request
218
- * @return string
228
+ * @return null|string
229
+ * @throws Exception
230
+ * @throws FileSystemException
231
+ * @throws InputException
219
232
* @throws LocalizedException
220
- * @throws \Magento\Framework\Exception\FileSystemException
221
- * @throws \Magento\Framework\Exception\InputException
222
233
*/
223
- private function storePreviewImage (RequestInterface $ request ) : ?string
234
+ private function storePreviewImage (RequestInterface $ request ): ?string
224
235
{
225
236
$ fileName = preg_replace ("/[^A-Za-z0-9]/ " , '' , str_replace (
226
- ' ' ,
227
- '- ' ,
228
- strtolower ($ request ->getParam (TemplateInterface::KEY_NAME ))
229
- )) . uniqid () . '.jpg ' ;
237
+ ' ' ,
238
+ '- ' ,
239
+ strtolower ($ request ->getParam (TemplateInterface::KEY_NAME ))
240
+ )) . uniqid () . '.jpg ' ;
230
241
231
242
// Prepare the image data
232
243
$ imgData = str_replace (' ' , '+ ' , $ request ->getParam ('previewImage ' ));
233
- $ imgData = substr ($ imgData , strpos ($ imgData , " , " ) + 1 );
234
- // phpcs:ignore
244
+ $ imgData = substr ($ imgData , strpos ($ imgData , ' , ' ) + 1 );
245
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
235
246
$ decodedImage = base64_decode ($ imgData );
236
247
237
248
$ imageProperties = getimagesizefromstring ($ decodedImage );
249
+
238
250
if (!$ imageProperties ) {
239
251
throw new LocalizedException (__ ('Unable to get properties from image. ' ));
240
252
}
@@ -246,16 +258,16 @@ private function storePreviewImage(RequestInterface $request) : ?string
246
258
$ imageContent ->setType ($ imageProperties ['mime ' ]);
247
259
248
260
if ($ this ->imageContentValidator ->isValid ($ imageContent )) {
249
- $ mediaDirWrite = $ this ->filesystem
250
- ->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::MEDIA );
261
+ $ mediaDirWrite = $ this ->filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
251
262
$ directory = $ mediaDirWrite ->getAbsolutePath ('.template-manager ' );
252
263
$ mediaDirWrite ->create ($ directory );
253
- $ fileAbsolutePath = $ directory . $ fileName ;
264
+
265
+ $ fileAbsolutePath = $ directory . DIRECTORY_SEPARATOR . $ fileName ;
254
266
// Write the file to the directory
255
267
$ mediaDirWrite ->getDriver ()->filePutContents ($ fileAbsolutePath , $ decodedImage );
256
268
// Generate a thumbnail, called -thumb next to the image for usage in the grid
257
269
$ thumbPath = str_replace ('.jpg ' , '-thumb.jpg ' , $ fileName );
258
- $ thumbAbsolutePath = $ directory . $ thumbPath ;
270
+ $ thumbAbsolutePath = $ directory . DIRECTORY_SEPARATOR . $ thumbPath ;
259
271
$ imageFactory = $ this ->imageAdapterFactory ->create ();
260
272
$ imageFactory ->open ($ fileAbsolutePath );
261
273
$ imageFactory ->resize (350 );
0 commit comments