Php SonataMediaBundle:如何添加svg文件扩展名

Php SonataMediaBundle:如何添加svg文件扩展名,php,symfony,svg,sonata-admin,sonata-media-bundle,Php,Symfony,Svg,Sonata Admin,Sonata Media Bundle,我需要将.svg文件扩展名添加到我的配置中 目前在我的项目中,我还有其他扩展(pdf,图片) 我做了以下更改 在上下文中添加了一个新的svg_文件 添加了文件提供程序(在配置文件末尾) 在允许的扩展中添加svg 在允许的mime类型中添加了图像/svg+xml 现在,我可以上传svg文件,但问题是用户可以上传其他文件扩展名,例如pdf等 怎样才能避免呢?或者找到一种合适的方式进行表单验证 奏鸣曲文献: 帮助了我,但不是为了表单验证 我错过了什么 我更改了以下文件: #app/confi

我需要将.svg文件扩展名添加到我的配置中

目前在我的项目中,我还有其他扩展(pdf,图片)

我做了以下更改

  • 上下文中添加了一个新的svg_文件
  • 添加了文件提供程序(在配置文件末尾)
  • 允许的扩展中添加svg
  • 在允许的mime类型中添加了图像/svg+xml
  • 现在,我可以上传svg文件,但问题是用户可以上传其他文件扩展名,例如pdf等

    怎样才能避免呢?或者找到一种合适的方式进行表单验证

    奏鸣曲文献:

    帮助了我,但不是为了表单验证

    我错过了什么


    我更改了以下文件:

    #app/config/sonata_config.yml
    
    sonata_media:
    default_context: images_file
    db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr
    contexts:
        pdf_file:
            providers:
                - sonata.media.provider.file
            formats: ~
        images_file:
            providers:
                - sonata.media.provider.image
            formats:
                1x: { width: 870 , height: 412 , quality: 80 }
                2x: { width: 1740 , height: 824 , quality: 50 }
        svg_file:
            providers:
                - sonata.media.provider.file
            formats: ~
        cdn:
            server:
              path: /uploads/media # http://media.sonata-project.org/
    
        filesystem:
            local:
               directory:  %kernel.root_dir%/../web/uploads/media
               create:     false
    
        providers:
            file:
               service:    sonata.media.provider.file
               resizer:    false
               filesystem: sonata.media.filesystem.local
               cdn:        sonata.media.cdn.server
               generator:  sonata.media.generator.default
               thumbnail:  sonata.media.thumbnail.format
               allowed_extensions: ['pdf', 'txt', 'rtf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pttx', 'odt', 'odg', 'odp', 'ods', 'odc', 'odf', 'odb', 'csv', 'xml','svg']
               allowed_mime_types: ['application/pdf', 'application/x-pdf', 'application/rtf', 'text/html', 'text/rtf', 'text/plain', 'image/svg+xml']
    
    表格文件:

    use Sonata\AdminBundle\Admin\Admin;
    class CustomAdmin extends Admin
    {
    /**
     * @param FormMapper $formMapper
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add(
                'NormalLogo',
                'sonata_type_model_list',
                array('required' => false),
                array(
                    'link_parameters' => array('context' => 'images_file', 'provider' => 'sonata.media.provider.image'),
                )
            )
            ->add(
                'SvgLogo',
                'sonata_type_model_list',
                array('required' => false),
                array(
                    'link_parameters' => array('context' => 'svg_file', 'provider' => 'sonata.media.provider.file'),
                )
            )
            ->add('overriddenBy', 'sonata_type_model',
                array(
                    'empty_value' => 'Not overridden',
                    'btn_add' => false,
                    'btn_list' => false,
                    'btn_delete' => false,
                    'btn_catalogue' => false,
                )
            );
    }
    }
    

    您可以为SVG文件创建自己的提供程序。要做到这一点,您需要首先为SVG提供程序定义一个服务

    parameters:
        application_sonata_media.svg_class: Application\Sonata\MediaBundle\Provider\SVGProvider
    services:
        sonata.media.provider.svg:
              class: %application_sonata_media.svg_class%
              tags:
                  - { name: sonata.media.provider }
              arguments:
                  - sonata.media.provider.svg
                  - @sonata.media.filesystem.local
                  - @sonata.media.cdn.server
                  - @sonata.media.generator.default
                  - @sonata.media.thumbnail.format
                  - allowed_extensions: ['svg']
                  - allowed_mime_types: ['image/svg+xml']
    
    使用sonata,您可以生成扩展捆绑包。默认情况下,它会在
    src/Application/sonata/MediaBundle
    中生成扩展捆绑包,但您也可以指定其他目标。现在,在扩展媒体捆绑包的
    services.yml
    中创建上述服务,并在main
    config.yml
    中导入。如果您想添加更多mime类型或者您可以在上述服务中定义的扩展
    允许的mime类型:['image/svg+xml','application/pdf']

    imports:
        - { resource: @ApplicationSonataMediaBundle/Resources/config/services.yml }
    
    现在,在扩展媒体包中创建提供者类作为
    SVGProvider
    ,并使用sonata media的
    FileProvider

    <?php
    namespace Application\Sonata\MediaBundle\Provider;
    
    use Sonata\MediaBundle\Provider\FileProvider as BaseFileProvider;
    use Gaufrette\Filesystem;
    use Sonata\AdminBundle\Form\FormMapper;
    use Sonata\AdminBundle\Validator\ErrorElement;
    use Sonata\MediaBundle\CDN\CDNInterface;
    use Sonata\MediaBundle\Generator\GeneratorInterface;
    use Sonata\MediaBundle\Metadata\MetadataBuilderInterface;
    use Sonata\MediaBundle\Model\MediaInterface;
    use Sonata\MediaBundle\Thumbnail\ThumbnailInterface;
    use Symfony\Component\HttpFoundation\File\File;
    use Symfony\Component\HttpFoundation\File\UploadedFile;
    use Symfony\Component\Validator\Constraints\NotBlank;
    use Symfony\Component\Validator\Constraints\NotNull;
    
    class SVGProvider extends BaseFileProvider {
        protected $allowedMimeTypes;
        protected $allowedExtensions;
        protected $metadata;
    
        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;
        }
    
        public function buildCreateForm( FormMapper $formMapper ) {
            $formMapper->add( 'binaryContent', 'file', array(
                'label'       => 'Upload SVG file only',
                'constraints' => array(
                    new NotBlank(),
                    new NotNull(),
                ),
            ) );
        }
    
        /**
         * {@inheritdoc}
         */
        public function validate( ErrorElement $errorElement, MediaInterface $media ) {
    
            if ( ! $media->getBinaryContent() instanceof \SplFileInfo ) {
                return;
            }
    
            if ( $media->getBinaryContent() instanceof UploadedFile ) {
                $fileName = $media->getBinaryContent()->getClientOriginalName();
            } elseif ( $media->getBinaryContent() instanceof File ) {
                $fileName = $media->getBinaryContent()->getFilename();
            } else {
                throw new \RuntimeException( sprintf( 'Invalid binary content type: %s', get_class( $media->getBinaryContent() ) ) );
            }
    
            if ( ! in_array( strtolower( pathinfo( $fileName, PATHINFO_EXTENSION ) ), $this->allowedExtensions ) ) {
                $errorElement
                    ->with( 'binaryContent' )
                    ->addViolation( 'Invalid extensions' )
                    ->end();
            }
    
            if ( ! in_array( $media->getBinaryContent()->getMimeType(), $this->allowedMimeTypes ) ) {
                $errorElement
                    ->with( 'binaryContent' )
                    ->addViolation( 'Invalid mime type : ' . $media->getBinaryContent()->getMimeType() )
                    ->end();
            }
        }
    }
    

    还是一个有效的解决方案,谢谢。我对它做了一点修改,可以找到它。
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add(
                'SvgLogo',
                'sonata_type_model_list',
                array('required' => false),
                array(
                    'link_parameters' => array('context' => 'svg_file', 'provider' => 'sonata.media.provider.svg'),
                )
            )
    }