Skip to content

Commit 0cafa20

Browse files
author
Viktor Kopin
committed
1 parent cbc38f6 commit 0cafa20

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

app/code/Magento/MediaGalleryUi/Model/InsertImageData/GetInsertImageData.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\MediaGalleryUi\Model\InsertImageData;
1010

11+
use Magento\Cms\Helper\Wysiwyg\Images;
1112
use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent;
1213
use Magento\Framework\App\Filesystem\DirectoryList;
1314
use Magento\Framework\File\Mime;
@@ -21,7 +22,7 @@ class GetInsertImageData
2122
/**
2223
* @var ReadInterface
2324
*/
24-
private $pubDirectory;
25+
private $mediaDirectory;
2526

2627
/**
2728
* @var Filesystem
@@ -43,24 +44,32 @@ class GetInsertImageData
4344
*/
4445
private $mime;
4546

47+
/**
48+
* @var Images
49+
*/
50+
private $imagesHelper;
51+
4652
/**
4753
* GetInsertImageData constructor.
4854
*
4955
* @param GetInsertImageContent $getInsertImageContent
5056
* @param Filesystem $fileSystem
5157
* @param Mime $mime
5258
* @param InsertImageDataFactory $insertImageDataFactory
59+
* @param Images $imagesHelper
5360
*/
5461
public function __construct(
5562
GetInsertImageContent $getInsertImageContent,
5663
Filesystem $fileSystem,
5764
Mime $mime,
58-
InsertImageDataFactory $insertImageDataFactory
65+
InsertImageDataFactory $insertImageDataFactory,
66+
Images $imagesHelper
5967
) {
6068
$this->getInsertImageContent = $getInsertImageContent;
6169
$this->filesystem = $fileSystem;
6270
$this->mime = $mime;
6371
$this->insertImageDataFactory = $insertImageDataFactory;
72+
$this->imagesHelper = $imagesHelper;
6473
}
6574

6675
/**
@@ -84,8 +93,9 @@ public function execute(
8493
$renderAsTag,
8594
$storeId
8695
);
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) : '';
8999
return $this->insertImageDataFactory->create([
90100
'content' => $content,
91101
'size' => $size,
@@ -101,7 +111,7 @@ public function execute(
101111
*/
102112
private function getSize(string $path): int
103113
{
104-
$directory = $this->getPubDirectory();
114+
$directory = $this->getMediaDirectory();
105115

106116
return $directory->isExist($path) ? $directory->stat($path)['size'] : 0;
107117
}
@@ -114,21 +124,34 @@ private function getSize(string $path): int
114124
*/
115125
public function getType(string $path): string
116126
{
117-
$fileExist = $this->getPubDirectory()->isExist($path);
127+
$fileExist = $this->getMediaDirectory()->isExist($path);
118128

119-
return $fileExist ? $this->mime->getMimeType($this->getPubDirectory()->getAbsolutePath($path)) : '';
129+
return $fileExist ? $this->mime->getMimeType($this->getMediaDirectory()->getAbsolutePath($path)) : '';
120130
}
121131

122132
/**
123133
* Retrieve pub directory read interface instance
124134
*
125135
* @return ReadInterface
126136
*/
127-
private function getPubDirectory(): ReadInterface
137+
private function getMediaDirectory(): ReadInterface
128138
{
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);
131141
}
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));
133156
}
134157
}

0 commit comments

Comments
 (0)