Skip to content

Commit 42b287e

Browse files
committed
B2B-2058: ImageUploader uses Name class which works only with local FS
1 parent f086789 commit 42b287e

File tree

2 files changed

+50
-25
lines changed
  • dev/tests/integration/testsuite/Magento/Framework/File
  • lib/internal/Magento/Framework/File

2 files changed

+50
-25
lines changed

dev/tests/integration/testsuite/Magento/Framework/File/NameTest.php

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

1010
use Magento\Framework\App\Filesystem\DirectoryList;
11-
use Magento\Framework\Filesystem\Directory\WriteInterface;
1211
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\Framework\Filesystem;
1313

1414
/**
1515
* Test for \Magento\Framework\File\Uploader
@@ -24,18 +24,17 @@ class NameTest extends \PHPUnit\Framework\TestCase
2424
private $nameModel;
2525

2626
/**
27-
* @var WriteInterface
27+
* @var Filesystem
2828
*/
29-
private $mediaDirectory;
29+
private $fileSystem;
3030

3131
/**
3232
* @inheritdoc
3333
*/
3434
protected function setUp(): void
3535
{
3636
$objectManager = Bootstrap::getObjectManager();
37-
$this->mediaDirectory = $objectManager->get(\Magento\Framework\Filesystem::class)
38-
->getDirectoryWrite(DirectoryList::MEDIA);
37+
$this->fileSystem = $objectManager->get(\Magento\Framework\Filesystem::class);
3938
$this->nameModel = $objectManager->get(Name::class);
4039
}
4140

@@ -48,9 +47,10 @@ protected function setUp(): void
4847
*
4948
* @dataProvider getNewFileNameDataProvider
5049
*/
51-
public function testGetNewFileName($destinationFile, $expectedFileName)
50+
public function testGetNewFileName($directory, $destinationFile, $expectedFileName)
5251
{
53-
$path = $this->mediaDirectory->getAbsolutePath('image/' . $destinationFile);
52+
$directory = $this->fileSystem->getDirectoryWrite($directory);
53+
$path = $directory->getAbsolutePath('image/' . $destinationFile);
5454
$name = $this->nameModel->getNewFileName($path);
5555
$this->assertEquals($expectedFileName, $name);
5656
}
@@ -62,9 +62,11 @@ public function testGetNewFileName($destinationFile, $expectedFileName)
6262
public function getNewFileNameDataProvider()
6363
{
6464
return [
65-
['image.jpg', 'image.jpg'],
66-
['image_one.jpg', 'image_one_1.jpg'],
67-
['image_two.jpg', 'image_two_2.jpg']
65+
[DirectoryList::VAR_DIR, 'image.jpg', 'image.jpg'],
66+
[DirectoryList::VAR_DIR, 'image_one.jpg', 'image_one_1.jpg'],
67+
[DirectoryList::MEDIA, 'image.jpg', 'image.jpg'],
68+
[DirectoryList::MEDIA, 'image_one.jpg', 'image_one_1.jpg'],
69+
[DirectoryList::MEDIA, 'image_two.jpg', 'image_two_2.jpg']
6870
];
6971
}
7072
}

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

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,25 @@
99

1010
use Magento\Framework\App\Filesystem\DirectoryList;
1111
use Magento\Framework\App\ObjectManager;
12-
use Magento\Framework\Filesystem\Directory\TargetDirectory;
12+
use Magento\Framework\Filesystem;
13+
use \Magento\Framework\Filesystem\DriverInterface;
1314

1415
/**
1516
* Utility for generating a unique file name
1617
*/
1718
class Name
1819
{
1920
/**
20-
* @var \Magento\Framework\Filesystem\Directory\TargetDirectory
21+
* @var Filesystem
2122
*/
22-
private $targetDirectory;
23+
private $filesystem;
2324

2425
/**
25-
* @param TargetDirectory|null $targetDirectory
26+
* @param Filesystem|null $filesystem
2627
*/
27-
public function __construct(TargetDirectory $targetDirectory = null)
28+
public function __construct(Filesystem $filesystem = null)
2829
{
29-
$this->targetDirectory = $targetDirectory ?? ObjectManager::getInstance()->get(TargetDirectory::class);
30+
$this->filesystem = $filesystem ?: ObjectManager::getInstance()->get(Filesystem::class);
3031
}
3132

3233
/**
@@ -39,22 +40,44 @@ public function __construct(TargetDirectory $targetDirectory = null)
3940
public function getNewFileName(string $destinationFile)
4041
{
4142
$fileInfo = $this->getPathInfo($destinationFile);
42-
$directory = $this->targetDirectory->getDirectoryWrite(DirectoryList::ROOT);
43-
if ($directory->isExist($destinationFile)) {
44-
$index = 1;
45-
$baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
46-
while ($directory->isExist($fileInfo['dirname'] . '/' . $baseName)) {
47-
$baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension'];
48-
$index++;
49-
}
50-
$destFileName = $baseName;
43+
$driver = $this->filesystem->getDirectoryWrite(
44+
DirectoryList::ROOT,
45+
Filesystem\DriverPool::FILE
46+
)->getDriver();
47+
48+
/**
49+
* Try with non-local driver.
50+
*/
51+
if (!$driver->isExists($destinationFile)) {
52+
$driver = $this->filesystem->getDirectoryWrite(
53+
DirectoryList::ROOT
54+
)->getDriver();
55+
}
56+
57+
if ($driver->isExists($destinationFile)) {
58+
$destFileName = $this->generateFileName($driver, $fileInfo);
5159
} else {
5260
return $fileInfo['basename'];
5361
}
5462

5563
return $destFileName;
5664
}
5765

66+
/**
67+
* @param DriverInterface $driver
68+
* @param string $fileInfo
69+
* @param int $index
70+
* @return string
71+
*/
72+
private function generateFileName($driver, $fileInfo, $index = 1)
73+
{
74+
$baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension'];
75+
if ($driver->isExists($fileInfo['dirname'] . '/' . $baseName)) {
76+
return $this->generateFileName($driver, $fileInfo, ++$index);
77+
}
78+
return $baseName;
79+
}
80+
5881
/**
5982
* Get the path information from a given file
6083
*

0 commit comments

Comments
 (0)