Skip to content

Commit 1013ab3

Browse files
committed
B2B-2058: ImageUploader uses Name class which works only with local FS
1 parent ac88ad9 commit 1013ab3

File tree

5 files changed

+127
-15
lines changed

5 files changed

+127
-15
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\File;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Filesystem\Directory\WriteInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
/**
15+
* Test for \Magento\Framework\File\Uploader
16+
*
17+
* @magentoDataFixture Magento/Framework/File/_files/framework_file_name.php
18+
*/
19+
class NameTest extends \PHPUnit\Framework\TestCase
20+
{
21+
/**
22+
* @var Name
23+
*/
24+
private $nameModel;
25+
26+
/**
27+
* @var WriteInterface
28+
*/
29+
private $mediaDirectory;
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
protected function setUp(): void
35+
{
36+
$objectManager = Bootstrap::getObjectManager();
37+
$this->mediaDirectory = $objectManager->get(\Magento\Framework\Filesystem::class)
38+
->getDirectoryWrite(DirectoryList::MEDIA);
39+
$this->nameModel = $objectManager->get(Name::class);
40+
}
41+
42+
/**
43+
*
44+
* @return void
45+
* @magentoDataFixture Magento/Framework/File/_files/framework_file_name.php
46+
*
47+
* @dataProvider getNewFileNameDataProvider
48+
*/
49+
50+
/**
51+
* @param string $destinationFile
52+
* @param string $expectedFileName
53+
* @return void
54+
*
55+
* @throws \Magento\Framework\Exception\FileSystemException
56+
*
57+
* @dataProvider getNewFileNameDataProvider
58+
*/
59+
public function testGetNewFileName($destinationFile, $expectedFileName)
60+
{
61+
$path = $this->mediaDirectory->getAbsolutePath('image/' . $destinationFile);
62+
$name = $this->nameModel->getNewFileName($path);
63+
$this->assertEquals($expectedFileName, $name);
64+
}
65+
66+
public function getNewFileNameDataProvider()
67+
{
68+
return [
69+
['image.jpg', 'image.jpg'],
70+
['image_one.jpg', 'image_one_1.jpg'],
71+
['image_two.jpg', 'image_two_2.jpg']
72+
];
73+
}
74+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
10+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
11+
12+
/** @var $mediaDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */
13+
$mediaDirectory = $objectManager->get(\Magento\Framework\Filesystem::class)->getDirectoryWrite(DirectoryList::MEDIA);
14+
$dir = 'image';
15+
$mediaDirectory->create($dir);
16+
$imageContent = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'magento_small_image.jpg');
17+
$driver = $mediaDirectory->getDriver();
18+
$driver->filePutContents($mediaDirectory->getAbsolutePath($dir . '/image_one.jpg'), $imageContent);
19+
$driver->filePutContents($mediaDirectory->getAbsolutePath($dir . '/image_two.jpg'), $imageContent);
20+
$driver->filePutContents($mediaDirectory->getAbsolutePath($dir . '/image_two_1.jpg'), $imageContent);
21+
unset($imageContent);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
10+
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
11+
$mediaDirectory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
12+
\Magento\Framework\Filesystem::class
13+
)->getDirectoryWrite(
14+
DirectoryList::MEDIA
15+
);
16+
17+
$mediaDirectory->delete('image');
2.97 KB
Loading

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,38 @@
88
namespace Magento\Framework\File;
99

1010
use Magento\Framework\App\Filesystem\DirectoryList;
11-
use Magento\Framework\Exception\FileSystemException;
12-
use Magento\Framework\Validation\ValidationException;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Filesystem\Directory\TargetDirectory;
1313

1414
/**
1515
* Utility for generating a unique file name
1616
*/
1717
class Name
1818
{
19+
/**
20+
* @var \Magento\Framework\Filesystem\Directory\TargetDirectory
21+
*/
22+
private $targetDirectory;
23+
24+
public function __construct(TargetDirectory $targetDirectory = null)
25+
{
26+
$this->targetDirectory = $targetDirectory ?? ObjectManager::getInstance()->get(TargetDirectory::class);
27+
}
28+
1929
/**
2030
* Get new file name if the given name is in use
2131
*
2232
* @param string $destinationFile
2333
* @return string
34+
* @throws \Magento\Framework\Exception\FileSystemException
2435
*/
2536
public function getNewFileName(string $destinationFile)
2637
{
2738
$fileInfo = $this->getPathInfo($destinationFile);
28-
if ($this->fileExist($destinationFile)) {
39+
if ($this->targetDirectory->getDirectoryWrite(DirectoryList::ROOT)->isExist($destinationFile)) {
2940
$index = 1;
3041
$baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
31-
while ($this->fileExist($fileInfo['dirname'] . '/' . $baseName)) {
42+
while ($this->targetDirectory->getDirectoryWrite(DirectoryList::ROOT)->isExist($fileInfo['dirname'] . '/' . $baseName)) {
3243
$baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension'];
3344
$index++;
3445
}
@@ -50,15 +61,4 @@ private function getPathInfo(string $destinationFile)
5061
{
5162
return pathinfo($destinationFile);
5263
}
53-
54-
/**
55-
* Check to see if a given file exists
56-
*
57-
* @param string $destinationFile
58-
* @return bool
59-
*/
60-
private function fileExist(string $destinationFile)
61-
{
62-
return file_exists($destinationFile);
63-
}
6464
}

0 commit comments

Comments
 (0)