@@ -222,14 +222,11 @@ private function validate(RequestInterface $request)
222
222
*/
223
223
private function storePreviewImage (RequestInterface $ request ) : ?string
224
224
{
225
- $ mediaDir = $ this ->filesystem
226
- ->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::MEDIA );
227
225
$ fileName = preg_replace ("/[^A-Za-z0-9]/ " , '' , str_replace (
228
226
' ' ,
229
227
'- ' ,
230
228
strtolower ($ request ->getParam (TemplateInterface::KEY_NAME ))
231
229
)) . uniqid () . '.jpg ' ;
232
- $ filePath = '.template-manager ' . DIRECTORY_SEPARATOR . $ fileName ;
233
230
234
231
// Prepare the image data
235
232
$ imgData = str_replace (' ' , '+ ' , $ request ->getParam ('previewImage ' ));
@@ -249,21 +246,25 @@ private function storePreviewImage(RequestInterface $request) : ?string
249
246
$ imageContent ->setType ($ imageProperties ['mime ' ]);
250
247
251
248
if ($ this ->imageContentValidator ->isValid ($ imageContent )) {
252
- $ absolutePath = $ mediaDir ->getAbsolutePath () . $ filePath ;
249
+ $ mediaDirWrite = $ this ->filesystem
250
+ ->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::MEDIA );
251
+ $ directory = $ mediaDirWrite ->getAbsolutePath ('.template-manager ' );
252
+ $ mediaDirWrite ->create ($ directory );
253
+ $ fileAbsolutePath = $ directory . $ fileName ;
253
254
// Write the file to the directory
254
- $ mediaDir ->getDriver ()->filePutContents ($ absolutePath , $ decodedImage );
255
+ $ mediaDirWrite ->getDriver ()->filePutContents ($ fileAbsolutePath , $ decodedImage );
255
256
// 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 ;
257
+ $ thumbPath = str_replace ('.jpg ' , '-thumb.jpg ' , $ fileName );
258
+ $ thumbAbsolutePath = $ directory . $ thumbPath ;
258
259
$ imageFactory = $ this ->imageAdapterFactory ->create ();
259
- $ imageFactory ->open ($ absolutePath );
260
+ $ imageFactory ->open ($ fileAbsolutePath );
260
261
$ imageFactory ->resize (350 );
261
262
$ imageFactory ->save ($ thumbAbsolutePath );
262
- $ this ->mediaStorage ->saveFile ($ filePath );
263
- $ this ->mediaStorage ->saveFile ($ thumbPath );
263
+ $ this ->mediaStorage ->saveFile ($ fileAbsolutePath );
264
+ $ this ->mediaStorage ->saveFile ($ thumbAbsolutePath );
264
265
265
266
// Store the preview image within the new entity
266
- return $ filePath ;
267
+ return $ mediaDirWrite -> getRelativePath ( $ fileAbsolutePath ) ;
267
268
}
268
269
269
270
return null ;
0 commit comments