src/Controller/ImageCache/ImageCacheController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ImageCache;
  3. use App\Service\UrlImageCacheService;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. class ImageCacheController extends AbstractController
  8. {
  9. /**
  10. * Logo URL
  11. */
  12. const UVDESK_LOGO = 'https://updates.uvdesk.com/uvdesk-logo.png';
  13. protected $urlImageCacheService;
  14. protected $urlGenerator;
  15. public function __construct(
  16. UrlImageCacheService $urlImageCacheService
  17. ) {
  18. $this->urlImageCacheService = $urlImageCacheService;
  19. }
  20. /**
  21. * Get the cached image response
  22. * @return Response
  23. */
  24. public function getCachedImage(Request $request): Response
  25. {
  26. $siteUrl = $request->getSchemeAndHttpHost() . $request->getBasePath();
  27. $response = $this->showImage(self::UVDESK_LOGO, $siteUrl);
  28. // Get the file and its real path
  29. $file = $response->getFile();
  30. $filePath = $file->getRealPath();
  31. // Get the relative path by removing the kernel.project_dir
  32. $relativePath = str_replace($this->getParameter('kernel.project_dir') . '/public', '', $filePath);
  33. // Construct the image URL by combining the base URL and the relative path
  34. $imageUrl = $siteUrl . $relativePath;
  35. // Return the image URL as JSON
  36. return new Response(json_encode($imageUrl));
  37. }
  38. public function showImage(string $imageUrl, string $domain): Response
  39. {
  40. $cachedImagePath = $this->urlImageCacheService->getCachedImage($imageUrl, $domain);
  41. return $this->file($cachedImagePath);
  42. }
  43. }