Sonata Media Bundle您在配置中的提供程序上有缩略图属性,您可以在其中指定
sonata.media.thumbnail.format
sonata.media.thumbnail.liip_imagine
这一切都很好,sonata.media.thumbnail.format可以很好地满足我想要实现的一切。我的问题在于这些文件中发生的事情。
在FormatThumbnail.php中有一个名为generatePublicUrl的函数,它生成媒体文件的url以及格式化文件的名称。他们使用名称或网址中的媒体ID。如果你有私人文件,不是每个人都必须能够看到这导致问题,很容易将id操纵到另一个id。
我知道将要提供的公共文件将始终保持公开,因此如果可以猜到网址,则用户将访问该文件。出于这个特定的原因,我想尝试用它在创建实际格式化文件之前使用的唯一引用替换该id,因为这不会那么容易改变。
我知道存在泄露数据的风险。
我想改变这个
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;
}
到这个
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;
}
如何覆盖捆绑包刚刚接收更改的文件?
我已经按照Sonata网站上的步骤了解了如何使用easy extends bundle安装和设置捆绑包。我有自己的Application \ Sonata \ MediaBundle文件夹,它扩展了原始的Sonata \ MediaBundle。
有关安装相关信息,请查看文档(https://sonata-project.org/bundles/media/master/doc/reference/installation.html)
但是我尝试创建自己的Thumbnail文件夹并创建一个新的FormatThumbnail.php,如下所示
<?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;
}
}
但是捆绑包仍然使用id而不是引用生成所有内容。是否有更具体的方法来实现扩展此文件并覆盖该函数?
答案 0 :(得分:0)
在查看几个不同的包后,在查看代码后,我发现它们实际上有一个参数设置为使用Sonata \ MediaBundle \ Thumbnail \ FormatThumbnail。
解决方案是覆盖配置中的参数。
#As top level classification in app/config/config.yml
parameters:
sonata.media.thumbnail.format: Application\Sonata\MediaBundle\Thumbnail\FormatThumbnail
这样,自定义FormatThumbnail类将被注入到将在包中使用的任何位置。