Skip to content

Commit ff6b5fe

Browse files
ENGCOM-8116: Removed fileinfo extension dependency from CMS module #29671
- Merge Pull Request #29671 from konarshankar07/magento2:removed-dependency-for-fileinfo-ext - Merged commits: 1. 8344b11 2. 8da5e70 3. d312ab5 4. f7503ea 5. c09a261 6. 1209868 7. 60c0163 8. 21f1eb3 9. 0cf0e09 10. 09bd902
2 parents 6783060 + 09bd902 commit ff6b5fe

File tree

5 files changed

+66
-26
lines changed

5 files changed

+66
-26
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @SuppressWarnings(PHPMD.TooManyFields)
2323
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2424
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
25+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2526
*
2627
* @api
2728
* @since 100.0.2
@@ -152,6 +153,11 @@ class Storage extends \Magento\Framework\DataObject
152153
*/
153154
private $ioFile;
154155

156+
/**
157+
* @var \Magento\Framework\File\Mime|null
158+
*/
159+
private $mime;
160+
155161
/**
156162
* Construct
157163
*
@@ -174,6 +180,7 @@ class Storage extends \Magento\Framework\DataObject
174180
* @param \Magento\Framework\Filesystem\DriverInterface $file
175181
* @param \Magento\Framework\Filesystem\Io\File|null $ioFile
176182
* @param \Psr\Log\LoggerInterface|null $logger
183+
* @param \Magento\Framework\File\Mime $mime
177184
*
178185
* @throws \Magento\Framework\Exception\FileSystemException
179186
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -197,7 +204,8 @@ public function __construct(
197204
array $data = [],
198205
\Magento\Framework\Filesystem\DriverInterface $file = null,
199206
\Magento\Framework\Filesystem\Io\File $ioFile = null,
200-
\Psr\Log\LoggerInterface $logger = null
207+
\Psr\Log\LoggerInterface $logger = null,
208+
\Magento\Framework\File\Mime $mime = null
201209
) {
202210
$this->_session = $session;
203211
$this->_backendUrl = $backendUrl;
@@ -217,6 +225,7 @@ public function __construct(
217225
$this->_dirs = $dirs;
218226
$this->file = $file ?: ObjectManager::getInstance()->get(\Magento\Framework\Filesystem\Driver\File::class);
219227
$this->ioFile = $ioFile ?: ObjectManager::getInstance()->get(\Magento\Framework\Filesystem\Io\File::class);
228+
$this->mime = $mime ?: ObjectManager::getInstance()->get(\Magento\Framework\File\Mime::class);
220229
parent::__construct($data);
221230
}
222231

@@ -362,7 +371,7 @@ public function getFilesCollection($path, $type = null)
362371
$item->setUrl($this->_cmsWysiwygImages->getCurrentUrl() . $item->getBasename());
363372
$itemStats = $this->file->stat($item->getFilename());
364373
$item->setSize($itemStats['size']);
365-
$item->setMimeType(\mime_content_type($item->getFilename()));
374+
$item->setMimeType($this->mime->getMimeType($item->getFilename()));
366375

367376
if ($this->isImage($item->getBasename())) {
368377
$thumbUrl = $this->getThumbnailUrl($item->getFilename(), true);
@@ -647,7 +656,7 @@ public function resizeFile($source, $keepRatio = true)
647656
$image->keepAspectRatio($keepRatio);
648657

649658
list($imageWidth, $imageHeight) = $this->getResizedParams($source);
650-
659+
651660
$image->resize($imageWidth, $imageHeight);
652661
$dest = $targetDir . '/' . $this->ioFile->getPathInfo($source)['basename'];
653662
$image->save($dest);
@@ -670,7 +679,7 @@ private function getResizedParams(string $source): array
670679

671680
//phpcs:ignore Generic.PHP.NoSilencedErrors
672681
list($imageWidth, $imageHeight) = @getimagesize($source);
673-
682+
674683
if ($imageWidth && $imageHeight) {
675684
$imageWidth = $configWidth > $imageWidth ? $imageWidth : $configWidth;
676685
$imageHeight = $configHeight > $imageHeight ? $imageHeight : $configHeight;
@@ -679,7 +688,7 @@ private function getResizedParams(string $source): array
679688
}
680689
return [$configWidth, $configHeight];
681690
}
682-
691+
683692
/**
684693
* Resize images on the fly in controller action
685694
*

dev/tests/integration/testsuite/Magento/Cms/Model/Wysiwyg/Images/StorageTest.php

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
*/
77
namespace Magento\Cms\Model\Wysiwyg\Images;
88

