sonata-project / SonataMediaBundle

Symfony SonataMediaBundle
https://docs.sonata-project.org/projects/SonataMediaBundle
MIT License
449 stars 496 forks source link

Exception on youtube media creation #1804

Closed r-sede closed 3 years ago

r-sede commented 4 years ago

Environment

   Symfony 4.4 website

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.72.0 3.75.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.20.0 3.21.0 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/classification-bundle     3.12.1 3.13.2 Symfony SonataClassificationBundle
sonata-project/core-bundle               3.20.0 3.20.0 Symfony SonataCoreBundle (abandoned)
Package sonata-project/core-bundle is abandoned, you should avoid using it. No replacement was suggested.
sonata-project/datagrid-bundle           2.5.0  3.2.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.8.0  1.9.1  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.21.0 3.22.0 Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.5.0  2.5.0  Symfony SonataEasyExtendsBundle
sonata-project/exporter                  2.3.0  2.3.0  Lightweight Exporter library
sonata-project/form-extensions           0.1.2  1.6.0  Symfony form extensions
sonata-project/formatter-bundle          4.2.0  4.2.0  Symfony SonataFormatterBundle
sonata-project/media-bundle              3.25.0 3.27.0 Symfony SonataMediaBundle
sonata-project/seo-bundle                2.11.0 2.12.0 Symfony SonataSeoBundle
sonata-project/twig-extensions           0.1.1  1.4.1  Sonata twig extensions
sonata-project/user-bundle               4.6.0  4.8.0  Symfony SonataUserBundle

Symfony packages

$ composer show --latest 'symfony/*'
Restricting packages listed in "symfony/symfony" to "4.4.*"
symfony/apache-pack                v1.0.1  v1.0.1  A pack for Apache support in Symfony
symfony/asset                      v4.4.11 v4.4.13 Symfony Asset Component
symfony/cache                      v4.4.11 v4.4.13 Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v2.1.3  v2.1.3  Generic abstractions related to caching
symfony/config                     v4.4.11 v4.4.13 Symfony Config Component
symfony/console                    v4.4.11 v4.4.13 Symfony Console Component
symfony/debug                      v4.4.11 v4.4.13 Symfony Debug Component
symfony/debug-bundle               v4.4.11 v4.4.13 Symfony DebugBundle
symfony/dependency-injection       v4.4.11 v4.4.13 Symfony DependencyInjection Component
symfony/deprecation-contracts      v2.1.3  v2.1.3  A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge            v4.4.11 v4.4.13 Symfony Doctrine Bridge
symfony/dotenv                     v4.4.11 v4.4.13 Registers environment variables from a .env file
symfony/error-handler              v4.4.11 v4.4.13 Symfony ErrorHandler Component
symfony/event-dispatcher           v4.4.11 v4.4.13 Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.9  v2.1.3  Generic abstractions related to dispatching event
symfony/expression-language        v4.4.11 v4.4.13 Symfony ExpressionLanguage Component
symfony/filesystem                 v4.4.11 v4.4.13 Symfony Filesystem Component
symfony/finder                     v4.4.11 v4.4.13 Symfony Finder Component
symfony/flex                       v1.9.1  v1.9.3  Composer plugin for Symfony
symfony/form                       v4.4.11 v4.4.13 Symfony Form Component
symfony/framework-bundle           v4.4.11 v4.4.13 Symfony FrameworkBundle
symfony/http-client                v4.4.11 v4.4.13 Symfony HttpClient component
symfony/http-client-contracts      v2.1.3  v2.1.3  Generic abstractions related to HTTP clients
symfony/http-foundation            v4.4.11 v4.4.13 Symfony HttpFoundation Component
symfony/http-kernel                v4.4.11 v4.4.13 Symfony HttpKernel Component
symfony/inflector                  v4.4.11 v4.4.13 Symfony Inflector Component
symfony/intl                       v4.4.11 v4.4.13 A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/mailer                     v4.4.11 v4.4.13 Symfony Mailer Component
symfony/mime                       v4.4.11 v4.4.13 A library to manipulate MIME messages
symfony/monolog-bridge             v4.4.11 v4.4.13 Symfony Monolog Bridge
symfony/monolog-bundle             v3.5.0  v3.5.0  Symfony MonologBundle
symfony/options-resolver           v4.4.11 v4.4.13 Symfony OptionsResolver Component
symfony/orm-pack                   v1.1.0  v2.0.0  A pack for the Doctrine ORM
symfony/polyfill-intl-grapheme     v1.18.0 v1.18.1 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          v1.18.0 v1.18.1 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.18.0 v1.18.1 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   v1.18.0 v1.18.1 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          v1.18.0 v1.18.1 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72             v1.18.0 v1.18.1 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73             v1.18.0 v1.18.1 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80             v1.18.0 v1.18.1 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/process                    v4.4.11 v4.4.13 Symfony Process Component
symfony/property-access            v4.4.11 v4.4.13 Symfony PropertyAccess Component
symfony/routing                    v4.4.11 v4.4.13 Symfony Routing Component
symfony/security-acl               v3.0.4  v3.1.0  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.4.11 v4.4.13 Symfony SecurityBundle
symfony/security-core              v4.4.11 v4.4.13 Symfony Security Component - Core Library
symfony/security-csrf              v4.4.11 v4.4.13 Symfony Security Component - CSRF Library
symfony/security-guard             v4.4.11 v4.4.13 Symfony Security Component - Guard
symfony/security-http              v4.4.11 v4.4.13 Symfony Security Component - HTTP Integration
symfony/serializer                 v4.4.11 v4.4.13 Symfony Serializer Component
symfony/service-contracts          v2.1.3  v2.1.3  Generic abstractions related to writing services
symfony/stopwatch                  v4.4.11 v4.4.13 Symfony Stopwatch Component
symfony/string                     v5.1.3  v5.1.5  Symfony String component
symfony/templating                 v4.4.11 v4.4.13 Symfony Templating Component
symfony/translation                v4.4.11 v4.4.13 Symfony Translation Component
symfony/translation-contracts      v2.1.3  v2.1.3  Generic abstractions related to translation
symfony/twig-bridge                v4.4.11 v4.4.13 Symfony Twig Bridge
symfony/twig-bundle                v4.4.11 v4.4.13 Symfony TwigBundle
symfony/validator                  v4.4.11 v4.4.13 Symfony Validator Component
symfony/var-dumper                 v4.4.11 v4.4.13 Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v4.4.11 v4.4.13 A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-profiler-bundle        v4.4.11 v4.4.13 Symfony WebProfilerBundle
symfony/webpack-encore-bundle      v1.7.3  v1.7.3  Integration with your Symfony app & Webpack Encore!
symfony/yaml                       v4.4.11 v4.4.13 Symfony Yaml Component

