diff --git a/src/Controller/CapsuleController.php b/src/Controller/CapsuleController.php index b29733250dcc8901215d1eee9a7f5341295d69d1..894664f804cd3b9089b06a219d4b0a0b0e886f9c 100755 --- a/src/Controller/CapsuleController.php +++ b/src/Controller/CapsuleController.php @@ -21,6 +21,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Uid\Uuid; +use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\Translation\TranslatorInterface; class CapsuleController extends AbstractController @@ -60,7 +61,8 @@ class CapsuleController extends AbstractController #[Route('/{_locale<%app.supported_locales%>}', name:'home')] public function index( PaginatorInterface $paginator, - Request $request + Request $request, + HttpClientInterface $http_client ): Response { $current_user = $this->getUser(); @@ -97,7 +99,8 @@ class CapsuleController extends AbstractController 'filterByGroupForm' => $form, 'capsules' => $capsules, 'current_user' => $current_user, - 'legacy_url' => $this->getParameter('app.legacy_external_prefix') + 'legacy_url' => $this->getParameter('app.legacy_external_prefix'), + 'http_client' => $http_client ]); } diff --git a/src/Entity/Capsule.php b/src/Entity/Capsule.php index 47420222ffd1f9fde291a0724e7ac711da0a233f..b397ec3d67a07df3cefc208ae525ab493d564b30 100755 --- a/src/Entity/Capsule.php +++ b/src/Entity/Capsule.php @@ -12,6 +12,7 @@ use Doctrine\ORM\Mapping\UniqueConstraint; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Contracts\HttpClient\HttpClientInterface; #[ORM\Table(name:'capsule')] #[UniqueConstraint(name:'index_capsule_nom', columns:['nom'])] @@ -191,7 +192,7 @@ class Capsule return $this->editors; } - public function getVideoPreviewImageLink(): ?string + public function getVideoPreviewImageLink(HttpClientInterface $http_client): ?string { $file_system = new Filesystem(); @@ -204,7 +205,15 @@ class Capsule $video_id = $project_retriever->getVideoId($video_url); if (str_contains($video_url, 'yout')) { - return 'https://img.youtube.com/vi/' . $video_id . '/maxresdefault.jpg'; + $url = 'https://img.youtube.com/vi/%s/%s.jpg'; + $high_resolution = sprintf($url, $video_id, 'maxresdefault'); + $medium_resolution = sprintf($url, $video_id, 'mqdefault'); + try { + $http_client->request('GET', $high_resolution); + return $high_resolution; + } catch (\Exception $e) { + return $medium_resolution; + } } return 'https://vumbnail.com/' . $video_id . '.jpg'; diff --git a/templates/capsule/index.html.twig b/templates/capsule/index.html.twig index e0c7b9187de90ea531a7882c810e7ec945647333..f3d0c508324a3b236ad12158f2f0e7d4643bbc8a 100644 --- a/templates/capsule/index.html.twig +++ b/templates/capsule/index.html.twig @@ -62,10 +62,10 @@ <div class="d-flex flex-column flex-xl-row justify-content-center align-items-center"> <div class="m-4 ratio ratio-16x9"> - {% if capsule.getVideoPreviewImageLink() is null %} + {% if capsule.getVideoPreviewImageLink(http_client) is null %} <img src="{{ asset('build/images/project_not_found.jpg') }}" alt="video preview"> {% else %} - <img src="{{ capsule.getVideoPreviewImageLink() }}" alt="video preview"> + <img src="{{ capsule.getVideoPreviewImageLink(http_client) }}" alt="video preview"> {% endif %} </div>