9+
use Magento\Cms\Model\Wysiwyg\Images\Storage\Collection;
910
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\DataObject;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\Driver\File;
14+
use Magento\Framework\Filesystem\DriverInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
1016

1117
/**
1218
* Test methods of class Storage
@@ -29,22 +35,27 @@ class StorageTest extends \PHPUnit\Framework\TestCase
2935
private $objectManager;
3036

3137
/**
32-
* @var \Magento\Framework\Filesystem
38+
* @var Filesystem
3339
*/
3440
private $filesystem;
3541

3642
/**
37-
* @var \Magento\Cms\Model\Wysiwyg\Images\Storage
43+
* @var Storage
3844
*/
3945
private $storage;
4046

47+
/**
48+
* @var DriverInterface
49+
*/
50+
private $driver;
51+
4152
/**
4253
* @inheritdoc
4354
*/
4455
// phpcs:disable
4556
public static function setUpBeforeClass(): void
4657
{
47-
self::$_baseDir = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
58+
self::$_baseDir = Bootstrap::getObjectManager()->get(
4859
\Magento\Cms\Helper\Wysiwyg\Images::class
4960
)->getCurrentPath() . 'MagentoCmsModelWysiwygImagesStorageTest';
5061
if (!file_exists(self::$_baseDir)) {
@@ -60,8 +71,8 @@ public static function setUpBeforeClass(): void
6071
// phpcs:ignore
6172
public static function tearDownAfterClass(): void
6273
{
63-
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
64-
\Magento\Framework\Filesystem\Driver\File::class
74+
Bootstrap::getObjectManager()->create(
75+
File::class
6576
)->deleteDirectory(
6677
self::$_baseDir
6778
);
@@ -72,9 +83,10 @@ public static function tearDownAfterClass(): void
7283
*/
7384
protected function setUp(): void
7485
{
75-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
76-
$this->filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
77-
$this->storage = $this->objectManager->create(\Magento\Cms\Model\Wysiwyg\Images\Storage::class);
86+
$this->objectManager = Bootstrap::getObjectManager();
87+
$this->filesystem = $this->objectManager->get(Filesystem::class);
88+
$this->storage = $this->objectManager->create(Storage::class);
89+
$this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class);
7890
}
7991

8092
/**
@@ -83,16 +95,31 @@ protected function setUp(): void
8395
*/
8496
public function testGetFilesCollection(): void
8597
{
86-
\Magento\TestFramework\Helper\Bootstrap::getInstance()
98+
Bootstrap::getInstance()
8799
->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
88-
$collection = $this->storage->getFilesCollection(self::$_baseDir, 'media');
89-
$this->assertInstanceOf(\Magento\Cms\Model\Wysiwyg\Images\Storage\Collection::class, $collection);
100+
$fileName = 'magento_image.jpg';
101+
$imagePath = realpath(__DIR__ . '/../../../../Catalog/_files/' . $fileName);
102+
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
103+
$modifiableFilePath = $mediaDirectory->getAbsolutePath('MagentoCmsModelWysiwygImagesStorageTest/' . $fileName);
104+
$this->driver->copy(
105+
$imagePath,
106+
$modifiableFilePath
107+
);
108+
$this->storage->resizeFile($modifiableFilePath);
109+
$collection = $this->storage->getFilesCollection(self::$_baseDir, 'image');
110+
$this->assertInstanceOf(Collection::class, $collection);
90111
foreach ($collection as $item) {
91-
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $item);
92-
$this->assertStringEndsWith('/1.swf', $item->getUrl());
93-
$this->assertStringMatchesFormat(
94-
'http://%s/static/%s/adminhtml/%s/%s/Magento_Cms/images/placeholder_thumbnail.jpg',
95-
$item->getThumbUrl()
112+
$this->assertInstanceOf(DataObject::class, $item);
113+
$this->assertStringEndsWith('/' . $fileName, $item->getUrl());
114+
$this->assertEquals(
115+
'/pub/media/.thumbsMagentoCmsModelWysiwygImagesStorageTest/magento_image.jpg',
116+
parse_url($item->getThumbUrl(), PHP_URL_PATH),
117+
"Check if Thumbnail URL is equal to the generated URL"
118+
);
119+
$this->assertEquals(
120+
'image/jpeg',
121+
$item->getMimeType(),
122+
"Check if Mime Type is equal to the image in the file system"
96123
);
97124
return;
98125
}
@@ -121,7 +148,7 @@ public function testDeleteDirectory(): void
121148
$this->storage->createDirectory($dir, $path);
122149
$this->assertFileExists($fullPath);
123150
$this->storage->deleteDirectory($fullPath);
124-
$this->assertFileNotExists($fullPath);
151+
$this->assertFileDoesNotExist($fullPath);
125152
}
126153

127154
/**
@@ -142,7 +169,7 @@ public function testDeleteDirectoryWithExcludedDirPath(): void
142169
public function testUploadFile(): void
143170
{
144171
$fileName = 'magento_small_image.jpg';
145-
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
172+
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
146173
$filePath = $tmpDirectory->getAbsolutePath($fileName);
147174
// phpcs:disable
148175
$fixtureDir = realpath(__DIR__ . '/../../../../Catalog/_files');
@@ -172,7 +199,7 @@ public function testUploadFileWithExcludedDirPath(): void
172199
);
173200

174201
$fileName = 'magento_small_image.jpg';
175-
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
202+
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
176203
$filePath = $tmpDirectory->getAbsolutePath($fileName);
177204
// phpcs:disable
178205
$fixtureDir = realpath(__DIR__ . '/../../../../Catalog/_files');
@@ -204,7 +231,7 @@ public function testUploadFileWithWrongExtension(string $fileName, string $fileT
204231
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
205232
$this->expectExceptionMessage('File validation failed.');
206233

207-
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
234+
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
208235
$filePath = $tmpDirectory->getAbsolutePath($fileName);
209236
// phpcs:disable
210237
$fixtureDir = realpath(__DIR__ . '/../../../_files');
@@ -251,7 +278,7 @@ public function testUploadFileWithWrongFile(): void
251278
$this->expectExceptionMessage('File validation failed.');
252279

253280
$fileName = 'file.gif';
254-
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
281+
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
255282
$filePath = $tmpDirectory->getAbsolutePath($fileName);
256283
// phpcs:disable
257284
$file = fopen($filePath, "wb");

lib/internal/Magento/Framework/File/Mime.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public function getMimeType($file)
108108

109109
if (function_exists('mime_content_type')) {
110110
$result = $this->getNativeMimeType($file);
111+
} else {
112+
$imageInfo = getimagesize($file);
113+
$result = $imageInfo['mime'];
111114
}
112115

113116
if (null === $result && isset($this->mimeTypes[$extension])) {

lib/internal/Magento/Framework/File/Test/Unit/MimeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function getMimeTypeDataProvider(): array
5858
'weird extension' => [__DIR__ . '/_files/file.weird', 'application/octet-stream'],
5959
'weird uppercase extension' => [__DIR__ . '/_files/UPPERCASE.WEIRD', 'application/octet-stream'],
6060
'generic mime type' => [__DIR__ . '/_files/blank.html', 'text/html'],
61+
'tmp file mime type' => [__DIR__ . '/_files/magento', 'image/jpeg'],
6162
];
6263
}
6364
}
Binary file not shown.

0 commit comments

Comments
 (0)