Skip to content

Commit 22da500

Browse files
author
Viktor Kopin
committed
adobe-stock-integration#1806: fix inserted rendition image size from media gallery on preview
1 parent 9b0e80d commit 22da500

File tree

5 files changed

+153
-7
lines changed

5 files changed

+153
-7
lines changed

app/code/Magento/Cms/Model/Wysiwyg/Images/GetInsertImageContent.php

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
use Magento\Catalog\Helper\Data as CatalogHelper;
1212
use Magento\Cms\Helper\Wysiwyg\Images as ImagesHelper;
13+
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use Magento\Framework\File\Mime;
15+
use Magento\Framework\Filesystem;
16+
use Magento\Framework\Filesystem\Directory\ReadInterface;
17+
use Magento\Framework\Filesystem\File\WriteInterface;
1318

1419
class GetInsertImageContent
1520
{
@@ -23,14 +28,37 @@ class GetInsertImageContent
2328
*/
2429
private $catalogHelper;
2530

31+
/**
32+
* @var Filesystem
33+
*/
34+
private $filesystem;
35+
36+
/**
37+
* @var Mime
38+
*/
39+
private $mime;
40+
41+
/**
42+
* @var WriteInterface
43+
*/
44+
private $pubDirectory;
45+
2646
/**
2747
* @param ImagesHelper $imagesHelper
2848
* @param CatalogHelper $catalogHelper
49+
* @param Filesystem $fileSystem
50+
* @param Mime $mime
2951
*/
30-
public function __construct(ImagesHelper $imagesHelper, CatalogHelper $catalogHelper)
31-
{
52+
public function __construct(
53+
ImagesHelper $imagesHelper,
54+
CatalogHelper $catalogHelper,
55+
Filesystem $fileSystem,
56+
Mime $mime
57+
) {
3258
$this->imagesHelper = $imagesHelper;
3359
$this->catalogHelper = $catalogHelper;
60+
$this->filesystem = $fileSystem;
61+
$this->mime = $mime;
3462
}
3563

3664
/**
@@ -60,4 +88,43 @@ public function execute(
6088

6189
return $this->imagesHelper->getImageHtmlDeclaration($filename, $renderAsTag);
6290
}
91+
92+
/**
93+
* Retrieve size of requested file
94+
*
95+
* @param string $path
96+
* @return int
97+
*/
98+
public function getImageSize(string $path): int
99+
{
100+
$directory = $this->getPubDirectory();
101+
102+
return $directory->isExist($path) ? $directory->stat($path)['size'] : 0;
103+
}
104+
105+
/**
106+
* Retrieve MIME type of requested file
107+
*
108+
* @param string $path
109+
* @return string
110+
*/
111+
public function getMimeType(string $path)
112+
{
113+
$absoluteFilePath = $this->getPubDirectory()->getAbsolutePath($path);
114+
115+
return $this->mime->getMimeType($absoluteFilePath);
116+
}
117+
118+
/**
119+
* Retrieve pub directory read interface instance
120+
*
121+
* @return ReadInterface
122+
*/
123+
private function getPubDirectory()
124+
{
125+
if ($this->pubDirectory === null) {
126+
$this->pubDirectory = $this->filesystem->getDirectoryRead(DirectoryList::PUB);
127+
}
128+
return $this->pubDirectory;
129+
}
63130
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\MediaGalleryRenditions\Controller\Adminhtml\Image;
8+
9+
use Magento\Backend\App\Action;
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent;
12+
use Magento\Framework\App\Action\HttpPostActionInterface;
13+
use Magento\Framework\Controller\Result\JsonFactory;
14+
use Magento\Framework\Controller\ResultInterface;
15+
16+
/**
17+
* Class OnInsert
18+
*/
19+
class OnInsert extends Action implements HttpPostActionInterface
20+
{
21+
/**
22+
* @var JsonFactory
23+
*/
24+
protected $resultJsonFactory;
25+
26+
/**
27+
* @var GetInsertImageContent
28+
*/
29+
private $getInsertImageContent;
30+
31+
/**
32+
* @param Context $context
33+
* @param JsonFactory $resultJsonFactory
34+
* @param GetInsertImageContent|null $getInsertImageContent
35+
*/
36+
public function __construct(
37+
Context $context,
38+
JsonFactory $resultJsonFactory,
39+
GetInsertImageContent $getInsertImageContent
40+
) {
41+
parent::__construct($context);
42+
$this->resultJsonFactory = $resultJsonFactory;
43+
$this->getInsertImageContent = $getInsertImageContent;
44+
}
45+
46+
/**
47+
* Return a content (just a link or an html block) for inserting image to the content
48+
*
49+
* @return ResultInterface
50+
*/
51+
public function execute()
52+
{
53+
$data = $this->getRequest()->getParams();
54+
$path = $this->getInsertImageContent->execute(
55+
$data['filename'],
56+
$data['force_static_path'],
57+
$data['as_is'],
58+
isset($data['store']) ? (int)$data['store'] : null
59+
);
60+
$size = $this->getInsertImageContent->getImageSize($path);
61+
$type = $this->getInsertImageContent->getMimeType($path);
62+
return $this->resultJsonFactory->create()->setData(['path' => $path, 'size' => $size, 'type' => $type]);
63+
}
64+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
10+
<router id="admin">
11+
<route id="media_gallery_renditions" frontName="media_gallery_renditions">
12+
<module name="Magento_MediaGalleryRenditions" before="Magento_Backend" />
13+
</route>
14+
</router>
15+
</config>

app/code/Magento/MediaGalleryUi/Ui/Component/Listing/Columns/Url.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function prepare(): void
9898
array_replace_recursive(
9999
(array)$this->getData('config'),
100100
[
101-
'onInsertUrl' => $this->urlInterface->getUrl('cms/wysiwyg_images/oninsert'),
101+
'onInsertUrl' => $this->urlInterface->getUrl('media_gallery_renditions/image/oninsert'),
102102
'storeId' => $this->storeManager->getStore()->getId()
103103
]
104104
)

app/code/Magento/MediaGalleryUi/view/adminhtml/web/js/grid/columns/image/insertImageAction.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ define([
4747
showLoader: true
4848
}).done($.proxy(function (data) {
4949
if (targetElement.is('textarea')) {
50-
this.insertAtCursor(targetElement.get(0), data);
50+
this.insertAtCursor(targetElement.get(0), data.path);
5151
targetElement.focus();
5252
$(targetElement).change();
5353
} else {
54-
targetElement.val(data)
55-
.data('size', record.size)
56-
.data('mime-type', record['content_type'])
54+
targetElement.val(data.path)
55+
.data('size', data.size)
56+
.data('mime-type', data.type)
5757
.trigger('change');
5858
}
5959
}, this));

0 commit comments

Comments
 (0)