Skip to content

Commit 9a6bd96

Browse files
author
Viktor Kopin
committed
magento/adobe-stock-integration#1806: fix static, refactoring
1 parent 7cfbc30 commit 9a6bd96

File tree

8 files changed

+228
-96
lines changed

8 files changed

+228
-96
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
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;
1813

1914
class GetInsertImageContent
2015
{

app/code/Magento/MediaGalleryRenditions/Test/Mftf/Test/AdminMediaGalleryInsertLargeImageFileSizeTest.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
<useCaseId value="https://github.com/magento/adobe-stock-integration/issues/1806"/>
1515
<title value="Admin user should see correct image file size after rendition"/>
1616
<testCaseId value="https://studio.cucumber.io/projects/131313/test-plan/folders/1507933/scenarios/5200023"/>
17+
<stories value="User inserts image rendition to the content"/>
1718
<description value="Admin user should see correct image file size after rendition"/>
18-
<severity value="MAJOR"/>
19+
<severity value="AVERAGE"/>
1920
<group value="media_gallery_ui"/>
2021
</annotations>
2122
<before>
@@ -31,7 +32,7 @@
3132
<actionGroup ref="AdminOpenMediaGalleryFromCategoryImageUploaderActionGroup" stepKey="openMediaGalleryForDelete"/>
3233
<actionGroup ref="AdminEnhancedMediaGalleryEnableMassActionModeActionGroup" stepKey="enableMassActionToDeleteImages"/>
3334
<actionGroup ref="AdminEnhancedMediaGallerySelectImageForMassActionActionGroup" stepKey="selectSecondImageToDelete">
34-
<argument name="imageName" value="{{LargeImage.title}}"/>
35+
<argument name="imageName" value="{{ImageUpload.fileName}}"/>
3536
</actionGroup>
3637
<actionGroup ref="AdminEnhancedMediaGalleryClickDeleteImagesButtonActionGroup" stepKey="clickDeleteSelectedButton"/>
3738
<actionGroup ref="AdminEnhancedMediaGalleryConfirmDeleteImagesActionGroup" stepKey="deleteImages"/>
@@ -45,15 +46,14 @@
4546
<actionGroup ref="AdminEditCategoryInGridPageActionGroup" stepKey="editCategoryItem">
4647
<argument name="categoryName" value="$category.name$"/>
4748
</actionGroup>
48-
<actionGroup ref="AdminSaveCategoryFormActionGroup" stepKey="saveCategoryForm"/>
4949

5050
<!-- Add image to category from gallery -->
5151
<actionGroup ref="AdminOpenMediaGalleryFromCategoryImageUploaderActionGroup" stepKey="openMediaGallery"/>
5252
<actionGroup ref="AdminEnhancedMediaGalleryUploadImageActionGroup" stepKey="addCategoryImage">
53-
<argument name="image" value="LargeImage"/>
53+
<argument name="image" value="ImageUpload"/>
5454
</actionGroup>
5555
<actionGroup ref="AdminMediaGalleryClickImageInGridActionGroup" stepKey="selectImage">
56-
<argument name="imageName" value="{{LargeImage.title}}"/>
56+
<argument name="imageName" value="{{ImageUpload.fileName}}"/>
5757
</actionGroup>
5858
<actionGroup ref="AdminMediaGalleryClickAddSelectedActionGroup" stepKey="addSelected"/>
5959

app/code/Magento/MediaGalleryUi/Controller/Adminhtml/Image/OnInsert.php

Lines changed: 15 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,65 +8,44 @@
88

99
use Magento\Backend\App\Action;
1010
use Magento\Backend\App\Action\Context;
11-
use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent;
1211
use Magento\Framework\App\Action\HttpPostActionInterface;
13-
use Magento\Framework\App\Filesystem\DirectoryList;
1412
use Magento\Framework\Controller\Result\JsonFactory;
1513
use Magento\Framework\Controller\ResultInterface;
16-
use Magento\Framework\File\Mime;
17-
use Magento\Framework\Filesystem;
18-
use Magento\Framework\Filesystem\Directory\ReadInterface;
19-
use Magento\Framework\Filesystem\File\WriteInterface;
14+
use Magento\MediaGalleryUiApi\Api\GetInsertImageDataInterface;
2015

2116
/**
22-
* Class OnInsert
17+
* OnInsert action returns on insert image details
2318
*/
2419
class OnInsert extends Action implements HttpPostActionInterface
2520
{
2621
/**
27-
* @var JsonFactory
28-
*/
29-
private $resultJsonFactory;
30-
31-
/**
32-
* @var GetInsertImageContent
33-
*/
34-
private $getInsertImageContent;
35-
36-
/**
37-
* @var Filesystem
22+
* @see _isAllowed()
3823
*/
39-
private $filesystem;
24+
public const ADMIN_RESOURCE = 'Magento_MediaGalleryUiApi::insert_assets';
4025

4126
/**
42-
* @var Mime
27+
* @var JsonFactory
4328
*/
44-
private $mime;
29+
private $resultJsonFactory;
4530

4631
/**
47-
* @var WriteInterface
32+
* @var GetInsertImageDataInterface
4833
*/
49-
private $pubDirectory;
34+
private $getInsertImageData;
5035

5136
/**
5237
* @param Context $context
5338
* @param JsonFactory $resultJsonFactory
54-
* @param GetInsertImageContent|null $getInsertImageContent
55-
* @param Filesystem $fileSystem
56-
* @param Mime $mime
39+
* @param GetInsertImageDataInterface|null $getInsertImageContent
5740
*/
5841
public function __construct(
5942
Context $context,
6043
JsonFactory $resultJsonFactory,
61-
GetInsertImageContent $getInsertImageContent,
62-
Filesystem $fileSystem,
63-
Mime $mime
44+
GetInsertImageDataInterface $getInsertImageData
6445
) {
6546
parent::__construct($context);
6647
$this->resultJsonFactory = $resultJsonFactory;
67-
$this->getInsertImageContent = $getInsertImageContent;
68-
$this->filesystem = $fileSystem;
69-
$this->mime = $mime;
48+
$this->getInsertImageData = $getInsertImageData;
7049
}
7150

7251
/**
@@ -77,54 +56,15 @@ public function __construct(
7756
public function execute()
7857
{
7958
$data = $this->getRequest()->getParams();
80-
$path = $this->getInsertImageContent->execute(
59+
$content = $this->getInsertImageData->getImageContent(
8160
$data['filename'],
8261
$data['force_static_path'],
8362
$data['as_is'],
8463
isset($data['store']) ? (int)$data['store'] : null
8564
);
8665

87-
$size = $data['force_static_path'] ? $this->getImageSize($path) : 0;
88-
$type = $data['force_static_path'] ? $this->getMimeType($path) : '';
89-
return $this->resultJsonFactory->create()->setData(['path' => $path, 'size' => $size, 'type' => $type]);
90-
}
91-
92-
/**
93-
* Retrieve size of requested file
94-
*
95-
* @param string $path
96-
* @return int
97-
*/
98-
private 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-
private 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;
66+
$size = $data['force_static_path'] ? $this->getInsertImageData->getFileSize($content) : 0;
67+
$type = $data['force_static_path'] ? $this->getInsertImageData->getMimeType($content) : '';
68+
return $this->resultJsonFactory->create()->setData(['content' => $content, 'size' => $size, 'type' => $type]);
12969
}
13070
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\MediaGalleryUi\Model;
10+
11+
use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent;
12+
use Magento\Framework\Api\ExtensionAttributesInterface;
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\MediaGalleryUiApi\Api\GetInsertImageDataInterface;
18+
19+
/**
20+
* Class responsible to provide insert image details
21+
*/
22+
class GetInsertImageData implements GetInsertImageDataInterface
23+
{
24+
private $extensionAttributes;
25+
26+
/**
27+
* @var GetInsertImageContent
28+
*/
29+
private $getInsertImageContent;
30+
31+
/**
32+
* @var Filesystem
33+
*/
34+
private $filesystem;
35+
36+
/**
37+
* @var Mime
38+
*/
39+
private $mime;
40+
41+
/**
42+
* @var ReadInterface
43+
*/
44+
private $pubDirectory;
45+
46+
/**
47+
* GetInsertImageData constructor.
48+
*
49+
* @param GetInsertImageContent $getInsertImageContent
50+
* @param Filesystem $fileSystem
51+
* @param Mime $mime
52+
*/
53+
public function __construct(
54+
GetInsertImageContent $getInsertImageContent,
55+
Filesystem $fileSystem,
56+
Mime $mime
57+
) {
58+
$this->getInsertImageContent = $getInsertImageContent;
59+
$this->filesystem = $fileSystem;
60+
$this->mime = $mime;
61+
}
62+
63+
/**
64+
* Retrieves a content (just a link or an html block) for inserting image to the content
65+
*
66+
* @param string $encodedFilename
67+
* @param bool $forceStaticPath
68+
* @param bool $renderAsTag
69+
* @param int|null $storeId
70+
* @return null|string
71+
*/
72+
public function getImageContent(
73+
string $encodedFilename,
74+
bool $forceStaticPath,
75+
bool $renderAsTag,
76+
?int $storeId = null
77+
): string {
78+
return $this->getInsertImageContent->execute($encodedFilename, $forceStaticPath, $renderAsTag, $storeId);
79+
}
80+
81+
/**
82+
* Retrieve size of requested file
83+
*
84+
* @param string $path
85+
* @return int
86+
*/
87+
public function getFileSize(string $path): int
88+
{
89+
$directory = $this->getPubDirectory();
90+
91+
return $directory->isExist($path) ? $directory->stat($path)['size'] : 0;
92+
}
93+
94+
/**
95+
* Retrieve MIME type of requested file
96+
*
97+
* @param string $path
98+
* @return string
99+
*/
100+
public function getMimeType(string $path): string
101+
{
102+
$absoluteFilePath = $this->getPubDirectory()->getAbsolutePath($path);
103+
104+
return $this->getPubDirectory()->isExist($path) ? $this->mime->getMimeType($absoluteFilePath) : '';
105+
}
106+
107+
/**
108+
* Retrieve pub directory read interface instance
109+
*
110+
* @return ReadInterface
111+
*/
112+
private function getPubDirectory(): ReadInterface
113+
{
114+
if ($this->pubDirectory === null) {
115+
$this->pubDirectory = $this->filesystem->getDirectoryRead(DirectoryList::PUB);
116+
}
117+
return $this->pubDirectory;
118+
}
119+
120+
/**
121+
* Get extension attributes
122+
*
123+
* @return ExtensionAttributesInterface|null
124+
*/
125+
public function getExtensionAttributes(): ?ExtensionAttributesInterface
126+
{
127+
return $this->extensionAttributes;
128+
}
129+
130+
/**
131+
* Set extension attributes
132+
*
133+
* @param ExtensionAttributesInterface|null $extensionAttributes
134+
* @return void
135+
*/
136+
public function setExtensionAttributes(?ExtensionAttributesInterface $extensionAttributes): void
137+
{
138+
$this->extensionAttributes = $extensionAttributes;
139+
}
140+
}

app/code/Magento/MediaGalleryUi/Test/Mftf/Data/AdminEnhancedMediaGalleryImageData.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,4 @@
4040
<data key="extension">jpg</data>
4141
<data key="keywords">magento, mediagallerymetadata</data>
4242
</entity>
43-
<entity name="LargeImage" type="image">
44-
<data key="title">magento</data>
45-
<data key="description">Description of the large magento image</data>
46-
<data key="file">magento.jpg</data>
47-
<data key="value">magento.jpg</data>
48-
<data key="fileName">magento</data>
49-
<data key="extension">jpg</data>
50-
<data key="keywords">magento, mediagallerymetadata</data>
51-
</entity>
5243
</entities>

app/code/Magento/MediaGalleryUi/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\MediaGalleryUiApi\Api\ConfigInterface" type="Magento\MediaGalleryUi\Model\Config"/>
10+
<preference for="Magento\MediaGalleryUiApi\Api\GetInsertImageDataInterface" type="Magento\MediaGalleryUi\Model\GetInsertImageData"/>
1011
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
1112
<arguments>
1213
<argument name="collections" xsi:type="array">

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

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

0 commit comments

Comments
 (0)