8
8
9
9
namespace Magento \MediaGalleryUi \Model \InsertImageData ;
10
10
11
+ use Magento \Cms \Helper \Wysiwyg \Images ;
11
12
use Magento \Cms \Model \Wysiwyg \Images \GetInsertImageContent ;
12
13
use Magento \Framework \App \Filesystem \DirectoryList ;
13
14
use Magento \Framework \File \Mime ;
@@ -21,7 +22,7 @@ class GetInsertImageData
21
22
/**
22
23
* @var ReadInterface
23
24
*/
24
- private $ pubDirectory ;
25
+ private $ mediaDirectory ;
25
26
26
27
/**
27
28
* @var Filesystem
@@ -43,24 +44,32 @@ class GetInsertImageData
43
44
*/
44
45
private $ mime ;
45
46
47
+ /**
48
+ * @var Images
49
+ */
50
+ private $ imagesHelper ;
51
+
46
52
/**
47
53
* GetInsertImageData constructor.
48
54
*
49
55
* @param GetInsertImageContent $getInsertImageContent
50
56
* @param Filesystem $fileSystem
51
57
* @param Mime $mime
52
58
* @param InsertImageDataFactory $insertImageDataFactory
59
+ * @param Images $imagesHelper
53
60
*/
54
61
public function __construct (
55
62
GetInsertImageContent $ getInsertImageContent ,
56
63
Filesystem $ fileSystem ,
57
64
Mime $ mime ,
58
- InsertImageDataFactory $ insertImageDataFactory
65
+ InsertImageDataFactory $ insertImageDataFactory ,
66
+ Images $ imagesHelper
59
67
) {
60
68
$ this ->getInsertImageContent = $ getInsertImageContent ;
61
69
$ this ->filesystem = $ fileSystem ;
62
70
$ this ->mime = $ mime ;
63
71
$ this ->insertImageDataFactory = $ insertImageDataFactory ;
72
+ $ this ->imagesHelper = $ imagesHelper ;
64
73
}
65
74
66
75
/**
@@ -84,8 +93,9 @@ public function execute(
84
93
$ renderAsTag ,
85
94
$ storeId
86
95
);
87
- $ size = $ forceStaticPath ? $ this ->getSize ($ content ) : 0 ;
88
- $ type = $ forceStaticPath ? $ this ->getType ($ content ) : '' ;
96
+ $ relativePath = $ this ->getImageRelativePath ($ content );
97
+ $ size = $ forceStaticPath ? $ this ->getSize ($ relativePath ) : 0 ;
98
+ $ type = $ forceStaticPath ? $ this ->getType ($ relativePath ) : '' ;
89
99
return $ this ->insertImageDataFactory ->create ([
90
100
'content ' => $ content ,
91
101
'size ' => $ size ,
@@ -101,7 +111,7 @@ public function execute(
101
111
*/
102
112
private function getSize (string $ path ): int
103
113
{
104
- $ directory = $ this ->getPubDirectory ();
114
+ $ directory = $ this ->getMediaDirectory ();
105
115
106
116
return $ directory ->isExist ($ path ) ? $ directory ->stat ($ path )['size ' ] : 0 ;
107
117
}
@@ -114,21 +124,34 @@ private function getSize(string $path): int
114
124
*/
115
125
public function getType (string $ path ): string
116
126
{
117
- $ fileExist = $ this ->getPubDirectory ()->isExist ($ path );
127
+ $ fileExist = $ this ->getMediaDirectory ()->isExist ($ path );
118
128
119
- return $ fileExist ? $ this ->mime ->getMimeType ($ this ->getPubDirectory ()->getAbsolutePath ($ path )) : '' ;
129
+ return $ fileExist ? $ this ->mime ->getMimeType ($ this ->getMediaDirectory ()->getAbsolutePath ($ path )) : '' ;
120
130
}
121
131
122
132
/**
123
133
* Retrieve pub directory read interface instance
124
134
*
125
135
* @return ReadInterface
126
136
*/
127
- private function getPubDirectory (): ReadInterface
137
+ private function getMediaDirectory (): ReadInterface
128
138
{
129
- if ($ this ->pubDirectory === null ) {
130
- $ this ->pubDirectory = $ this ->filesystem ->getDirectoryRead (DirectoryList::PUB );
139
+ if ($ this ->mediaDirectory === null ) {
140
+ $ this ->mediaDirectory = $ this ->filesystem ->getDirectoryRead (DirectoryList::MEDIA );
131
141
}
132
- return $ this ->pubDirectory ;
142
+
143
+ return $ this ->mediaDirectory ;
144
+ }
145
+
146
+ /**
147
+ * Retrieves image relative path
148
+ *
149
+ * @param string $content
150
+ * @return string
151
+ */
152
+ private function getImageRelativePath (string $ content ): string
153
+ {
154
+ $ mediaPath = parse_url ($ this ->imagesHelper ->getCurrentUrl (), PHP_URL_PATH );
155
+ return substr ($ content , strlen ($ mediaPath ));
133
156
}
134
157
}
0 commit comments