Skip to content

Commit 26b506f

Browse files
committed
Merge remote-tracking branch 'origin/B2B-2058' into B2B-2022
2 parents 06aa753 + d6058a9 commit 26b506f

File tree

7 files changed

+172
-89
lines changed

7 files changed

+172
-89
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@
9696
<argument name="filesystem" xsi:type="object">fullRemoteFilesystem</argument>
9797
</arguments>
9898
</type>
99+
<type name="Magento\Framework\File\Name">
100+
<arguments>
101+
<argument name="filesystem" xsi:type="object">fullRemoteFilesystem</argument>
102+
</arguments>
103+
</type>
99104
<type name="Magento\Framework\Image\Adapter\AbstractAdapter">
100105
<plugin name="remoteImageFile" type="Magento\RemoteStorage\Plugin\Image" sortOrder="10"/>
101106
</type>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\TestFramework\Helper\Bootstrap;
12+
use Magento\Framework\Filesystem;
13+
14+
/**
15+
* Test for \Magento\Framework\File\Name
16+
*/
17+
class NameTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var Name
21+
*/
22+
private $nameModel;
23+
24+
/**
25+
* @var Filesystem
26+
*/
27+
private $fileSystem;
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
protected function setUp(): void
33+
{
34+
$objectManager = Bootstrap::getObjectManager();
35+
$this->fileSystem = $objectManager->get(\Magento\Framework\Filesystem::class);
36+
$this->nameModel = $objectManager->get(Name::class);
37+
}
38+
39+
/**
40+
* @param string $destinationFile
41+
* @param string $expectedFileName
42+
* @return void
43+
*
44+
* @throws \Magento\Framework\Exception\FileSystemException
45+
*
46+
* @magentoDataFixture Magento/Framework/File/_files/framework_file_name.php
47+
* @dataProvider getNewFileNameDataProvider
48+
*/
49+
public function testGetNewFileName($directory, $destinationFile, $expectedFileName)
50+
{
51+
$directory = $this->fileSystem->getDirectoryWrite($directory);
52+
$path = $directory->getAbsolutePath('image/' . $destinationFile);
53+
$name = $this->nameModel->getNewFileName($path);
54+
$this->assertEquals($expectedFileName, $name);
55+
}
56+
57+
/**
58+
* Data provider for testGetNewFileName
59+
* @return array
60+
*/
61+
public function getNewFileNameDataProvider()
62+
{
63+
return [
64+
[DirectoryList::VAR_DIR, 'image.jpg', 'image.jpg'],
65+
[DirectoryList::VAR_DIR, 'image_one.jpg', 'image_one_1.jpg'],
66+
[DirectoryList::MEDIA, 'image.jpg', 'image.jpg'],
67+
[DirectoryList::MEDIA, 'image_one.jpg', 'image_one_1.jpg'],
68+
[DirectoryList::MEDIA, 'image_two.jpg', 'image_two_2.jpg']
69+
];
70+
}
71+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
/** @var $varDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */
15+
$varDirectory = $objectManager->get(\Magento\Framework\Filesystem::class)->getDirectoryWrite(DirectoryList::VAR_DIR);
16+
$dir = 'image';
17+
$mediaDirectory->create($dir);
18+
$imageContent = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'magento_small_image.jpg');
19+
20+
$mediaDriver = $mediaDirectory->getDriver();
21+
$mediaDriver->filePutContents($mediaDirectory->getAbsolutePath($dir . '/image_one.jpg'), $imageContent);
22+
$mediaDriver->filePutContents($mediaDirectory->getAbsolutePath($dir . '/image_two.jpg'), $imageContent);
23+
$mediaDriver->filePutContents($mediaDirectory->getAbsolutePath($dir . '/image_two_1.jpg'), $imageContent);
24+
25+
$varDirectory->create($dir);
26+
$varDirectory->getDriver()->filePutContents($varDirectory->getAbsolutePath($dir . '/image_one.jpg'), $imageContent);
27+
28+
unset($imageContent);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
$fileSystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
11+
\Magento\Framework\Filesystem::class
12+
);
13+
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
14+
$mediaDirectory = $fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
15+
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $varDirectory */
16+
$varDirectory = $fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
17+
18+
$mediaDirectory->delete('image');
19+
$varDirectory->delete('image');
2.97 KB
Loading

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

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

1010
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\Exception\FileSystemException;
12-
use Magento\Framework\Validation\ValidationException;
13+
use Magento\Framework\Filesystem;
14+
use \Magento\Framework\Filesystem\DriverInterface;
1315

1416
/**
1517
* Utility for generating a unique file name
1618
*/
1719
class Name
1820
{
1921
/**
20-
* Get new file name if the given name is in use
22+
* @var Filesystem
23+
*/
24+
private $filesystem;
25+
26+
/**
27+
* @param Filesystem|null $filesystem
28+
*/
29+
public function __construct(Filesystem $filesystem = null)
30+
{
31+
$this->filesystem = $filesystem ?: ObjectManager::getInstance()->get(Filesystem::class);
32+
}
33+
34+
/**
35+
* Gets new file name if the given name is in use
2136
*
2237
* @param string $destinationFile
2338
* @return string
39+
* @throws \Magento\Framework\Exception\FileSystemException
2440
*/
2541
public function getNewFileName(string $destinationFile)
2642
{
2743
$fileInfo = $this->getPathInfo($destinationFile);
28-
if ($this->fileExist($destinationFile)) {
29-
$index = 1;
30-
$baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
31-
while ($this->fileExist($fileInfo['dirname'] . '/' . $baseName)) {
32-
$baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension'];
33-
$index++;
34-
}
35-
$destFileName = $baseName;
36-
} else {
37-
return $fileInfo['basename'];
44+
$driver = $this->filesystem->getDirectoryWrite(
45+
DirectoryList::ROOT,
46+
Filesystem\DriverPool::FILE
47+
)->getDriver();
48+
49+
if ($driver->isExists($destinationFile)) {
50+
return $this->generateFileName($driver, $fileInfo);
3851
}
3952

40-
return $destFileName;
53+
/**
54+
* Try with non-local driver.
55+
*/
56+
$driver = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT)->getDriver();
57+
58+
return $driver->isExists($destinationFile)
59+
? $this->generateFileName($driver, $fileInfo)
60+
: $fileInfo['basename'];
4161
}
4262

4363
/**
44-
* Get the path information from a given file
64+
* Generates new file name until file with provided name doesn't exist
4565
*
46-
* @param string $destinationFile
47-
* @return string|string[]
66+
* @param DriverInterface $driver
67+
* @param string $fileInfo
68+
* @param int $index
69+
* @return string
70+
* @throws FileSystemException
4871
*/
49-
private function getPathInfo(string $destinationFile)
72+
private function generateFileName($driver, $fileInfo, $index = 1)
5073
{
51-
return pathinfo($destinationFile);
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;
5279
}
5380

5481
/**
55-
* Check to see if a given file exists
82+
* Gets the path information from a given file
5683
*
5784
* @param string $destinationFile
58-
* @return bool
85+
* @return string|string[]
5986
*/
60-
private function fileExist(string $destinationFile)
87+
private function getPathInfo(string $destinationFile)
6188
{
62-
return file_exists($destinationFile);
89+
return pathinfo($destinationFile);
6390
}
6491
}

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

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)