sonata-project / SonataMediaBundle

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

YouTubeProvider fixBinaryContent() does not store Share URL regex match #314

Closed wcluijt closed 9 years ago

wcluijt commented 11 years ago

For a YouTube input field, a user could input the following types of URLs:

Browser URL - http://www.youtube.com/watch?v=J---aiyznGQ Share URL - http://youtu.be/J---aiyznGQ

When a media object is transformed using the YouTubeProvider (during a form submission and saving of an entity for instance), it eventually calls the fixBinaryContent() method, which contains the following lines:

if (preg_match("/(?<=v(\=|\/))([-a-zA-Z0-9_]+)|(?<=youtu\.be\/)([-a-zA-Z0-9_]+)/", $media->getBinaryContent(), $matches)) {
    $media->setBinaryContent($matches[2]);
}

The regex itself is fine, but the value passed to setBinaryContent is not always the correct one.

After some debugging, when the above example Browser URL is returned by $media->getBinaryContent(), the resulting preg_match() $matches variable looks like the following:

$matches = array(
    0 => 'J---aiyznGQ',
    1 => '=',
    2 => 'J---aiyznGQ',
);

Browser URL values store the correct value as expected.

When the above example Share URL is returned by $media->getBinaryContent(), the resulting preg_match() $matches variable looks like the following:

$matches = array(
    0 => 'J---aiyznGQ',
    1 => '',
    2 => '',
    3 => 'J---aiyznGQ',
);

So, when a Share URL is used, the media does not have a reference to the YouTube URL since fixBinaryContent only saves the $matches[2] value.

From the data, it looks like the setBinaryContent line could be changed to:

$media->setBinaryContent($matches[0]);

But maybe a safer way is to change it to something like:

$media->setBinaryContent(isset($matches[3]) && $matches[3] ? $matches[3] : $matches[2]);
rande commented 9 years ago

fixed https://github.com/sonata-project/SonataMediaBundle/commit/bf98f791e7c09067f75622ffb985cb3d3df094ce