PHP version

$ php -v
PHP 7.2.3 (cli) (built: Mar 22 2018 22:03:09) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.9.1, Copyright (c) 2002-2020, by Derick Rethans

Subject

I have one media context like:

        post:
            providers:
                - sonata.media.provider.image
                - sonata.media.provider.youtube
            formats:
                small: { width: 350, quality: 70 }
                big: { width: 510,  quality: 70 }

when a try to choose a youtube media for my post it throw me a Exception

Call to a member function getPathname() on string

Error:
Call to a member function getPathname() on string

  at vendor/sonata-project/media-bundle/src/Validator/Constraints/ImageUploadDimensionValidator.php:72
  at Sonata\MediaBundle\Validator\Constraints\ImageUploadDimensionValidator->validate(object(Media), object(ImageUploadDimension))
     (vendor/symfony/validator/Validator/RecursiveContextualValidator.php:767)
  at Symfony\Component\Validator\Validator\RecursiveContextualValidator->validateInGroup(object(Media), '00000000614e2c0d0000000030ac40f0', object(ClassMetadata), 'Default', object(ExecutionContext))
     (vendor/symfony/validator/Validator/RecursiveContextualValidator.php:492)
  at Symfony\Component\Validator\Validator\RecursiveContextualValidator->validateClassNode(object(Media), '00000000614e2c0d0000000030ac40f0', object(ClassMetadata), 'data', array('Default'), null, 1, object(ExecutionContext))
     (vendor/symfony/validator/Validator/RecursiveContextualValidator.php:322)
  at Symfony\Component\Validator\Validator\RecursiveContextualValidator->validateObject(object(Media), 'data', array('Default'), 1, object(ExecutionContext))
     (vendor/symfony/validator/Validator/RecursiveContextualValidator.php:138)
  at Symfony\Component\Validator\Validator\RecursiveContextualValidator->validate(object(Media), null, array('Default'))
     (vendor/symfony/form/Extension/Validator/Constraints/FormValidator.php:104)
  at Symfony\Component\Form\Extension\Validator\Constraints\FormValidator->validate(object(Form), object(Form))
     (vendor/symfony/validator/Validator/RecursiveContextualValidator.php:767)
  at Symfony\Component\Validator\Validator\RecursiveContextualValidator->validateInGroup(object(Form), '00000000614e2d590000000030ac40f0', object(ClassMetadata), 'Default', object(ExecutionContext))
     (vendor/symfony/validator/Validator/RecursiveContextualValidator.php:492)
  at Symfony\Component\Validator\Validator\RecursiveContextualValidator->validateClassNode(object(Form), '00000000614e2d590000000030ac40f0', object(ClassMetadata), '', array('Default'), null, 1, object(ExecutionContext))
     (vendor/symfony/validator/Validator/RecursiveContextualValidator.php:322)
  at Symfony\Component\Validator\Validator\RecursiveContextualValidator->validateObject(object(Form), '', array('Default'), 1, object(ExecutionContext))
     (vendor/symfony/validator/Validator/RecursiveContextualValidator.php:138)
  at Symfony\Component\Validator\Validator\RecursiveContextualValidator->validate(object(Form), null, array('Default'))
     (vendor/symfony/validator/Validator/RecursiveValidator.php:93)
  at Symfony\Component\Validator\Validator\RecursiveValidator->validate(object(Form), null, null)
     (vendor/symfony/validator/Validator/TraceableValidator.php:66)
  at Symfony\Component\Validator\Validator\TraceableValidator->validate(object(Form))
     (vendor/symfony/form/Extension/Validator/EventListener/ValidationListener.php:50)
  at Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener->validateForm(object(PostSubmitEvent), 'form.post_submit', object(EventDispatcher))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:264)
  at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(Closure), object(Closure), object(Closure)), 'form.post_submit', object(PostSubmitEvent))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:239)
  at Symfony\Component\EventDispatcher\EventDispatcher->callListeners(array(object(Closure), object(Closure), object(Closure)), 'form.post_submit', object(PostSubmitEvent))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:73)
  at Symfony\Component\EventDispatcher\EventDispatcher->dispatch(object(PostSubmitEvent), 'form.post_submit')
     (vendor/symfony/event-dispatcher/ImmutableEventDispatcher.php:44)
  at Symfony\Component\EventDispatcher\ImmutableEventDispatcher->dispatch(object(PostSubmitEvent), 'form.post_submit')
     (vendor/symfony/form/Form.php:671)
  at Symfony\Component\Form\Form->submit(array(), true)
     (vendor/symfony/form/Extension/HttpFoundation/HttpFoundationRequestHandler.php:109)
  at Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler->handleRequest(object(Form), object(Request))
     (vendor/symfony/form/Form.php:493)
  at Symfony\Component\Form\Form->handleRequest(object(Request))
     (vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php:577)
  at Sonata\AdminBundle\Controller\CRUDController->createAction()
     (vendor/sonata-project/media-bundle/src/Controller/MediaAdminController.php:45)
  at Sonata\MediaBundle\Controller\MediaAdminController->createAction(object(Request))
     (vendor/symfony/http-kernel/HttpKernel.php:158)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:80)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:201)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:25)

