10
10
11
11
use Magento \Backend \App \Action ;
12
12
use Magento \Backend \App \Action \Context ;
13
+ use Magento \Framework \Api \ImageContent ;
13
14
use Magento \Framework \Api \ImageContentFactory ;
14
15
use Magento \Framework \Api \ImageContentValidator ;
15
16
use Magento \Framework \Api \SearchCriteriaBuilder ;
16
17
use Magento \Framework \App \Action \HttpPostActionInterface ;
17
- use Magento \Framework \App \ObjectManager ;
18
18
use Magento \Framework \App \RequestInterface ;
19
19
use Magento \Framework \Controller \ResultFactory ;
20
20
use Magento \Framework \Exception \LocalizedException ;
25
25
use Magento \PageBuilder \Model \TemplateFactory ;
26
26
use Psr \Log \LoggerInterface ;
27
27
use Magento \Framework \Image \AdapterFactory ;
28
- use Magento \PageBuilder \Model \ImageContentUploader ;
29
28
30
29
/**
31
30
* Save a template within template manager
@@ -63,31 +62,23 @@ class Save extends Action implements HttpPostActionInterface
63
62
64
63
/**
65
64
* @var ImageContentValidator
66
- * @deprecated
67
65
*/
68
66
private $ imageContentValidator ;
69
67
70
68
/**
71
69
* @var ImageContentFactory
72
- * @deprecated
73
70
*/
74
71
private $ imageContentFactory ;
75
72
76
73
/**
77
74
* @var Database
78
- * @deprecated
79
75
*/
80
76
private $ mediaStorage ;
81
77
82
78
/**
83
79
* @var AdapterFactory
84
80
*/
85
81
private $ imageAdapterFactory ;
86
-
87
- /**
88
- * @var ImageContentUploader
89
- */
90
- private $ contentUploader ;
91
82
92
83
/**
93
84
* @param Context $context
@@ -100,7 +91,6 @@ class Save extends Action implements HttpPostActionInterface
100
91
* @param ImageContentFactory $imageContentFactory
101
92
* @param Database $mediaStorage
102
93
* @param AdapterFactory $imageAdapterFactory
103
- * @param ImageContentUploader|null $contentUploader
104
94
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
105
95
*/
106
96
public function __construct (
@@ -113,8 +103,7 @@ public function __construct(
113
103
ImageContentValidator $ imageContentValidator ,
114
104
ImageContentFactory $ imageContentFactory ,
115
105
Database $ mediaStorage ,
116
- AdapterFactory $ imageAdapterFactory ,
117
- ImageContentUploader $ contentUploader = null
106
+ AdapterFactory $ imageAdapterFactory
118
107
) {
119
108
parent ::__construct ($ context );
120
109
@@ -127,7 +116,6 @@ public function __construct(
127
116
$ this ->imageContentFactory = $ imageContentFactory ;
128
117
$ this ->mediaStorage = $ mediaStorage ;
129
118
$ this ->imageAdapterFactory = $ imageAdapterFactory ;
130
- $ this ->contentUploader = $ contentUploader ?? ObjectManager::getInstance ()->get (ImageContentUploader::class);
131
119
}
132
120
133
121
/**
@@ -230,36 +218,54 @@ private function validate(RequestInterface $request)
230
218
* @return string
231
219
* @throws LocalizedException
232
220
* @throws \Magento\Framework\Exception\FileSystemException
233
- * @throws \Exception
221
+ * @throws \Magento\Framework\ Exception\InputException
234
222
*/
235
223
private function storePreviewImage (RequestInterface $ request ) : ?string
236
224
{
237
225
$ mediaDir = $ this ->filesystem
238
226
->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::MEDIA );
239
227
$ fileName = preg_replace ("/[^A-Za-z0-9]/ " , '' , str_replace (
240
- ' ' ,
241
- '- ' ,
242
- strtolower ($ request ->getParam (TemplateInterface::KEY_NAME ))
243
- )) . uniqid () . '.jpg ' ;
244
- $ fileDirectoryPath = $ mediaDir -> getAbsolutePath ( '.template-manager ' ) ;
228
+ ' ' ,
229
+ '- ' ,
230
+ strtolower ($ request ->getParam (TemplateInterface::KEY_NAME ))
231
+ )) . uniqid () . '.jpg ' ;
232
+ $ filePath = '.template-manager ' . DIRECTORY_SEPARATOR . $ fileName ;
245
233
246
234
// Prepare the image data
247
235
$ imgData = str_replace (' ' , '+ ' , $ request ->getParam ('previewImage ' ));
248
236
$ imgData = substr ($ imgData , strpos ($ imgData , ", " ) + 1 );
249
-
250
- $ uploadedImage = $ this ->contentUploader ->upload ($ fileName , $ imgData , $ fileDirectoryPath );
251
- $ filePath = $ fileDirectoryPath . $ uploadedImage ;
252
- $ relativeFilePath = $ mediaDir ->getRelativePath ($ filePath );
253
- if ($ relativeFilePath === null ) {
254
- throw new LocalizedException (__ ('Unable to retrieve image. ' ));
237
+ // phpcs:ignore
238
+ $ decodedImage = base64_decode ($ imgData );
239
+
240
+ $ imageProperties = getimagesizefromstring ($ decodedImage );
241
+ if (!$ imageProperties ) {
242
+ throw new LocalizedException (__ ('Unable to get properties from image. ' ));
243
+ }
244
+
245
+ /* @var ImageContent $imageContent */
246
+ $ imageContent = $ this ->imageContentFactory ->create ();
247
+ $ imageContent ->setBase64EncodedData ($ imgData );
248
+ $ imageContent ->setName ($ fileName );
249
+ $ imageContent ->setType ($ imageProperties ['mime ' ]);
250
+
251
+ if ($ this ->imageContentValidator ->isValid ($ imageContent )) {
252
+ $ absolutePath = $ mediaDir ->getAbsolutePath () . $ filePath ;
253
+ // Write the file to the directory
254
+ $ mediaDir ->getDriver ()->filePutContents ($ absolutePath , $ decodedImage );
255
+ // Generate a thumbnail, called -thumb next to the image for usage in the grid
256
+ $ thumbPath = str_replace ('.jpg ' , '-thumb.jpg ' , $ filePath );
257
+ $ thumbAbsolutePath = $ mediaDir ->getAbsolutePath () . $ thumbPath ;
258
+ $ imageFactory = $ this ->imageAdapterFactory ->create ();
259
+ $ imageFactory ->open ($ absolutePath );
260
+ $ imageFactory ->resize (350 );
261
+ $ imageFactory ->save ($ thumbAbsolutePath );
262
+ $ this ->mediaStorage ->saveFile ($ filePath );
263
+ $ this ->mediaStorage ->saveFile ($ thumbPath );
264
+
265
+ // Store the preview image within the new entity
266
+ return $ filePath ;
255
267
}
256
- $ thumbPath = str_replace ('.jpg ' , '-thumb.jpg ' , $ relativeFilePath );
257
- $ thumbAbsolutePath = $ mediaDir ->getAbsolutePath () . $ thumbPath ;
258
- $ imageFactory = $ this ->imageAdapterFactory ->create ();
259
- $ imageFactory ->open ($ filePath );
260
- $ imageFactory ->resize (350 );
261
- $ imageFactory ->save ($ thumbAbsolutePath );
262
-
263
- return $ relativeFilePath ;
268
+
269
+ return null ;
264
270
}
265
271
}
0 commit comments