Skip to content

Commit

Permalink
Merge pull request #1476 from garak/fix-namer
Browse files Browse the repository at this point in the history
🐛 fix double extension in namer
  • Loading branch information
garak authored Nov 18, 2024
2 parents 10e7e01 + c2d35a2 commit 7925bea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/Naming/OrignameNamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Vich\UploaderBundle\Util\Transliterator;

/**
* OrignameNamer.
*
* @author Ivan Borzenkov <[email protected]>
*/
final class OrignameNamer implements NamerInterface, ConfigurableInterface
Expand Down Expand Up @@ -43,8 +41,8 @@ public function name(object $object, PropertyMapping $mapping): string

$extension = $this->getExtension($file);

if (\is_string($extension) && '' !== $extension) {
$name = "$name.$extension";
if (\is_string($extension) && '' !== $extension && !str_ends_with($name, ".$extension")) {
$name .= ".$extension";
}

return \uniqid().'_'.$name;
Expand Down
16 changes: 10 additions & 6 deletions tests/Naming/OrignameNamerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@
use Vich\UploaderBundle\Tests\TestCase;

/**
* OrignameNamerTest.
*
* @author Ivan Borzenkov <[email protected]>
*/
final class OrignameNamerTest extends TestCase
{
/**
* @return array<array{string, string, string, bool}>
*/
public static function fileDataProvider(): array
{
return [
['file.jpeg', '/[a-z0-9]{13}_file.jpeg/', false],
['file', '/[a-z0-9]{13}_file/', false],
['Yéöù.jpeg', '/[a-z0-9]{13}_yeou.jpeg/', true],
['file.jpeg', 'jpeg', '/^[a-z0-9]{13}_file.jpeg$/', false],
['file', 'png', '/^[a-z0-9]{13}_file.png$/', false],
['Yéöù.jpeg', 'jpg', '/^[a-z0-9]{13}_yeou.jpeg.jpg$/', true],
];
}

/**
* @dataProvider fileDataProvider
*/
public function testNameReturnsAnUniqueName(string $name, string $pattern, bool $transliterate): void
public function testNameReturnsAnUniqueName(string $name, string $ext, string $pattern, bool $transliterate): void
{
$file = $this->getUploadedFileMock();
$file
->method('getClientOriginalName')
->willReturn($name);
$file
->method('guessExtension')
->willReturn($ext);

$entity = new \DateTime();

Expand Down

0 comments on commit 7925bea

Please sign in to comment.