If you don't wan't the file (type file only) to be rename during upload by sonata and keep its original name then you have to override sonata's FileProvider
class , when you install Sonata's Media Bundle
its good to have a child bundle by generating its easy extend
bundle by default it generates extended bundle in src\Application
but your free to choose your own location, once you have its extended bundle that is in src\Application\Sonata\MediaBundle
you can override the FileProvider
's class parameter (sonata.media.provider.file.class
) by defining in your configuration file (yml,xml etc)
parameters:
sonata.media.provider.file.class: Application\Sonata\MediaBundle\Provider\FileProvider
And now extend your FileProvider
class with sonata's FileProvider
so that other functionalities will work as it is
namespace Application\Sonata\MediaBundle\Provider;
//... other uses classes
use Sonata\MediaBundle\Provider\FileProvider as BaseProvider ;
class FileProvider extends BaseProvider
{
public function __construct($name, Filesystem $filesystem, CDNInterface $cdn, GeneratorInterface $pathGenerator, ThumbnailInterface $thumbnail, array $allowedExtensions = array(), array $allowedMimeTypes = array(), MetadataBuilderInterface $metadata = null)
{
parent::__construct($name, $filesystem, $cdn, $pathGenerator, $thumbnail);
$this->allowedExtensions = $allowedExtensions;
$this->allowedMimeTypes = $allowedMimeTypes;
$this->metadata = $metadata;
}
protected function generateReferenceName(MediaInterface $media)
{
return $media->getName();
/** return $this->generateMediaUniqId($media).'.'.$media->getBinaryContent()->guessExtension();*/
}
}
In above class sonata sets file name in providerReference
by calling generateReferenceName()
in this function its generates a unique name for each file using sha1
something like sha1($media->getName().uniqid().rand(11111, 99999))
so to have a original name for the uploaded file just return $media->getName()
in this function and your uploaded file will have same name