Sonata Media Bundle - How to extend FormatThumbnail.php

dTHQb

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?

dTHQb

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sonata Media Bundle Override

From Dev

error in sonata media bundle

From Dev

How to get Sonata Media Bundle to generate pictures in separate folders

From Dev

How to override default upload path of sonata media bundle in symfony?

From Dev

How to get Sonata Media Bundle to generate pictures in separate folders

From Dev

How to override default upload path of sonata media bundle in symfony?

From Dev

Sonata Media Bundle Installation error

From Dev

Sonata Media Bundle with Sonata Admin Bundle 3.0 (or 2.4)

From Dev

Issue Configuring Sonata Media Bundle with MongoDB

From Dev

Issue Configuring Sonata Media Bundle with MongoDB

From Dev

API Post to Sonata Media Bundle Symfony 3

From Dev

Symfony 3 - Sonata Aplication Media Bundle error

From Dev

How to use Sonata Notification Bundle?

From Dev

How to use Sonata Notification Bundle?

From Dev

Sonata admin bundle preview image from some entity in list mapper without sonata media bundle

From Dev

Symfony 2 Sonata Media bundle: saving media file image without sonata admin

From Dev

Symfony Sonata Media Bundle add images/videos to a user

From Dev

Sonata Media Bundle + AWS S3 - specify subdirectory?

From Dev

how to rename file upload with sonata bundle

From Dev

How to display a photo in Sonata Admin Bundle

From Dev

How to validation to sonata media type in symfony 2

From Dev

how to override a css of sonata admin bundle in symfony2

From Dev

How to upload image and display on edit of profile using sonata admin bundle

From Dev

How to get images listed in sonata admin bundle backend

From Dev

how to include an entity join in sonata bundle admin list view

From Dev

Sonata Admin Bundle and roles may explain to me how it works

From Dev

How to add filter to configureListField in Sonata Admin Bundle (createQuery method)

From Dev

How to create a custom form using CRUD controller in sonata admin bundle?

From Dev

sonata admin bundle symfony

Related Related

  1. 1

    Sonata Media Bundle Override

  2. 2

    error in sonata media bundle

  3. 3

    How to get Sonata Media Bundle to generate pictures in separate folders

  4. 4

    How to override default upload path of sonata media bundle in symfony?

  5. 5

    How to get Sonata Media Bundle to generate pictures in separate folders

  6. 6

    How to override default upload path of sonata media bundle in symfony?

  7. 7

    Sonata Media Bundle Installation error

  8. 8

    Sonata Media Bundle with Sonata Admin Bundle 3.0 (or 2.4)

  9. 9

    Issue Configuring Sonata Media Bundle with MongoDB

  10. 10

    Issue Configuring Sonata Media Bundle with MongoDB

  11. 11

    API Post to Sonata Media Bundle Symfony 3

  12. 12

    Symfony 3 - Sonata Aplication Media Bundle error

  13. 13

    How to use Sonata Notification Bundle?

  14. 14

    How to use Sonata Notification Bundle?

  15. 15

    Sonata admin bundle preview image from some entity in list mapper without sonata media bundle

  16. 16

    Symfony 2 Sonata Media bundle: saving media file image without sonata admin

  17. 17

    Symfony Sonata Media Bundle add images/videos to a user

  18. 18

    Sonata Media Bundle + AWS S3 - specify subdirectory?

  19. 19

    how to rename file upload with sonata bundle

  20. 20

    How to display a photo in Sonata Admin Bundle

  21. 21

    How to validation to sonata media type in symfony 2

  22. 22

    how to override a css of sonata admin bundle in symfony2

  23. 23

    How to upload image and display on edit of profile using sonata admin bundle

  24. 24

    How to get images listed in sonata admin bundle backend

  25. 25

    how to include an entity join in sonata bundle admin list view

  26. 26

    Sonata Admin Bundle and roles may explain to me how it works

  27. 27

    How to add filter to configureListField in Sonata Admin Bundle (createQuery method)

  28. 28

    How to create a custom form using CRUD controller in sonata admin bundle?

  29. 29

    sonata admin bundle symfony

HotTag

Archive