dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.85k stars 519 forks source link

SlugNamer does not work correctly with dot in filename #1247

Closed AddMoreScripts closed 2 years ago

AddMoreScripts commented 2 years ago

Bug Report

Q A
BC Break no
Bundle version 1.18.0
Symfony version 5.2.14
PHP version 8.0

Summary

I noticed that if the file name contains two or more dots in the name, then transliteration occurs only up to the first dot. For instance:

Current behavior

раз.два.jpg -> raz.два.jpg

How to reproduce

Set in config vich_uploader.yaml namer option:
namer: Vich\UploaderBundle\Naming\SlugNamer and upload file named with dot раз.два.jpg

Expected behavior

The file name must be fully translated, possibly replacing the dot with another character

Possible problem solution

in file /src/Naming/SlugNamer.php in method name():

    $originalName = $file->getClientOriginalName();
    $extension = \strtolower(\pathinfo($originalName, \PATHINFO_EXTENSION));
    $basename = \substr(\pathinfo($originalName, \PATHINFO_FILENAME), 0, 240);
    $basename = \strtolower($this->transliterator->transliterate($basename));

There is a separation of the file name and extension, and then it is sent to the transliterate method of the Transliterator class. But in transliterate() method FilenameUtils::spitNameByExtension is used and split filename again, and transliterate only first part.

It seems to me that we can simply pass the original file name $originalName with the extension to the method $this->transliterator->transliterate() without split it twice.

But maybe I'm wrong.

garak commented 2 years ago

The namer is not meant to cover all possible situations, only the most common ones. If you have this need, you can always implement your own namer.

guilliamxavier commented 2 years ago

@garak: There is clearly a logical bug if the filename is split twice :confused: e.g. "раз1.раз2.раз3.jpg" gives "raz1-raz2.раз3.jpg" ("раз1.раз2.раз3.jpg" is first split to "раз1.раз2.раз3" + "jpg", then "раз1.раз2.раз3" is split again to "раз1.раз2" + "раз3", then only "раз1.раз2" is sluggified to "raz1-raz2")

garak commented 2 years ago

It's not a bug. It's by design, and it covers 99% of cases.

guilliamxavier commented 2 years ago

So you are saying that double-splitting the extension, resulting in this (vertical table):

Inputраз1.jpgраз1.раз2.jpgраз1.раз2.раз3.jpgраз1.раз2.раз3.раз4.jpg
Outputraz1.jpgraz1.раз2.jpgraz1-raz2.раз3.jpgraz1-raz2-raz3.раз4.jpg

is by design... :confused: Anyway, I don't want to argue more

garak commented 2 years ago

You can use your own namer, or add a validation to disallow files with additional dots.