Skip to content
Snippets Groups Projects
Commit 0a4bdf73 authored by Camille Simiand's avatar Camille Simiand
Browse files

Use medium quality thumbnail if no maxres

parent a8c697cd
No related branches found
No related tags found
1 merge request!80tuleap-194-some-youtube-video-thumbnail-are-not-retrieved
Pipeline #974 passed
......@@ -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
]);
}
......
......@@ -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';
......
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment