Skip to content

Commit 3b0a8b7

Browse files
committed
Merge remote-tracking branch 'origin/MC-29523' into 2.4-develop-pr5
2 parents 12fd3d5 + 1c3ee94 commit 3b0a8b7

File tree

4 files changed

+142
-57
lines changed

4 files changed

+142
-57
lines changed

app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
*/
66
namespace Magento\Catalog\Model\Category\Attribute\Backend;
77

8+
use Magento\Catalog\Model\ImageUploader;
89
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\App\ObjectManager;
911
use Magento\Framework\File\Uploader;
12+
use Magento\Store\Api\Data\StoreInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
1014

1115
/**
1216
* Catalog category image attribute backend model
@@ -45,7 +49,7 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
4549
protected $_logger;
4650

4751
/**
48-
* @var \Magento\Catalog\Model\ImageUploader
52+
* @var ImageUploader
4953
*/
5054
private $imageUploader;
5155

@@ -54,19 +58,32 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
5458
*/
5559
private $additionalData = '_additional_data_';
5660

61+
/**
62+
* @var StoreManagerInterface
63+
*/
64+
private $storeManager;
65+
5766
/**
5867
* @param \Psr\Log\LoggerInterface $logger
5968
* @param \Magento\Framework\Filesystem $filesystem
6069
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
70+
* @param StoreManagerInterface $storeManager
71+
* @param ImageUploader $imageUploader
6172
*/
6273
public function __construct(
6374
\Psr\Log\LoggerInterface $logger,
6475
\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
6679
) {
6780
$this->_filesystem = $filesystem;
6881
$this->_fileUploaderFactory = $fileUploaderFactory;
6982
$this->_logger = $logger;
83+
$this->storeManager = $storeManager ??
84+
ObjectManager::getInstance()->get(StoreManagerInterface::class);
85+
$this->imageUploader = $imageUploader ??
86+
ObjectManager::getInstance()->get(ImageUploader::class);
7087
}
7188

7289
/**
@@ -94,13 +111,13 @@ private function getUploadedImageName($value)
94111
*/
95112
private function checkUniqueImageName(string $imageName): string
96113
{
97-
$imageUploader = $this->getImageUploader();
98114
$mediaDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::MEDIA);
99115
$imageAbsolutePath = $mediaDirectory->getAbsolutePath(
100-
$imageUploader->getBasePath() . DIRECTORY_SEPARATOR . $imageName
116+
$this->imageUploader->getBasePath() . DIRECTORY_SEPARATOR . $imageName
101117
);
102118

103-
$imageName = Uploader::getNewFilename($imageAbsolutePath);
119+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
120+
$imageName = call_user_func([Uploader::class, 'getNewFilename'], $imageAbsolutePath);
104121

105122
return $imageName;
106123
}
@@ -119,7 +136,18 @@ public function beforeSave($object)
119136
$attributeName = $this->getAttribute()->getName();
120137
$value = $object->getData($attributeName);
121138

122-
if ($this->fileResidesOutsideCategoryDir($value)) {
139+
if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) {
140+
try {
141+
/** @var StoreInterface $store */
142+
$store = $this->storeManager->getStore();
143+
$baseMediaDir = $store->getBaseMediaDir();
144+
$newImgRelativePath = $this->imageUploader->moveFileFromTmp($imageName, true);
145+
$value[0]['url'] = '/' . $baseMediaDir . '/' . $newImgRelativePath;
146+
$value[0]['name'] = $value[0]['url'];
147+
} catch (\Exception $e) {
148+
$this->_logger->critical($e);
149+
}
150+
} elseif ($this->fileResidesOutsideCategoryDir($value)) {
123151
// use relative path for image attribute so we know it's outside of category dir when we fetch it
124152
// phpcs:ignore Magento2.Functions.DiscouragedFunction
125153
$value[0]['url'] = parse_url($value[0]['url'], PHP_URL_PATH);
@@ -139,23 +167,6 @@ public function beforeSave($object)
139167
return parent::beforeSave($object);
140168
}
141169

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-
159170
/**
160171
* Check if temporary file is available for new image upload.
161172
*
@@ -194,19 +205,10 @@ private function fileResidesOutsideCategoryDir($value)
194205
*
195206
* @param \Magento\Framework\DataObject $object
196207
* @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
208+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
197209
*/
198210
public function afterSave($object)
199211
{
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-
210212
return $this;
211213
}
212214
}

app/code/Magento/Catalog/Model/ImageUploader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ public function getFilePath($path, $imageName)
191191
* Checking file for moving and move it
192192
*
193193
* @param string $imageName
194-
*
194+
* @param bool $returnRelativePath
195195
* @return string
196196
*
197197
* @throws \Magento\Framework\Exception\LocalizedException
198198
*/
199-
public function moveFileFromTmp($imageName)
199+
public function moveFileFromTmp($imageName, $returnRelativePath = false)
200200
{
201201
$baseTmpPath = $this->getBaseTmpPath();
202202
$basePath = $this->getBasePath();
@@ -226,7 +226,7 @@ public function moveFileFromTmp($imageName)
226226
);
227227
}
228228

229-
return $imageName;
229+
return $returnRelativePath ? $baseImagePath : $imageName;
230230
}
231231

232232
/**

0 commit comments

Comments
 (0)