-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1476 from garak/fix-namer
🐛 fix double extension in namer
- Loading branch information
Showing
2 changed files
with
12 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,6 @@ | |
use Vich\UploaderBundle\Util\Transliterator; | ||
|
||
/** | ||
* OrignameNamer. | ||
* | ||
* @author Ivan Borzenkov <[email protected]> | ||
*/ | ||
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
||
|