3

How to rename file i download with sonata-bundle ?

the file is PDF and the name in database folder is : /upload/media/default/0001/01/0000000013ac6bf9000000017c7d6398.pdf I want my file appears like this : /upload/media/0001/01/myfile.pdf

thank you !!

Darkness007
  • 93
  • 2
  • 12

2 Answers2

2

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

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • If doing this I would suggest adding a timestamp or at least rand(11111, 99999) to the filename, because the file can get overwritten if another file with the same name is uploaded. – kunicmarko20 May 08 '17 at 19:20
0

To change the file (file type only) name before download you can follow my previous answer for overriding FileProvider class and then in your class override base file provider's getDownloadResponse()function and define your desired name for the download file

public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = array())
{

    $guesser = ExtensionGuesser::getInstance();
    $extension = $guesser->guess($media->getContentType());
    // build the default headers
    $headers = array_merge(array(
        'Content-Type'          => $media->getContentType(),
        'Content-Disposition'   => sprintf('attachment; filename="%s"', 'myfile.'.$extension),
    ), $headers);

    if (!in_array($mode, array('http', 'X-Sendfile', 'X-Accel-Redirect'))) {
        throw new \RuntimeException('Invalid mode provided');
    }

    if ($mode == 'http') {
        if ($format == 'reference') {
            $file = $this->getReferenceFile($media);
        } else {
            $file = $this->getFilesystem()->get($this->generatePrivateUrl($media, $format));
        }

        return new StreamedResponse(function() use ($file) {
            echo $file->getContent();
        }, 200, $headers);
    }

    if (!$this->getFilesystem()->getAdapter() instanceof \Sonata\MediaBundle\Filesystem\Local) {
        throw new \RuntimeException('Cannot use X-Sendfile or X-Accel-Redirect with non \Sonata\MediaBundle\Filesystem\Local');
    }

    $filename = sprintf('%s/%s',
        $this->getFilesystem()->getAdapter()->getDirectory(),
        $this->generatePrivateUrl($media, $format)
    );

    return new BinaryFileResponse($filename, 200, $headers);
}
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118