Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix double extension in namer #1476

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading