Skip to content

Commit 1bc55b2

Browse files
author
Alex Paliarush
committed
MAGETWO-58335: [Backport] - Can't view uploaded image - for 2.1
1 parent 637a87a commit 1bc55b2

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

app/code/Magento/Customer/Model/FileProcessor.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Customer\Api\CustomerMetadataInterface;
1010
use Magento\Framework\App\Filesystem\DirectoryList;
1111
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\File\Mime;
1213
use Magento\Framework\Filesystem;
1314
use Magento\Framework\Filesystem\Directory\WriteInterface;
1415
use Magento\Framework\Url\EncoderInterface;
@@ -56,12 +57,18 @@ class FileProcessor
5657
*/
5758
private $allowedExtensions = [];
5859

60+
/**
61+
* @var Mime
62+
*/
63+
private $mime;
64+
5965
/**
6066
* @param Filesystem $filesystem
6167
* @param UploaderFactory $uploaderFactory
6268
* @param UrlInterface $urlBuilder
6369
* @param EncoderInterface $urlEncoder
6470
* @param string $entityTypeCode
71+
* @param Mime $mime
6572
* @param array $allowedExtensions
6673
*/
6774
public function __construct(
@@ -70,13 +77,15 @@ public function __construct(
7077
UrlInterface $urlBuilder,
7178
EncoderInterface $urlEncoder,
7279
$entityTypeCode,
80+
Mime $mime,
7381
array $allowedExtensions = []
7482
) {
7583
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
7684
$this->uploaderFactory = $uploaderFactory;
7785
$this->urlBuilder = $urlBuilder;
7886
$this->urlEncoder = $urlEncoder;
7987
$this->entityTypeCode = $entityTypeCode;
88+
$this->mime = $mime;
8089
$this->allowedExtensions = $allowedExtensions;
8190
}
8291

@@ -110,6 +119,21 @@ public function getStat($fileName)
110119
return $result;
111120
}
112121

122+
/**
123+
* Retrieve MIME type of requested file
124+
*
125+
* @param string $fileName
126+
* @return string
127+
*/
128+
public function getMimeType($fileName)
129+
{
130+
$filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
131+
$absoluteFilePath = $this->mediaDirectory->getAbsolutePath($filePath);
132+
133+
$result = $this->mime->getMimeType($absoluteFilePath);
134+
return $result;
135+
}
136+
113137
/**
114138
* Check if the file exists
115139
*

app/code/Magento/Customer/Test/Unit/Model/FileProcessorTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class FileProcessorTest extends \PHPUnit_Framework_TestCase
3737
*/
3838
private $mediaDirectory;
3939

40+
/**
41+
* @var \Magento\Framework\File\Mime|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $mime;
44+
4045
protected function setUp()
4146
{
4247
$this->mediaDirectory = $this->getMockBuilder('Magento\Framework\Filesystem\Directory\WriteInterface')
@@ -60,6 +65,10 @@ protected function setUp()
6065

6166
$this->urlEncoder = $this->getMockBuilder('Magento\Framework\Url\EncoderInterface')
6267
->getMockForAbstractClass();
68+
69+
$this->mime = $this->getMockBuilder(\Magento\Framework\File\Mime::class)
70+
->disableOriginalConstructor()
71+
->getMock();
6372
}
6473

6574
private function getModel($entityTypeCode, array $allowedExtensions = [])
@@ -70,6 +79,7 @@ private function getModel($entityTypeCode, array $allowedExtensions = [])
7079
$this->urlBuilder,
7180
$this->urlEncoder,
7281
$entityTypeCode,
82+
$this->mime,
7383
$allowedExtensions
7484
);
7585
return $model;
@@ -376,4 +386,26 @@ public function testMoveTemporaryFileWithException()
376386
$model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
377387
$model->moveTemporaryFile($filePath);
378388
}
389+
390+
public function testGetMimeType()
391+
{
392+
$fileName = '/filename.ext1';
393+
$absoluteFilePath = '/absolute_path/customer/filename.ext1';
394+
395+
$expected = 'ext1';
396+
397+
$this->mediaDirectory->expects($this->once())
398+
->method('getAbsolutePath')
399+
->with(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER . '/' . ltrim($fileName, '/'))
400+
->willReturn($absoluteFilePath);
401+
402+
$this->mime->expects($this->once())
403+
->method('getMimeType')
404+
->with($absoluteFilePath)
405+
->willReturn($expected);
406+
407+
$model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
408+
409+
$this->assertEquals($expected, $model->getMimeType($fileName));
410+
}
379411
}

0 commit comments

Comments
 (0)