5
5
*/
6
6
namespace Magento \Catalog \Model \Category \Attribute \Backend ;
7
7
8
+ use Magento \Catalog \Model \ImageUploader ;
8
9
use Magento \Framework \App \Filesystem \DirectoryList ;
10
+ use Magento \Framework \App \ObjectManager ;
9
11
use Magento \Framework \File \Uploader ;
12
+ use Magento \Store \Api \Data \StoreInterface ;
13
+ use Magento \Store \Model \StoreManagerInterface ;
10
14
11
15
/**
12
16
* Catalog category image attribute backend model
@@ -45,7 +49,7 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
45
49
protected $ _logger ;
46
50
47
51
/**
48
- * @var \Magento\Catalog\Model\ ImageUploader
52
+ * @var ImageUploader
49
53
*/
50
54
private $ imageUploader ;
51
55
@@ -54,19 +58,32 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
54
58
*/
55
59
private $ additionalData = '_additional_data_ ' ;
56
60
61
+ /**
62
+ * @var StoreManagerInterface
63
+ */
64
+ private $ storeManager ;
65
+
57
66
/**
58
67
* @param \Psr\Log\LoggerInterface $logger
59
68
* @param \Magento\Framework\Filesystem $filesystem
60
69
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
70
+ * @param StoreManagerInterface $storeManager
71
+ * @param ImageUploader $imageUploader
61
72
*/
62
73
public function __construct (
63
74
\Psr \Log \LoggerInterface $ logger ,
64
75
\Magento \Framework \Filesystem $ filesystem ,
65
- \Magento \MediaStorage \Model \File \UploaderFactory $ fileUploaderFactory
76
+ \Magento \MediaStorage \Model \File \UploaderFactory $ fileUploaderFactory ,
77
+ StoreManagerInterface $ storeManager = null ,
78
+ ImageUploader $ imageUploader = null
66
79
) {
67
80
$ this ->_filesystem = $ filesystem ;
68
81
$ this ->_fileUploaderFactory = $ fileUploaderFactory ;
69
82
$ this ->_logger = $ logger ;
83
+ $ this ->storeManager = $ storeManager ??
84
+ ObjectManager::getInstance ()->get (StoreManagerInterface::class);
85
+ $ this ->imageUploader = $ imageUploader ??
86
+ ObjectManager::getInstance ()->get (ImageUploader::class);
70
87
}
71
88
72
89
/**
@@ -91,16 +108,17 @@ private function getUploadedImageName($value)
91
108
*
92
109
* @param string $imageName
93
110
* @return string
111
+ * @throws \Magento\Framework\Exception\FileSystemException
94
112
*/
95
113
private function checkUniqueImageName (string $ imageName ): string
96
114
{
97
- $ imageUploader = $ this ->getImageUploader ();
98
115
$ mediaDirectory = $ this ->_filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
99
116
$ imageAbsolutePath = $ mediaDirectory ->getAbsolutePath (
100
- $ imageUploader ->getBasePath () . DIRECTORY_SEPARATOR . $ imageName
117
+ $ this -> imageUploader ->getBasePath () . DIRECTORY_SEPARATOR . $ imageName
101
118
);
102
119
103
- $ imageName = Uploader::getNewFilename ($ imageAbsolutePath );
120
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
121
+ $ imageName = call_user_func ([Uploader::class, 'getNewFilename ' ], $ imageAbsolutePath );
104
122
105
123
return $ imageName ;
106
124
}
@@ -112,14 +130,26 @@ private function checkUniqueImageName(string $imageName): string
112
130
*
113
131
* @param \Magento\Framework\DataObject $object
114
132
* @return $this
133
+ * @throws \Magento\Framework\Exception\FileSystemException
115
134
* @since 101.0.8
116
135
*/
117
136
public function beforeSave ($ object )
118
137
{
119
138
$ attributeName = $ this ->getAttribute ()->getName ();
120
139
$ value = $ object ->getData ($ attributeName );
121
140
122
- if ($ this ->fileResidesOutsideCategoryDir ($ value )) {
141
+ if ($ this ->isTmpFileAvailable ($ value ) && $ imageName = $ this ->getUploadedImageName ($ value )) {
142
+ try {
143
+ /** @var StoreInterface $store */
144
+ $ store = $ this ->storeManager ->getStore ();
145
+ $ baseMediaDir = $ store ->getBaseMediaDir ();
146
+ $ newImgRelativePath = $ this ->imageUploader ->moveFileFromTmp ($ imageName , true );
147
+ $ value [0 ]['url ' ] = '/ ' . $ baseMediaDir . '/ ' . $ newImgRelativePath ;
148
+ $ value [0 ]['name ' ] = $ value [0 ]['url ' ];
149
+ } catch (\Exception $ e ) {
150
+ $ this ->_logger ->critical ($ e );
151
+ }
152
+ } elseif ($ this ->fileResidesOutsideCategoryDir ($ value )) {
123
153
// use relative path for image attribute so we know it's outside of category dir when we fetch it
124
154
// phpcs:ignore Magento2.Functions.DiscouragedFunction
125
155
$ value [0 ]['url ' ] = parse_url ($ value [0 ]['url ' ], PHP_URL_PATH );
@@ -139,23 +169,6 @@ public function beforeSave($object)
139
169
return parent ::beforeSave ($ object );
140
170
}
141
171
142
- /**
143
- * Get Instance of Category Image Uploader.
144
- *
145
- * @return \Magento\Catalog\Model\ImageUploader
146
- *
147
- * @deprecated 101.0.0
148
- */
149
- private function getImageUploader ()
150
- {
151
- if ($ this ->imageUploader === null ) {
152
- $ this ->imageUploader = \Magento \Framework \App \ObjectManager::getInstance ()
153
- ->get (\Magento \Catalog \CategoryImageUpload::class);
154
- }
155
-
156
- return $ this ->imageUploader ;
157
- }
158
-
159
172
/**
160
173
* Check if temporary file is available for new image upload.
161
174
*
@@ -194,19 +207,10 @@ private function fileResidesOutsideCategoryDir($value)
194
207
*
195
208
* @param \Magento\Framework\DataObject $object
196
209
* @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
210
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
197
211
*/
198
212
public function afterSave ($ object )
199
213
{
200
- $ value = $ object ->getData ($ this ->additionalData . $ this ->getAttribute ()->getName ());
201
-
202
- if ($ this ->isTmpFileAvailable ($ value ) && $ imageName = $ this ->getUploadedImageName ($ value )) {
203
- try {
204
- $ this ->getImageUploader ()->moveFileFromTmp ($ imageName );
205
- } catch (\Exception $ e ) {
206
- $ this ->_logger ->critical ($ e );
207
- }
208
- }
209
-
210
214
return $ this ;
211
215
}
212
216
}
0 commit comments