From c2d35a26cf232ffeb084f2d37a7a6fd6e55db8cc Mon Sep 17 00:00:00 2001 From: Massimiliano Arione Date: Sat, 16 Nov 2024 18:18:49 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20double=20extension=20in=20?= =?UTF-8?q?namer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Naming/OrignameNamer.php | 6 ++---- tests/Naming/OrignameNamerTest.php | 16 ++++++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) 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();