in vendor/sonata-project/media-bundle/src/Validator/Constraints/ImageUploadDimensionValidator.php line 72 if i replace:

        try {
            $image = $this->imagineAdapter->open($value->getBinaryContent()->getPathname());
        } catch (\RuntimeException $e) {
            // Do nothing. The parent validator will throw a violation error.
            return;
        }

by:

        try {
            $image = $this->imagineAdapter->open($value->getBinaryContent()->getPathname());
        } catch (\Throwable $e) {
            // Do nothing. The parent validator will throw a violation error.
            return;
        }

it works

Expected results

a youtube media

Actual results

an Exception on create

VincentLanglet commented 4 years ago

The issue is that getPathname() is called on

/**
 * @return mixed
 */
public function getBinaryContent();

A check

if ($binaryContent instanceof File) {

seems needed.

ibasaw commented 3 years ago

got same error on sf 4.4

VincentLanglet commented 3 years ago

The issue is that getPathname() is called on

/**
 * @return mixed
 */
public function getBinaryContent();

A check

if ($binaryContent instanceof File) {

seems needed.

I gave the solution

You need to change

try {
            $image = $this->imagineAdapter->open($value->getBinaryContent()->getPathname());
        } catch (\RuntimeException $e) {
            // Do nothing. The parent validator will throw a violation error.
            return;
        }

to something like

     try {
            $binaryContent = $value->getBinaryContent();

            if (!$binaryContent instanceof File) {
                return;
            }

            $image = $this->imagineAdapter->open($binaryContent->getPathname());
        } catch (\RuntimeException $e) {
            // Do nothing. The parent validator will throw a violation error.
            return;
        }

If you want to make a PR @ibasaw

ibasaw commented 3 years ago

@VincentLanglet you 're absolutely right, this is working perfectly with your proposal. thank you for your quick answer.

I can't do a PR because i already fork this project, and i need to keep my old fork which is used in prod on many websites i manage.

phansys commented 3 years ago

Thank you for the confirmation.

I can't do a PR because i already fork this project, and i need to keep my old fork which is used in prod on many websites i manage.

Regardless your fork is being used in other projects, I think you could create a new branch from this remote without any impact:

git remote add upstream git@github.com:sonata-project/SonataMediaBundle.git
git fetch upstream
git checkout -b issue_1804 upstream/3.x
# apply and commit your changes
git push origin issue_1804
tambait commented 3 years ago

Run into the same issue this morning on Symfony 4.4 and Media Bundle 3.27

apuzanoff commented 3 years ago

I still have exact this issue with Symfony 4.4.26 and Media Bundle 3.13 which were updated month ago. Was it really fixed ? It looks as not...

VincentLanglet commented 3 years ago

Media Bundle 3.13

You're so 2018