简体   繁体   中英

Sonata Media Bundle - How to extend FormatThumbnail.php

The Sonata Media Bundle you have the thumbnail property on a provider in the config where you can specify either

sonata.media.thumbnail.format
sonata.media.thumbnail.liip_imagine

This all fine and the sonata.media.thumbnail.format one works fine for everything I want to achieve. My problem comes in with what happens within these files.

In the FormatThumbnail.php there is a function called generatePublicUrl where they generate the url of the media file and also the name of the formatted file. They use the media id within the name or url. If you have private files not everyone must be able to see this causes a problem with it is easy to manipulate the id to another id.

I know the public files that will be served will always stay public so if the url can be guessed the user will access the file. For this specific reason I want to try and replace that id with the unique reference that the bundle uses before they create the actual formatted files as this will not be as easy to just change.

I am aware that there are still risks of leaking out data.

I want to change this

public function generatePublicUrl(MediaProviderInterface $provider, MediaInterface $media, $format)
{
    if ($format == 'reference') {
        $path = $provider->getReferenceImage($media);
    } else {
        $path = sprintf('%s/thumb_%s_%s.%s', $provider->generatePath($media), $media->getId(), $format, $this->getExtension($media));
    }

    return $path;
}

to this

public function generatePublicUrl(MediaProviderInterface $provider, MediaInterface $media, $format)
{
    if ($format == 'reference') {
        $path = $provider->getReferenceImage($media);
    } else {
        $path = sprintf('%s/thumb_%s_%s.%s', $provider->generatePath($media), $media->getProviderReference(), $format, $this->getExtension($media));
    }

    return $path;
}

How do I override the file that the bundle just picks up the change?

I have followed the steps on Sonata's site on how to install and set up the bundle using the easy extends bundle. I have my own Application\\Sonata\\MediaBundle folder that is extending the original Sonata\\MediaBundle.

For installation related information have a look through the documentation( https://sonata-project.org/bundles/media/master/doc/reference/installation.html )

However I tried to create my own Thumbnail folder and creating a new FormatThumbnail.php as follows

<?php

namespace Application\Sonata\MediaBundle\Thumbnail;

use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\MediaBundle\Thumbnail\FormatThumbnail as BaseFormatThumbnail;

class FormatThumbnail extends BaseFormatThumbnail
{
    /**
     * Overriding this to replace the id with the reference
     *
     * {@inheritdoc}
     */
    public function generatePublicUrl(MediaProviderInterface $provider, MediaInterface $media, $format)
    {
        if ($format == 'reference') {
            $path = $provider->getReferenceImage($media);
        } else {
            $path = sprintf('%s/thumb_%s_%s.%s', $provider->generatePath($media), $media->getProviderReference(), $format, $this->getExtension($media));
        }

        return $path;
    }
}

But the bundle still generates everything using the id instead of the reference. Is there a more specific way to achieve extending this file and overriding the function?

After looking at a few different bundles and after looking in code I found that they physically have a parameter which is set to use Sonata\\MediaBundle\\Thumbnail\\FormatThumbnail.

The solution is to override the parameter in the config aswell.

#As top level classification in app/config/config.yml
parameters:
    sonata.media.thumbnail.format: Application\Sonata\MediaBundle\Thumbnail\FormatThumbnail

This way the custom FormatThumbnail class is injected everywhere it will be used within the bundle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM