From 0a4bdf735ec234269ac4cecee6d8528af128195e Mon Sep 17 00:00:00 2001 From: Camille Simiand <camille.simiand@tetras-libre.fr> Date: Mon, 21 Mar 2022 16:41:37 +0100 Subject: [PATCH] Use medium quality thumbnail if no maxres --- src/Controller/CapsuleController.php | 7 +++++-- src/Entity/Capsule.php | 13 +++++++++++-- templates/capsule/index.html.twig | 4 ++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Controller/CapsuleController.php b/src/Controller/CapsuleController.php index b297332..894664f 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 4742022..b397ec3 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 e0c7b91..f3d0c50 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> -- GitLab