diff --git a/src/Naming/OrignameNamer.php b/src/Naming/OrignameNamer.php index ac004bec..a4418ddf 100644 --- a/src/Naming/OrignameNamer.php +++ b/src/Naming/OrignameNamer.php @@ -8,8 +8,6 @@ use Vich\UploaderBundle\Util\Transliterator; /** - * OrignameNamer. - * * @author Ivan Borzenkov */ final class OrignameNamer implements NamerInterface, ConfigurableInterface @@ -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; diff --git a/tests/Naming/OrignameNamerTest.php b/tests/Naming/OrignameNamerTest.php index c0f586d2..c86c48d4 100644 --- a/tests/Naming/OrignameNamerTest.php +++ b/tests/Naming/OrignameNamerTest.php @@ -6,30 +6,34 @@ use Vich\UploaderBundle\Tests\TestCase; /** - * OrignameNamerTest. - * * @author Ivan Borzenkov */ final class OrignameNamerTest extends TestCase { + /** + * @return array + */ 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();