How to rename a file upload using the sonata package

How do I rename a file i using the sonata package?

the file is PDF and the name is in the database folder: /upload/media/default/0001/01/0000000013ac6bf9000000017c7d6398.pdf

I want my file to look like this:/upload/media/0001/01/myfile.pdf

Thank you!

+3


source to share


2 answers


If you do not want the file (only file type) to be renamed during sonata load and keep its original name, you need to override the sonata class FileProvider

when you install Sonata Media Bundle

it it is useful to have a child package generating it easy extend

with the default it generates an extended package in src\Application

but you can choose your own location, once you have the extended package found in src\Application\Sonata\MediaBundle

you can override the class parameter FileProvider

( sonata.media.provider.file.class

) by specifying in your config file (yml, xml, etc.)

parameters:
    sonata.media.provider.file.class: Application\Sonata\MediaBundle\Provider\FileProvider

      

And now extend the class with FileProvider

sonata FileProvider

so that other functions work as they are



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 the sonata above the class is given the filename in providerReference

, by calling generateReferenceName()

in this function, it generates a unique name for each file using sha1

something like sha1($media->getName().uniqid().rand(11111, 99999))

to have the original name for the loaded file return $media->getName()

in this function and your loaded file will have the same name

+1


source


To change the filename (file type only) before uploading, you can follow my previous answer to override the class FileProvider

and then in your class override the base file function getDownloadResponse()

and define your name for the upload 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);
}

      

0


source







All Articles